Understanding $el, $refs, and $root
All significant actions are recorded here.
Log is empty. Start an experiment!
`$el` is like the specific beaker or petri dish you are currently focused on. It gives you direct, immediate access to manipulate the properties of that single item.
<!-- The button modifies itself using $el -->
<button @click="heat($el)">Heat Beaker</button>
<script>
Alpine.data('experiment', () => ({
heat(buttonEl) {
// Use the passed $el to modify the button
buttonEl.textContent = 'Heating...';
buttonEl.disabled = true;
// ... modify component state
}
}));
</script>
`$refs` are like specifically named, high-tech instruments in your lab (e.g., 'Spectrometer', 'Centrifuge'). From a central control panel, you can refer to them by name and operate them from a distance.
Spectrometer
Status: Idle
Centrifuge
Status: Idle
Microscope
Status: Idle
<div x-data="controlPanel">
<!-- The control button -->
<button @click="activate('spectrometer')">Activate</button>
<!-- The named instrument -->
<div x-ref="spectrometer">...</div>
</div>
<script>
Alpine.data('controlPanel', () => ({
activate(instrumentName) {
// Use $refs to access the element by name
const instrument = this.$refs[instrumentName];
instrument.classList.add('active-styles');
}
}));
</script>
`$root` refers to the entire laboratory facility—the component's outermost element. A button deep inside, like a safety alarm, can use `$root` to affect the entire lab, for instance, by triggering lab-wide lockdown lights.
Status:
This station is nested deep inside the lab.
<!-- The outer "Lab" component -->
<div x-data="laboratory" x-on:lab-alert.window="...">
<!-- The nested "Safety Station" -->
<div x-data="safetyStation">
<!-- This button can affect its $root (the Lab) -->
<button @click="triggerAlert()">Trigger Alert</button>
</div>
</div>
<script>
Alpine.data('safetyStation', () => ({
triggerAlert() {
// Directly style the root element
this.$root.classList.add('emergency-styles');
// Dispatch an event for the root component to hear
this.$dispatch('lab-alert', 'Containment Breach!');
}
}));
</script>