Alpine.js

3.15.11 · active · verified Sun Apr 19

Alpine.js is a lightweight, declarative JavaScript framework designed for adding interactive behavior directly into HTML markup, often described as 'Tailwind for JavaScript'. It operates by directly attaching JavaScript behavior to DOM elements via `x-` attributes, avoiding the need for a build step, virtual DOM, or component compilation for many use cases. The current stable version is 3.15.11, with frequent patch and minor releases focusing on bug fixes, performance enhancements, and new directive modifiers. Key differentiators include its minimal bundle size, direct-in-HTML approach, and Vue-like reactivity for small, interactive components, making it ideal for augmenting server-rendered applications or for projects where a full-fledged SPA framework is overkill.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates importing Alpine.js, registering a global data component, defining a magic property, and starting Alpine programmatically. Shows how it maps to HTML usage.

import Alpine from 'alpinejs';

// Register a global data store or component for use with `x-data="myStore"`
Alpine.data('myCounter', () => ({
  count: 0,
  message: 'Hello Alpine!',
  init() {
    // Optional initialization logic
    console.log('myCounter component initialized');
  },
  increment() {
    this.count++;
  },
  decrement() {
    this.count--;
  }
}));

// Register a global magic property like $myMagic
Alpine.magic('myMagic', el => 'This is a magic string!');

// Start Alpine.js
Alpine.start();

// Typically, Alpine.js is then used in HTML like this:
/*
<div x-data="myCounter">
    <h1 x-text="message"></h1>
    <p>Count: <span x-text="count"></span></p>
    <button @click="increment">+</button>
    <button @click="decrement">-</button>
    <p x-text="$myMagic"></p>
</div>
*/

view raw JSON →