alpine
A rugged, minimal framework for composing JavaScript behavior in your markup.
Top Related Projects
Completely unstyled, fully accessible UI components, designed to integrate beautifully with Tailwind CSS.
This is the repo for Vue 2. For Vue 3, go to https://github.com/vuejs/core
The library for web and native user interfaces.
Cybernetically enhanced web apps
⚛️ Fast 3kB React alternative with the same modern API. Components & Virtual DOM.
Quick Overview
Alpine.js is a lightweight JavaScript framework for adding interactivity to web pages. It offers a declarative approach to building dynamic interfaces, using HTML attributes to define behavior. Alpine.js aims to provide many of the benefits of larger frameworks like Vue or React, but with a smaller footprint and simpler learning curve.
Pros
- Minimal learning curve, easy to integrate into existing projects
- Small file size (< 15kB gzipped) for fast loading
- No build step required, works directly in the browser
- Familiar syntax for developers who know JavaScript and HTML
Cons
- Limited ecosystem compared to larger frameworks
- Not suitable for complex, large-scale applications
- Lack of official router or state management solutions
- Performance may suffer in highly dynamic applications
Code Examples
- Basic data binding and event handling:
<div x-data="{ count: 0 }">
<button x-on:click="count++">Increment</button>
<span x-text="count"></span>
</div>
- Conditional rendering:
<div x-data="{ isOpen: false }">
<button @click="isOpen = !isOpen">Toggle</button>
<div x-show="isOpen">
This content is shown when isOpen is true.
</div>
</div>
- List rendering:
<ul x-data="{ items: ['Apple', 'Banana', 'Cherry'] }">
<template x-for="item in items">
<li x-text="item"></li>
</template>
</ul>
Getting Started
To use Alpine.js, include the script in your HTML file:
<script defer src="https://unpkg.com/alpinejs@3.x.x/dist/cdn.min.js"></script>
Then, add x-data
attributes to elements where you want to use Alpine.js:
<div x-data="{ message: 'Hello, Alpine.js!' }">
<h1 x-text="message"></h1>
<input type="text" x-model="message">
</div>
This creates a simple data-bound input field that updates the heading in real-time as you type.
Competitor Comparisons
Completely unstyled, fully accessible UI components, designed to integrate beautifully with Tailwind CSS.
Pros of Headless UI
- Provides fully accessible, unstyled UI components
- Integrates seamlessly with Tailwind CSS
- Offers a wide range of pre-built components
Cons of Headless UI
- Limited to React and Vue frameworks
- Steeper learning curve for beginners
- Less flexibility for custom interactions
Code Comparison
Alpine:
<div x-data="{ open: false }">
<button @click="open = !open">Toggle</button>
<div x-show="open">Content</div>
</div>
Headless UI (React):
import { Disclosure } from '@headlessui/react'
function MyDisclosure() {
return (
<Disclosure>
<Disclosure.Button>Toggle</Disclosure.Button>
<Disclosure.Panel>Content</Disclosure.Panel>
</Disclosure>
)
}
Alpine is a lightweight JavaScript framework that allows for easy DOM manipulation and state management directly in HTML. It's framework-agnostic and has a gentle learning curve.
Headless UI, on the other hand, provides unstyled, accessible components for React and Vue. It's designed to work well with Tailwind CSS and offers more complex, pre-built components out of the box.
While Alpine excels in simplicity and flexibility, Headless UI shines in accessibility and integration with popular frameworks and Tailwind CSS. The choice between them depends on project requirements, team expertise, and desired level of control over component behavior.
This is the repo for Vue 2. For Vue 3, go to https://github.com/vuejs/core
Pros of Vue
- More comprehensive framework with advanced features like routing and state management
- Larger ecosystem with extensive libraries and community support
- Better suited for large-scale applications and complex user interfaces
Cons of Vue
- Steeper learning curve, especially for beginners
- Heavier bundle size, which may impact initial load times
- Requires more setup and configuration for projects
Code Comparison
Vue:
<template>
<div>{{ message }}</div>
</template>
<script>
export default {
data() {
return {
message: 'Hello Vue!'
}
}
}
</script>
Alpine:
<div x-data="{ message: 'Hello Alpine!' }">
<span x-text="message"></span>
</div>
Summary
Vue is a more feature-rich framework suitable for complex applications, while Alpine offers a lightweight and simple approach for adding interactivity to web pages. Vue provides a comprehensive ecosystem but requires more learning and setup, whereas Alpine focuses on simplicity and ease of use with a minimal footprint. The choice between the two depends on project requirements, team expertise, and desired level of complexity.
The library for web and native user interfaces.
Pros of React
- More powerful and feature-rich for complex applications
- Extensive ecosystem with a wide range of third-party libraries
- Virtual DOM for efficient rendering and better performance in large apps
Cons of React
- Steeper learning curve and more complex setup
- Larger bundle size, potentially impacting initial load times
- Requires additional tools and libraries for state management in larger apps
Code Comparison
React:
import React from 'react';
function Counter() {
const [count, setCount] = React.useState(0);
return <button onClick={() => setCount(count + 1)}>{count}</button>;
}
Alpine:
<div x-data="{ count: 0 }">
<button x-on:click="count++">
<span x-text="count"></span>
</button>
</div>
Summary
React offers more power and flexibility for complex applications, with a rich ecosystem and efficient rendering through its Virtual DOM. However, it comes with a steeper learning curve and larger bundle size. Alpine provides a simpler, lightweight alternative with a gentler learning curve, making it ideal for smaller projects or enhancing existing HTML. The code comparison demonstrates React's component-based approach versus Alpine's HTML-centric declarative style.
Cybernetically enhanced web apps
Pros of Svelte
- Offers a full-featured framework with routing, state management, and component architecture
- Compiles to highly optimized vanilla JavaScript, resulting in smaller bundle sizes
- Provides reactive declarations and statements for efficient updates
Cons of Svelte
- Steeper learning curve due to its unique syntax and compilation process
- Smaller ecosystem and community compared to more established frameworks
- Requires a build step, which can complicate the development process
Code Comparison
Alpine:
<div x-data="{ count: 0 }">
<button @click="count++">Increment</button>
<span x-text="count"></span>
</div>
Svelte:
<script>
let count = 0;
</script>
<button on:click={() => count++}>Increment</button>
<span>{count}</span>
Summary
Alpine is a lightweight JavaScript framework that enhances HTML with minimal JavaScript. It's easy to learn and integrate into existing projects. Svelte, on the other hand, is a more comprehensive framework that compiles to vanilla JavaScript, offering powerful features and optimized performance. While Alpine excels in simplicity and ease of use, Svelte provides a more robust solution for building complex applications with better performance at the cost of a steeper learning curve.
⚛️ Fast 3kB React alternative with the same modern API. Components & Virtual DOM.
Pros of Preact
- Smaller bundle size and faster performance for complex applications
- Full Virtual DOM implementation for efficient updates
- Extensive ecosystem with compatible React libraries
Cons of Preact
- Steeper learning curve for beginners
- Less suitable for simple DOM manipulations
- Requires build tools and compilation for optimal use
Code Comparison
Alpine:
<div x-data="{ open: false }">
<button @click="open = !open">Toggle</button>
<span x-show="open">Content</span>
</div>
Preact:
function Toggle() {
const [open, setOpen] = useState(false);
return (
<div>
<button onClick={() => setOpen(!open)}>Toggle</button>
{open && <span>Content</span>}
</div>
);
}
Summary
Alpine is ideal for adding interactivity to existing HTML with minimal setup, making it perfect for small to medium-sized projects. It has a gentler learning curve and doesn't require a build step.
Preact, on the other hand, is better suited for larger, more complex applications. It offers better performance at scale and compatibility with the React ecosystem, but comes with a steeper learning curve and requires more setup.
Choose Alpine for quick, lightweight interactivity in traditional web projects. Opt for Preact when building larger single-page applications or when you need the power of a full Virtual DOM implementation.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
Alpine.js
Go to the Alpine docs for most things: Alpine Docs
You are welcome to submit updates to the docs by submitting a PR to this repo. Docs are located in the /packages/docs
directory.
Stay here for contribution-related information.
Looking for V2 docs? here they are
Contribution Guide:
Quickstart
- clone this repo locally
- run
npm install
&npm run build
- Include the
/packages/alpinejs/dist/cdn.js
file from a<script>
tag on a webpage and you're good to go!
Brief Tour
You can get everything installed with: npm install
in the root directory of this repo after cloning it locally.
This repo is a "mono-repo" using npm workspaces for managing the packages. Each package has its own folder in the /packages
directory.
Rather than having to run separate builds for each package, all package bundles are handled with the same command: npm run build
Here's a brief look at each package in this repo:
Package | Description |
---|---|
alpinejs | The main Alpine repo with all of Alpine's core |
collapse | A plugin for expanding and collapsing elements using smooth animations |
csp | A repo to provide a "CSP safe" build of Alpine |
docs | The Alpine documentation |
focus | A plugin that allows you to manage focus inside an element |
history | A plugin for binding data to query string parameters using the history API (name is likely to change) |
intersect | A plugin for triggering JS expressions based on elements intersecting with the viewport |
mask | A plugin for automatically formatting a text input field as a user types |
morph | A plugin for morphing HTML (like morphdom) inside the page intelligently |
persist | A plugin for persisting Alpine state across page loads |
The compiled JS files (as a result of running npm run [build/watch]
) to be included as a <script>
tag for example are stored in each package's packages/[package]/dist
directory.
Each package should at least have: a "cdn" build that is self-initializing and can be included using the src
attribute in a <script defer>
tag, and a module.[esm/cjs].js
file that is used for importing as a JS module (cjs for node, esm for everything else).
The bundling for Alpine V3 is handled exclusively by ESBuild. All of the configuration for these builds is stored in the scripts/build.js
file.
Testing
There are 2 different testing tools used in this repo: Cypress (for integration tests), and Jest (for unit tests).
All tests are stored inside the /tests
folder under /tests/cypress
and /tests/jest
.
You can run them both from the command line using: npm run test
If you wish to only run cypress and open it's user interface (recommended during development), you can run: npm run cypress
If you wish to only run Jest tests, you can run npm run jest
like normal and target specific tests. You can specify command line config options to forward to the jest command with --
like so: npm run jest -- --watch
Top Related Projects
Completely unstyled, fully accessible UI components, designed to integrate beautifully with Tailwind CSS.
This is the repo for Vue 2. For Vue 3, go to https://github.com/vuejs/core
The library for web and native user interfaces.
Cybernetically enhanced web apps
⚛️ Fast 3kB React alternative with the same modern API. Components & Virtual DOM.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot