To use Alpine.js in Astro, run npx astro add alpinejs. The official @astrojs/alpinejs integration loads Alpine on every page, and you write x-data, x-show and @click attributes directly in .astro markup. There are no islands or client directives involved: Alpine scans the HTML Astro already rendered and adds behaviour to it, which makes it a good fit for menus, tabs, accordions and small toggles.
Key Takeaways
- Astro Alpine.js setup is one command; Alpine then works in any
.astrofile.- Alpine enhances server-rendered HTML, so content stays visible and indexable.
- Register plugins and global data in an
entrypointfile.- Alpine loads on every page; for one widget on one page, a plain
<script>may be lighter.
Install
# terminal
npx astro add alpinejs
This installs alpinejs and @astrojs/alpinejs and registers the integration:
// astro.config.mjs
import { defineConfig } from 'astro/config';
import alpinejs from '@astrojs/alpinejs';
export default defineConfig({
integrations: [alpinejs()],
});
Use it in a component
---
// src/components/Disclosure.astro
interface Props { title: string }
const { title } = Astro.props;
---
<div x-data="{ open: false }" class="disclosure">
<button type="button" @click="open = !open" :aria-expanded="open">
{title}
</button>
<div x-show="open" x-transition x-cloak>
<slot />
</div>
</div>
<style is:global>
[x-cloak] { display: none !important; }
</style>
Astro props like title are rendered on the server; Alpine handles the open state in the browser. x-cloak hides the panel until Alpine initialises, so it doesn’t flash open. Because x-cloak content is hidden without JavaScript, use it only for content that’s fine to hide; a native <details> element is the no-JavaScript alternative.
Watch out for one syntax clash. In .astro markup, curly braces are Astro expressions. Alpine attribute values are strings, so x-data="{ open: false }" is fine, but don’t write x-data={{ open: false }}, which Astro would evaluate as an object.
Passing server data to Alpine
Serialize data into the attribute with JSON.stringify:
---
// src/components/PlanPicker.astro
const plans = [
{ id: 'basic', name: 'Basic', price: 9 },
{ id: 'pro', name: 'Pro', price: 19 },
];
---
<div x-data={`{ plans: ${JSON.stringify(plans)}, selected: 'basic' }`}>
<template x-for="plan in plans" :key="plan.id">
<label>
<input type="radio" name="plan" :value="plan.id" x-model="selected" />
<span x-text="`${plan.name} ($${plan.price}/mo)`"></span>
</label>
</template>
</div>
For anything larger, render the HTML with Astro and use Alpine only for state. That keeps content in the page source.
Plugins and global data with an entrypoint
To register plugins such as @alpinejs/focus or @alpinejs/collapse, point the integration at an entrypoint:
// astro.config.mjs
alpinejs({ entrypoint: '/src/entrypoint' })
// src/entrypoint.ts
import type { Alpine } from 'alpinejs';
import focus from '@alpinejs/focus';
import collapse from '@alpinejs/collapse';
export default (Alpine: Alpine) => {
Alpine.plugin(focus);
Alpine.plugin(collapse);
Alpine.store('cart', { count: 0 });
};
Alpine.store gives you shared state between components on a page, the Alpine equivalent of a store shared between islands.
Alpine with view transitions
With <ClientRouter />, Alpine starts once and watches the DOM, so components on newly swapped pages generally initialise. Test interactive components after navigating, and if something doesn’t start, check our tip on re-running scripts after view transitions.
Alpine, plain scripts or a framework island
| Alpine.js | Plain <script> |
Framework island | |
|---|---|---|---|
| Where the logic lives | HTML attributes | A script per component | A component file |
| Loads on | Every page | Pages that use it | Pages that use it |
| Best for | Many small toggles | One-off behaviour | Complex, stateful UI |
If only a single page has interactivity, a plain script is smaller; see Astro script tags. If the UI has lots of state and rendering logic, a React, Svelte or Preact island is clearer. The config docs explain integrations in a Komal theme, and our Astro themes use plain scripts by default, so Alpine is an easy add if you prefer it.
FAQ
Does Alpine.js work with Astro?
Yes. The official @astrojs/alpinejs integration loads Alpine globally, and you write Alpine directives in .astro files. No client directive is needed.
Do I need client:load for Alpine components?
No. Alpine isn’t an island framework. It runs on the whole page and initialises any element with x-data.
Can I use Alpine and React in the same Astro project?
Yes. Alpine handles the markup in .astro files and React handles its islands. Avoid putting Alpine directives inside React components, because React controls that DOM.