Understanding $el, $refs, and $root
$el refers to the DOM element the Alpine directive is on. It's like the active window you're currently interacting with. Actions affect that window directly.
This is a window.
<button @click="highlightSelf($el)">Highlight Me</button>
$refs gives you access to specific elements marked with x-ref. It's like your desktop's dock or start menu launching specific, named applications.
Control Panel
<div x-ref="browser">...</div>
<button @click="launchApp('browser')">...</button>
$root refers to the root element of the Alpine component. It's like a system-wide command that affects the entire desktop environment, like changing the wallpaper.
System Settings
This button can affect the whole component.
<div x-data="...">
<!-- Deeply nested -->
<button @click="toggleNightMode($root)">...</button>
</div>