Alpine.js: The Recording Studio

Understanding `$el`, `$refs`, and `$root`

Studio Log

Awaiting engineer actions...

$el: The Current Track

An element refers to itself. Like a track button that knows how to 'solo' or 'mute' itself.

Digital Audio Workstation (DAW) Tracks

Code Example:

<button @click="toggleTrackState($el, 'Solo')">
  Track 1: Vocals
</button>

$refs: Named Equipment

Access any named element from anywhere in the component. Like a central mixing board controlling specific, named pieces of gear.

🎤 Mic 1
🎸 Guitar Amp
🎹 Keyboard
🥁 Drum Machine

Mixing Board

Code Example:

<div x-ref="mic1">...</div>

<button @click="adjustEquipment('mic1')">
  ...
</button>

// In JS: this.$refs.mic1.classList.add(...)

$root: The Entire Studio

Access the top-level component element. Like a master power switch that affects the entire studio at once.

Studio Master Controls

This button affects the entire demo area, including the other panels.

Code Example:

<div x-data="studioDemo">
  ...
  <button @click="togglePower($root)">
    ...
  </button>
</div>