Tinkerwell - The PHP Scratchpad

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.

image
Battle Ready Laravel

The ultimate guide to auditing, testing, fixing and improving your Laravel applications so you can build better apps faster and with more confidence.

Visit Battle Ready Laravel
Curotec logo

Curotec

World class Laravel experts with GenAI dev skills. LATAM-based, embedded engineers that ship fast, communicate clearly, and elevate your product. No bloat, no BS.

Curotec
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
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
Cut PHP Code Review Time & Bugs into Half with CodeRabbit logo

Cut PHP Code Review Time & Bugs into Half with CodeRabbit

CodeRabbit is an AI-powered code review tool that specializes in PHP and Laravel, running PHPStan and offering automated PR analysis, security checks, and custom review features while remaining free for open-source projects.

Cut PHP Code Review Time & Bugs into Half with CodeRabbit
Join the Mastering Laravel community logo

Join the Mastering Laravel community

Connect with experienced developers in a friendly, noise-free environment. Get insights, share ideas, and find support for your coding challenges. Join us today and elevate your Laravel skills!

Join the Mastering Laravel community
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
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 Multi-tenant 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

The latest

View all →
Streamline Conditional Logic with Laravel's Fluent Conditionable Trait image

Streamline Conditional Logic with Laravel's Fluent Conditionable Trait

Read article
Sublime Text Releases Update With Support for Right Sidebar image

Sublime Text Releases Update With Support for Right Sidebar

Read article
Enhance Email Validation with Laravel's Fluent Email Rule Object image

Enhance Email Validation with Laravel's Fluent Email Rule Object

Read article
Locale-aware Number Parsing in Laravel 12.15 image

Locale-aware Number Parsing in Laravel 12.15

Read article
Handle Fluent Values as Arrays with Laravel's array() Method image

Handle Fluent Values as Arrays with Laravel's array() Method

Read article
Chargebee Starter Kit for Billing in Laravel image

Chargebee Starter Kit for Billing in Laravel

Read article