Skip to content

Latest commit

 

History

History
171 lines (135 loc) · 4.87 KB

alpinejs.mdx

File metadata and controls

171 lines (135 loc) · 4.87 KB
type title description githubIntegrationURL category i18nReady
integration
@astrojs/alpinejs
Learn how to use the @astrojs/alpinejs framework integration to extend component support in your Astro project.
renderer
true

import PackageManagerTabs from '/components/tabs/PackageManagerTabs.astro' import Card from '/components/Landing/Card.astro' import Grid from '~/components/FluidGrid.astro'

This Astro integration adds Alpine.js to your project so that you can use Alpine.js anywhere on your page.

Installation

Astro includes an astro add command to automate the setup of official integrations. If you prefer, you can install integrations manually instead.

To install @astrojs/alpinejs, run the following from your project directory and follow the prompts:

```sh npx astro add alpinejs ``` ```sh pnpm astro add alpinejs ``` ```sh yarn astro add alpinejs ```

If you run into any issues, feel free to report them to us on GitHub and try the manual installation steps below.

Manual Install

First, install the @astrojs/alpinejs package.

```sh npm install @astrojs/alpinejs ``` ```sh pnpm add @astrojs/alpinejs ``` ```sh yarn add @astrojs/alpinejs ```

Most package managers will install associated peer dependencies as well. However, if you see a "Cannot find package 'alpinejs'" (or similar) warning when you start up Astro, you'll need to manually install Alpine.js yourself:

```sh npm install alpinejs @types/alpinejs ``` ```sh pnpm add alpinejs @types/alpinejs ``` ```sh yarn add alpinejs @types/alpinejs ```

Then, apply the integration to your astro.config.* file using the integrations property:

import { defineConfig } from 'astro/config';
import alpine from '@astrojs/alpinejs';

export default defineConfig({
  // ...
  integrations: [alpine()],
});

Configuration Options

entrypoint

You can extend Alpine by setting the entrypoint option to a root-relative import specifier (e.g. entrypoint: "/src/entrypoint").

The default export of this file should be a function that accepts an Alpine instance prior to starting. This allows the use of custom directives, plugins and other customizations for advanced use cases.

import { defineConfig } from 'astro/config';
import alpine from '@astrojs/alpinejs';

export default defineConfig({
  // ...
  integrations: [alpine({ entrypoint: '/src/entrypoint' })],
});
import type { Alpine } from 'alpinejs'
import intersect from '@alpinejs/intersect'

export default (Alpine: Alpine) => {
    Alpine.plugin(intersect)
}

Usage

Once the integration is installed, you can use Alpine.js directives and syntax inside any Astro component. The Alpine.js script is automatically added and enabled on every page of your website. Add plugin scripts to the page <head>.

The following example adds Alpine's Collapse plugin to expand and collapse paragraph text:

---
---
<html>
	<head>
		<!-- ... -->
		<script defer src="https://cdn.jsdelivr.net/npm/@alpinejs/collapse@3.x.x/dist/cdn.min.js"></script>
	</head>
	<body>
    <!-- ... -->
		<div x-data="{ expanded: false }">
			<button @click="expanded = ! expanded">Toggle Content</button>

			<p id="foo" x-show="expanded" x-collapse>
        Lorem ipsum
			</p>
		</div>
	</body>
</html>

Intellisense for TypeScript

The @astrojs/alpine integration adds Alpine to the global window object. For IDE autocompletion, add the following to your src/env.d.ts:

interface Window {
  Alpine: import('alpinejs').Alpine;
}

Examples

Themes