Skip to content
Tutorials Center
My learning

Topics

Lesson 10 of 28 ยท Alpine.js Going Further

Alpine Plugins and Other Features

Tour the official Alpine.js plugins with live demos of collapse, intersect and mask, plus x-id, x-ignore and Alpine.bind for reusing attribute sets.

Alpine's core is deliberately tiny. Features that not every page needs live in separate plugins: small scripts you add only when you want them. There are official plugins for animating height, reacting to scrolling, formatting inputs, trapping focus, positioning popups and more. This lesson gives you an overview, runs three of them live, and then covers three useful core features: x-id, x-ignore and Alpine.bind.

You should know x-data, x-show and events first. Everything here uses the same pinned version, 3.14.8, as the rest of the track.

The official plugins

Plugins for state and layout

Persist Keep a value in localStorage.
Collapse Animate height open and closed.
Anchor Position a popup next to an element.
Sort Drag and drop to reorder lists.
1 / 2

All official plugins follow one pattern. Add the plugin script before the Alpine core script, both with defer, in that order. The plugin registers itself and gives you new directives.

HTML
<script defer src="https://cdn.jsdelivr.net/npm/@alpinejs/[email protected]/dist/cdn.min.js"></script>
<script defer src="https://cdn.jsdelivr.net/npm/[email protected]/dist/cdn.min.js"></script>

If the order is reversed, Alpine has already started and the directive is ignored. With a bundler you call Alpine.plugin(collapse) before Alpine.start().

Collapse: smooth height animation

x-show toggles display, so a panel jumps open. x-collapse on the same element animates its height from zero to its natural size, which is perfect for accordions and FAQs. Add .min.50px to keep a preview visible, or .duration.500ms to change speed.

An accordion with x-collapse

Put padding on an inner element, not on the collapsing element itself, because padding on the collapsing element would stay visible at zero height.

Intersect: react to scrolling

x-intersect uses the browser's IntersectionObserver. x-intersect="code" runs when the element enters the viewport. Variants: x-intersect:leave, .once (fire a single time, great for lazy work), .half or .full (how much must be visible), and .margin.100px (start early).

Reveal elements as they scroll in

In a later lesson you will see the HTMX version of the same idea: hx-trigger="revealed" loads server content when an element scrolls into view, which is the basis of infinite scroll.

Mask: formatted input

x-mask formats what the user types. In a pattern, 9 matches a digit, a a letter, * any character; everything else is literal. x-mask:dynamic takes a JavaScript expression when the pattern depends on the value, and $money($input) formats currency.

Phone and date masks

A mask only formats; it does not validate. Dates like 99/99/9999 are accepted, and the server must still check the data.

Read-only: Persist, Focus, Anchor, Sort and Morph

These plugins need things the preview sandbox cannot offer (storage, focus management in a real page, or heavier setup), so here they are as read-only snippets.

HTML
<!-- Persist: survives reloads through localStorage -->
<div x-data="{ theme: $persist('light') }">
  <button @click="theme = theme === 'light' ? 'dark' : 'light'">Theme: <span x-text="theme"></span></button>
</div>

<!-- Focus: keep Tab inside the dialog while it is open -->
<div x-show="open" x-trap.noscroll="open">...</div>

<!-- Anchor: place a popup beside its button -->
<button x-ref="btn">Open</button>
<div x-show="open" x-anchor.bottom-start="$refs.btn">Popup</div>

<!-- Sort: drag and drop reordering -->
<ul x-sort="reorder"><li x-sort:item="1">One</li><li x-sort:item="2">Two</li></ul>

$persist stores any JSON-friendly value under a key derived from its name (change it with .as('key')). Morph (Alpine.morph(el, html)) updates an element to match new HTML while keeping Alpine state and focus, and it pairs with HTMX through an extension, which you will meet when the two libraries work together.

Three core features worth knowing

x-id: unique, stable ids

Accessible forms need an id that links a label to its input. When a component repeats, hard-coded ids collide. x-id="['name']" creates a scope in which $id('name') returns an id unique to that copy (name-1, name-2, ...).

x-ignore: keep Alpine out

Content inside an element with x-ignore is not initialized by Alpine, so it shows up literally. Use it for code samples that mention Alpine syntax, or for areas managed by another library.

Alpine.bind: reusable attribute sets

Alpine.bind('name', () => ({ ... })) stores a group of directives, and x-bind="name" applies them. It keeps repeated markup short.

x-id, x-ignore and Alpine.bind

The two email fields each get their own id, the button reuses one bundle of behavior, and the x-ignore block keeps (not evaluated) visible instead of showing 2.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Where do you put the script tag of an Alpine plugin from a CDN?

    Plugins must register before Alpine starts, so their deferred scripts come before the core script.
  2. 2. Which pair is correct for animating an accordion panel open?

    x-collapse animates the height of an element controlled by x-show.
  3. 3. What does x-mask="99/99/9999" guarantee?

    A mask only formats input. Validation still belongs to your code and the server.
  4. 4. When is x-id useful?

    x-id with $id() gives each copy of a component its own id, which prevents collisions in repeated markup.

Summary

  • Official plugins add focused features: persist, collapse, anchor, sort, mask, focus, intersect and morph.
  • Load each plugin script (with defer) before the Alpine core script; with a bundler call Alpine.plugin() before Alpine.start().
  • x-collapse animates height, x-intersect reacts to scrolling, and x-mask formats input without validating it.
  • x-id makes unique ids, x-ignore leaves a subtree alone, and Alpine.bind packages directives for reuse.

Alpine is now covered. Time to meet the other half: your first HTMX request.