### 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
Count is greater than 5!
``` -------------------------------- ### Example: Creating a '$now' Magic Property Source: https://github.com/alpinejs/alpine/blob/main/packages/docs/src/en/advanced/extending.md Demonstrates how to create a simple magic property '$now' that returns the current time. ```APIDOC ## POST /alpine/magic/now ### Description Registers the `'$now'` magic property. When accessed in an Alpine.js template, it returns the current time formatted as a string. ### Method POST ### Endpoint `/alpine/magic/now` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body - **Alpine** (object) - Required - The Alpine global object. ### Request Example ```json { "Alpine": "Alpine" } ``` ### Response #### Success Response (200) - **time** (string) - The current time formatted as a string (e.g., "12:00:00 PM"). #### Response Example ```json { "time": "12:34:56 PM" } ``` ### Usage in Template ```html ``` ``` -------------------------------- ### Install and Initialize Alpine.js Sort Plugin via NPM Source: https://github.com/alpinejs/alpine/blob/main/packages/docs/src/en/plugins/sort.md Install the Sort plugin using NPM and then import and initialize it within your JavaScript bundle. This is the recommended approach for projects using a build system. ```shell npm install @alpinejs/sort ``` ```javascript import Alpine from 'alpinejs' import sort from '@alpinejs/sort' Alpine.plugin(sort) ... ``` -------------------------------- ### Install and Initialize Alpine.js CSP Build via NPM Source: https://github.com/alpinejs/alpine/blob/main/packages/docs/src/en/advanced/csp.md Install the CSP-friendly build of Alpine.js using NPM and initialize it within your JavaScript bundle. This method is suitable for projects using module bundlers. ```shell npm install @alpinejs/csp ``` ```javascript import Alpine from '@alpinejs/csp' window.Alpine = Alpine Alpine.start() ``` -------------------------------- ### Example CSP Header for Alpine.js Source: https://github.com/alpinejs/alpine/blob/main/packages/docs/src/en/advanced/csp.md Provides an example of a Content Security Policy (CSP) header that is compatible with Alpine.js's CSP build. The key is to remove 'unsafe-eval' from the script-src directive while allowing nonce-based scripts. ```http Content-Security-Policy: default-src 'self'; script-src 'nonce-[random]' 'strict-dynamic'; ``` -------------------------------- ### Install and Initialize Alpine.js Persist Plugin via NPM Source: https://github.com/alpinejs/alpine/blob/main/packages/docs/src/en/plugins/persist.md Install the Persist plugin using NPM and initialize it within your JavaScript bundle. This approach is for projects using a module bundler. ```shell npm install @alpinejs/persist ``` ```javascript import Alpine from 'alpinejs' import persist from '@alpinejs/persist' Alpine.plugin(persist) ... ``` -------------------------------- ### Install and Initialize Alpine.js Mask Plugin via NPM Source: https://github.com/alpinejs/alpine/blob/main/packages/docs/src/en/plugins/mask.md Install the Mask plugin using NPM and then import and initialize it within your JavaScript bundle. This approach is for projects using a module bundler. ```shell npm install @alpinejs/mask ``` ```javascript import Alpine from 'alpinejs' import mask from '@alpinejs/mask' Alpine.plugin(mask) ... ``` -------------------------------- ### Alpine.js Morph DOM Update and Logging Source: https://github.com/alpinejs/alpine/blob/main/morph.html The 'start' function initializes Alpine.js morph to update a DOM element based on provided HTML. It also sets up a logger to capture morphing details, including messages, source, and target elements, and displays them on the page. Dependencies include Alpine.js and basic DOM manipulation. ```javascript function start() { Alpine.morph.log((message, from, to) => { console.log(message, from, to) document.querySelector('#log-from').innerHTML = escape(from.outerHTML) document.querySelector('#log-to').innerHTML = escape(to.outerHTML) let li = document.createElement('li') li.textContent = message message && document.querySelector('#log-message').appendChild(li) }) Alpine.morph( document.querySelector('#before').firstElementChild, document.querySelector('#after').firstElementChild.outerHTML, { debug: true, key(el) { return el.dataset.key } } ) } ``` -------------------------------- ### Install and Initialize Alpine.js Intersect Plugin via NPM Source: https://github.com/alpinejs/alpine/blob/main/packages/docs/src/en/plugins/intersect.md Install the Intersect plugin using NPM and then import and initialize it within your JavaScript bundle before using Alpine.js. ```shell npm install @alpinejs/intersect ``` ```javascript import Alpine from 'alpinejs' import intersect from '@alpinejs/intersect' Alpine.plugin(intersect) ... ``` -------------------------------- ### Alpine.js Interactive Morph Example Source: https://github.com/alpinejs/alpine/blob/main/packages/docs/src/en/plugins/morph.md An interactive example showcasing the Morph plugin's functionality. It uses Alpine.js directives to control a slide counter and display an image, demonstrating state preservation. ```html
``` -------------------------------- ### Initialize Alpine.js Resize Plugin via NPM Source: https://github.com/alpinejs/alpine/blob/main/packages/docs/src/en/plugins/resize.md Install the Resize plugin using NPM and then import and register it with Alpine.js in your JavaScript bundle. This is the recommended approach for projects using a build system. ```shell npm install @alpinejs/resize ``` ```javascript import Alpine from 'alpinejs' import resize from '@alpinejs/resize' Alpine.plugin(resize) ... ``` -------------------------------- ### Fetch Data on Initialization with x-init in Alpine.js Source: https://github.com/alpinejs/alpine/blob/main/packages/docs/src/en/directives/init.md This example demonstrates using x-init to fetch JSON data from an endpoint and store it in the component's state (x-data) before further processing. It utilizes async/await for the fetch operation. ```alpine
...
``` -------------------------------- ### Alpine.js x-effect Example Source: https://github.com/alpinejs/alpine/blob/main/packages/docs/src/en/directives/effect.md Demonstrates the x-effect directive in Alpine.js. It logs the 'label' property to the console and re-logs it when the property changes. No external dependencies are required, only Alpine.js. ```alpine
``` -------------------------------- ### Install Alpine.js Focus Plugin via CDN Source: https://github.com/alpinejs/alpine/blob/main/packages/docs/src/en/plugins/focus.md Include the Focus plugin from a CDN before Alpine's core JS file. This method is suitable for quick integration without a build process. ```html ``` -------------------------------- ### Alpine.js: Initialize and Measure Performance Source: https://github.com/alpinejs/alpine/blob/main/benchmarks/init.html This snippet initializes Alpine.js and measures the performance of its initialization. It logs the time taken for Alpine to become ready after the DOM is loaded. No external dependencies are required beyond Alpine.js itself. ```javascript window.start = performance.now() document.addEventListener('alpine:initialized', () => { setTimeout(() => { console.log(performance.now() - window.start); }) }) ``` -------------------------------- ### Install Alpine.js Resize Plugin via CDN Source: https://github.com/alpinejs/alpine/blob/main/packages/docs/src/en/plugins/resize.md Include the Resize plugin from a CDN before Alpine's core JS file. This method is suitable for quick integration without a build process. ```html ``` -------------------------------- ### Control Focus with Arrow Keys using Alpine.js $focus Source: https://github.com/alpinejs/alpine/blob/main/packages/docs/src/en/plugins/focus.md This example demonstrates how to use the $focus.next() and $focus.previous() utilities to navigate focus between buttons using the left and right arrow keys. It requires no external dependencies beyond Alpine.js. ```html
``` -------------------------------- ### Install Alpine.js Collapse Plugin via CDN Source: https://github.com/alpinejs/alpine/blob/main/packages/docs/src/en/plugins/collapse.md Include the Collapse plugin from a CDN before Alpine's core JS file. This method is suitable for quick integration without a build process. ```html ``` -------------------------------- ### Install Alpine.js Sort Plugin via CDN Source: https://github.com/alpinejs/alpine/blob/main/packages/docs/src/en/plugins/sort.md Include the Sort plugin from a CDN before Alpine.js core. This method is suitable for quick integration without a build process. ```html ``` -------------------------------- ### Install Alpine.js CSP Build via CDN Source: https://github.com/alpinejs/alpine/blob/main/packages/docs/src/en/advanced/csp.md Include the CSP-friendly build of Alpine.js directly in your HTML using a script tag from a CDN. This is a straightforward way to enable CSP compliance for Alpine. ```html ``` -------------------------------- ### Using Alpine.js without Reactive Data Source: https://github.com/alpinejs/alpine/blob/main/packages/docs/src/en/essentials/state.md This example demonstrates how to leverage Alpine.js functionality, like event listeners, without defining reactive data by omitting an expression in the `x-data` attribute. ```html ``` -------------------------------- ### Install Alpine.js Persist Plugin via CDN Source: https://github.com/alpinejs/alpine/blob/main/packages/docs/src/en/plugins/persist.md Include the Persist plugin from a CDN before Alpine's core JS file. This method is suitable for quick integration without a build process. ```html ``` -------------------------------- ### Alpine.js Plugin Bundle Module Consumption Source: https://github.com/alpinejs/alpine/blob/main/packages/docs/src/en/advanced/extending.md Demonstrates how to consume an Alpine.js plugin packaged as an NPM module. It involves importing Alpine.js, the plugin, registering the plugin using `Alpine.plugin()`, and then starting Alpine. ```javascript import Alpine from 'alpinejs' import foo from 'foo' Alpine.plugin(foo) window.Alpine = Alpine window.Alpine.start() ``` -------------------------------- ### Keyboard Event Handling with Modifiers Source: https://github.com/alpinejs/alpine/blob/main/packages/docs/src/en/directives/on.md This example demonstrates listening for specific keyboard events using `x-on` modifiers. It shows how to trigger an action only when the 'Enter' key is pressed in an input field. Modifiers can be chained for more complex key combinations. ```html ``` -------------------------------- ### Dispatch and Listen for Custom Events in Alpine.js Source: https://github.com/alpinejs/alpine/blob/main/packages/docs/src/en/directives/on.md This Alpine.js example shows how to dispatch a custom DOM event from a button and listen for it on a parent element. It illustrates both the native dispatchEvent method and the more concise $dispatch helper. ```alpine
``` ```alpine
``` -------------------------------- ### Element Initialization with init() Method Source: https://github.com/alpinejs/alpine/blob/main/packages/docs/src/en/essentials/lifecycle.md Alpine automatically calls `init()` methods defined within data objects during element initialization. This allows for setup logic within data definitions. ```javascript Alpine.data('dropdown', () => ({ init() { // I get called before the element using this data initializes. } })) ``` -------------------------------- ### Displaying Reactive Data with x-text in Alpine.js Source: https://github.com/alpinejs/alpine/blob/main/packages/docs/src/en/start-here.md Demonstrates how to dynamically update the text content of an HTML element based on component state using the x-text directive in Alpine.js. It binds the element's text to a JavaScript expression. ```html ``` -------------------------------- ### Alpine.js x-data with Methods Source: https://github.com/alpinejs/alpine/blob/main/packages/docs/src/en/directives/data.md Shows how to define JavaScript methods within the x-data object to encapsulate component logic. The example includes a 'toggle' method to manipulate the 'open' state, accessed via @click. ```html
Content...
``` -------------------------------- ### Reusable Alpine.js Data with Alpine.data Source: https://github.com/alpinejs/alpine/blob/main/packages/docs/src/en/directives/data.md Demonstrates how to extract x-data logic into reusable components using `Alpine.data`. This example defines a 'dropdown' component with 'open' state and a 'toggle' method, then registers it for use in the HTML. ```html
Content...
``` -------------------------------- ### Shorthand Event Handling with @ Source: https://github.com/alpinejs/alpine/blob/main/packages/docs/src/en/directives/on.md This example shows the shorthand syntax for `x-on`, using the `@` symbol instead of `x-on:`. This achieves the same functionality as the full `x-on` directive but with less verbosity. It's important to have a parent element with `x-data` defined for this to work. ```html ``` -------------------------------- ### Alpine.js CSP Supported Features: Basic Operations Source: https://github.com/alpinejs/alpine/blob/main/packages/docs/src/en/advanced/csp.md Shows examples of basic arithmetic, comparison, conditional (ternary), and string concatenation operations supported by Alpine's CSP build. These operations can be freely used in your Alpine expressions. ```html
``` -------------------------------- ### Apply dynamic transition classes Source: https://github.com/alpinejs/alpine/blob/main/tests/cypress/manual-transition-test.html Use x-transition with dynamic classes for custom transition behavior. ```html x-transition with dynamic classes ``` -------------------------------- ### Computed Properties with Getters in Alpine.js Source: https://github.com/alpinejs/alpine/blob/main/packages/docs/src/en/start-here.md Demonstrates using JavaScript getters within an x-data object to create computed properties. This allows for dynamic data manipulation that automatically updates the template. It requires the 'get' keyword and uses 'this.' to reference properties within the x-data scope. ```javascript { items: ['foo', 'bar', 'baz'], get filteredItems() { return this.items.filter( i => i.startsWith(this.search) ) } } ``` -------------------------------- ### Apply basic transitions Source: https://github.com/alpinejs/alpine/blob/main/tests/cypress/manual-transition-test.html Use x-transition to handle element visibility toggling. ```html x-transition ``` -------------------------------- ### Looping Elements with x-for in Alpine.js Source: https://github.com/alpinejs/alpine/blob/main/packages/docs/src/en/start-here.md Shows how to iterate over an array (like 'filteredItems') using the x-for directive in Alpine.js. The directive is applied to a