### Install and Import Alpine.js as a Module Source: https://github.com/alpinejs/alpine/blob/main/packages/docs/src/en/essentials/installation.md Install Alpine.js using NPM and import it into your JavaScript bundle. Initialize Alpine by calling Alpine.start(). Register extensions between import and start. Call Alpine.start() only once. ```shell npm install alpinejs ``` ```javascript import Alpine from 'alpinejs' window.Alpine = Alpine Alpine.start() ``` -------------------------------- ### Install Anchor via NPM Source: https://github.com/alpinejs/alpine/blob/main/packages/docs/src/en/plugins/anchor.md Install the package and initialize the plugin within your JavaScript bundle. ```shell npm install @alpinejs/anchor ``` ```js import Alpine from 'alpinejs' import anchor from '@alpinejs/anchor' Alpine.plugin(anchor) ... ``` -------------------------------- ### Alpine.js V3 Initialization: Importing and Starting Source: https://github.com/alpinejs/alpine/blob/main/packages/docs/src/en/upgrade-guide.md Shows the required change in how Alpine.js is imported and started when using it as a module in V3. Unlike V2, V3 requires an explicit `Alpine.start()` call after importing. ```javascript // 🚫 Before import 'alpinejs' // ✅ After import Alpine from 'alpinejs' window.Alpine = Alpine Alpine.start() ``` -------------------------------- ### Install Anchor via CDN Source: https://github.com/alpinejs/alpine/blob/main/packages/docs/src/en/plugins/anchor.md Include the plugin script tag before the Alpine core script tag. ```alpine ``` -------------------------------- ### Install and Initialize Alpine.js Focus Plugin via NPM Source: https://github.com/alpinejs/alpine/blob/main/packages/docs/src/en/plugins/focus.md Install the Focus plugin using NPM and import it into your JavaScript bundle. This approach is recommended for projects using a module bundler. ```shell npm install @alpinejs/focus ``` ```javascript import Alpine from 'alpinejs' import focus from '@alpinejs/focus' Alpine.plugin(focus) ... ``` -------------------------------- ### Listening for Click Events with x-on in Alpine.js Source: https://github.com/alpinejs/alpine/blob/main/packages/docs/src/en/start-here.md Shows how to attach event listeners to HTML elements using the x-on directive in Alpine.js. This example listens for a 'click' event to trigger an action that modifies component state. ```html ``` -------------------------------- ### Install and Initialize Alpine.js Collapse Plugin via NPM Source: https://github.com/alpinejs/alpine/blob/main/packages/docs/src/en/plugins/collapse.md Install the Collapse plugin using NPM and import it into your JavaScript bundle. This approach is recommended for projects using a module bundler. ```shell npm install @alpinejs/collapse ``` ```javascript import Alpine from 'alpinejs' import collapse from '@alpinejs/collapse' Alpine.plugin(collapse) ... ``` -------------------------------- ### Alpine.js Morph Plugin NPM Installation and Initialization Source: https://github.com/alpinejs/alpine/blob/main/packages/docs/src/en/plugins/morph.md Installs and initializes the Alpine.js Morph plugin using NPM. This involves importing Alpine and the morph plugin, then registering the plugin with Alpine. ```shell npm install @alpinejs/morph ``` ```javascript import Alpine from 'alpinejs' import morph from '@alpinejs/morph' window.Alpine = Alpine Alpine.plugin(morph) ... ``` -------------------------------- ### Example: Creating a '$clipboard' Magic Function Source: https://github.com/alpinejs/alpine/blob/main/packages/docs/src/en/advanced/extending.md Illustrates how to create a magic function '$clipboard' that copies text to the user's clipboard. ```APIDOC ## POST /alpine/magic/clipboard ### Description Registers the `'$clipboard'` magic function. This function accepts a string argument and writes it to the user's clipboard using the `navigator.clipboard.writeText` API. ### Method POST ### Endpoint `/alpine/magic/clipboard` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body - **subject** (string) - Required - The text to be copied to the clipboard. ### Request Example ```json { "subject": "hello world" } ``` ### Response #### Success Response (200) - **message** (string) - Confirmation message indicating the text was copied. #### Response Example ```json { "message": "Text copied to clipboard." } ``` ### Usage in Template ```html ``` ``` -------------------------------- ### Basic Alpine.js HTML Structure Source: https://github.com/alpinejs/alpine/blob/main/packages/docs/src/en/start-here.md A minimal HTML file setup to include Alpine.js from a CDN and display a simple message. This serves as the entry point for using Alpine.js in a web project. ```html
``` -------------------------------- ### Basic Alpine.js CSP Example Source: https://github.com/alpinejs/alpine/blob/main/packages/docs/src/en/advanced/csp.md Demonstrates a functional counter component using Alpine's CSP build, showcasing that most inline expressions work similarly to the standard build. This example includes basic CSP headers for context. ```html