New Alpine.js Plugins: Intersect, Persist, and Trap

Published on

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.

1<div x-data="{ shown: false }" x-intersect="shown = true">
2 <div x-show="shown" x-transition>
3 I'm in the viewport!
4 </div>
5</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.

1<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.

1<div x-data="{ count: $persist(0) }">
2 <button x-on:click="count++">Increment</button>
3 
4 <span x-text="count"></span>
5</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.

1<div x-data="{ open: false}">
2 <button @click="open = true">Open Dialogue</button>
3 
4 <span x-show="open" x-trap="open">
5 <p>...</p>
6 
7 <input type="text" placeholder="Some input...">
8 
9 <input type="text" placeholder="Some other input...">
10 
11 <button @click="open = false">Close Dialogue</button>
12 </span>
13</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)!