Understanding $el, $refs, and $root
$el: The Current SceneAn element referring to itself. It's like an actor in a scene changing their own costume or delivering a line.
<!-- $el is passed to the method -->
<button @click="changeLighting($el)">
...
</button>
<script>
// In Alpine.data component...
changeLighting(element) {
element.style.boxShadow = '0 0 15px 5px #facc15';
// ... log action
}
</script>
$refs: Named EquipmentAccessing other specific elements within the component by name. Like a director telling a named piece of equipment what to do.
<button @click="operateCamera()">...</button>
<div x-ref="cameraA">...</div>
<script>
// In Alpine.data component...
operateCamera() {
this.$refs.cameraA.classList.add('border-red-500');
// ... log action
}
</script>
$root: Entire ProductionAccessing the root element of the component. It's like the producer making a decision that affects the entire production set.
<div x-data="movieSet"> <!-- This is $root -->
<button @click="wrapProduction()">...</button>
</div>
<script>
// In Alpine.data component...
wrapProduction() {
this.isWrapped = true; // For reactive classes
this.$root.dataset.productionStatus = 'wrapped';
// ... log action
}
</script>