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 designtotailwind.com.

Filed in:
Cube

Laravel Newsletter

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

Laravel Forge logo

Laravel Forge

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

Laravel Forge
Tinkerwell logo

Tinkerwell

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

Tinkerwell
No Compromises logo

No Compromises

Joel and Aaron, the two seasoned devs from the No Compromises podcast, are now available to hire for your Laravel project. ⬧ Flat rate of $7500/mo. ⬧ No lengthy sales process. ⬧ No contracts. ⬧ 100% money back guarantee.

No Compromises
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
Lucky Media logo

Lucky Media

Get Lucky Now - the ideal choice for Laravel Development, with over a decade of experience!

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
LaraJobs logo

LaraJobs

The official Laravel job board

LaraJobs
SaaSykit: Laravel SaaS Starter Kit logo

SaaSykit: Laravel SaaS Starter Kit

SaaSykit is a Laravel SaaS Starter Kit that comes with all features required to run a modern SaaS. Payments, Beautiful Checkout, Admin Panel, User dashboard, Auth, Ready Components, Stats, Blog, Docs and more.

SaaSykit: Laravel SaaS Starter Kit
Rector logo

Rector

Your partner for seamless Laravel upgrades, cutting costs, and accelerating innovation for successful companies

Rector
MongoDB logo

MongoDB

Enhance your PHP applications with the powerful integration of MongoDB and Laravel, empowering developers to build applications with ease and efficiency. Support transactional, search, analytics and mobile use cases while using the familiar Eloquent APIs. Discover how MongoDB's flexible, modern database can transform your Laravel applications.

MongoDB

The latest

View all →
Add Comments to your Laravel Application with the Commenter Package image

Add Comments to your Laravel Application with the Commenter Package

Read article
Laravel Advanced String Package image

Laravel Advanced String Package

Read article
Take the Annual State of Laravel 2024 Survey image

Take the Annual State of Laravel 2024 Survey

Read article
Upload Files Using Filepond in Livewire Components image

Upload Files Using Filepond in Livewire Components

Read article
The Best Laravel Tutorials and Resources for Developers image

The Best Laravel Tutorials and Resources for Developers

Read article
Introducing Built with Laravel image

Introducing Built with Laravel

Read article