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

Published on by

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

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)!

Jason Beggs photo

TALL stack (Tailwind CSS, Alpine.js, Laravel, and Livewire) consultant and owner of roasted.dev.

Filed in:
Cube

Laravel Newsletter

Join 40k+ other developers and never miss out on new tips, tutorials, and more.

image
Oh Dear

Oh Dear is the best all-in-one monitoring tool for all your Laravel apps.

Visit Oh Dear
Kirschbaum logo

Kirschbaum

Providing innovation and stability to ensure your web application succeeds.

Kirschbaum
Shift logo

Shift

Running an old Laravel version? Instant, automated Laravel upgrades and code modernization to keep your applications fresh.

Shift
Bacancy logo

Bacancy

Supercharge your project with a seasoned Laravel developer with 4-6 years of experience for just $2500/month. Get 160 hours of dedicated expertise & a risk-free 15-day trial. Schedule a call now!

Bacancy
LoadForge logo

LoadForge

Easy, affordable load testing and stress tests for websites, APIs and databases.

LoadForge
Paragraph logo

Paragraph

Manage your Laravel app as if it was a CMS – edit any text on any page or in any email without touching Blade or language files.

Paragraph
Lucky Media logo

Lucky Media

Bespoke software solutions built for your business. Partner with Lucky Media, your favorite Laravel Development Agency!

Lucky Media
Lunar: Laravel E-Commerce logo

Lunar: Laravel E-Commerce

E-Commerce for Laravel. An open-source package that brings the power of modern headless e-commerce functionality to Laravel.

Lunar: Laravel E-Commerce
Laravel Forge logo

Laravel Forge

Easily create and manage your servers and deploy your Laravel applications in seconds.

Laravel Forge
Oh Dear logo

Oh Dear

Oh Dear is the best all-in-one monitoring tool for all your Laravel apps.

Oh Dear
Tinkerwell logo

Tinkerwell

The must-have code runner for Laravel developers. Tinker with AI, autocompletion and instant feedback on local and production environments.

Tinkerwell

The latest

View all →
Get insights into all your Laravel notifications with Paragraphs new package image

Get insights into all your Laravel notifications with Paragraphs new package

Read article
FrankenPHP v1.0 is Here image

FrankenPHP v1.0 is Here

Read article
Self-healing URLs in Laravel image

Self-healing URLs in Laravel

Read article
Laravel 10.35 Released image

Laravel 10.35 Released

Read article
Solve n+1 queries in PHP with Scout APM image

Solve n+1 queries in PHP with Scout APM

Read article
Show Outdated Composer Dependencies in Laravel Pulse image

Show Outdated Composer Dependencies in Laravel Pulse

Read article