New Alpine.js Plugins: Intersect, Persist, and Trap
Published on by Jason Beggs
Since the launch of Alpine.js v3, Caleb Porzio (creator of Alpine) has released three new first-party plugins. The new plugins each take something that is confusing or just annoying to maintain in JavaScript and makes it much simpler to implement with a few custom attributes in your HTML.
Intersect
The Intersect plugin provides a nice wrapper around Intersection Observer. The plugin makes it much easier and intuitive to implement UI patterns like infinite scrolling and lazy loading images. Another great use case is animating elements onto the screen when a user scrolls into that section on a marketing page.
To use the plugin, follow the installation instructions, then add the x-intersect
attribute to an element with a callback that should be ran whenever that element "intersects" with the user's viewport.
<div x-data="{ shown: false }" x-intersect="shown = true"> <div x-show="shown" x-transition> I'm in the viewport! </div></div>
You can also use x-intersect:enter="callback"
or x-intersect:leave="callback"
if you want the callback to only run when the element enters or leaves the user's viewport.
Additionally, the plugin supports a .once
modifier which can be used to denote that the callback should only be ran the first time the element intersects the user's viewport.
<div x-intersect.once="shown = true">...</div>
Persist
The Persist plugin provides a new magic method for integrating your Alpine dataset with localStorage.
To use the plugin, follow the installation instructions, then wrap your initial data values in $persist()
. Alpine will then know to "persist" those values - and any updates to those values - to localStorage. On subsequent page loads, Alpine will then pull those values back out of localStorage instead of initializing them with the default values.
<div x-data="{ count: $persist(0) }"> <button x-on:click="count++">Increment</button> <span x-text="count"></span></div>
Trap
The Trap plugin provides a simple way to "trap" focus inside elements like modals and and other dialogue elements. Trapping focus is very beneficial for users using screen readers or navigating via keyboard in general.
To use the plugin, follow the installation instructions, then add the x-trap
attribute to your modal/dialogue element. Any time the expression that's passed to x-trap
evaluates to true
, the plugin will trap focus inside the element.
<div x-data="{ open: false}"> <button @click="open = true">Open Dialogue</button> <span x-show="open" x-trap="open"> <p>...</p> <input type="text" placeholder="Some input..."> <input type="text" placeholder="Some other input..."> <button @click="open = false">Close Dialogue</button> </span></div>
One really nice aspect of the plugin is that it works recursively. This means that if you have nested dialogue elements, Alpine will trap focus inside each one, then return focus to the previous one as they are closed. This is something that would be extremely tedious to do in your own code without the plugin.
Conclusion
These new plugins have been really awesome additions to the framework. They're super-focused in scope, and you only have to install them in the projects that need them.
Do you have a really cool idea for a new plugin or have you built already built an Alpine plugin? I'd love to hear about it on Twitter (@jasonlbeggs)!
TALL stack (Tailwind CSS, Alpine.js, Laravel, and Livewire) consultant and owner of designtotailwind.com.