### NanoStores Plugin Setup Source: https://context7.com/nanostores/alpine/llms.txt Configure the all-in-one Alpine.js plugin to register `x-nano`, `x-nano-model`, and `$nano` magics. This is the recommended setup. ```javascript import Alpine from 'alpinejs' import { NanoStores } from '@nanostores/alpine' import { atom, map } from 'nanostores' // Create your stores const $counter = atom(0) const $user = map({ name: 'Alice', role: 'user' }) // Expose stores as Alpine magics for use in HTML expressions Alpine.magic('counter', () => $counter) Alpine.magic('user', () => $user) // Register the full plugin Alpine.plugin(NanoStores) Alpine.start() ``` -------------------------------- ### Install Nano Stores Alpine Source: https://github.com/nanostores/alpine/blob/main/README.md Install the necessary packages for Nano Stores Alpine integration using npm. ```sh npm install nanostores alpinejs @nanostores/alpine ``` -------------------------------- ### Setup Nano Stores Alpine Plugin Source: https://github.com/nanostores/alpine/blob/main/README.md Set up the Nano Stores plugin for Alpine.js by importing and registering it. This enables Nano Stores directives and magic properties. ```js import Alpine from 'alpinejs' import { NanoStores } from '@nanostores/alpine' Alpine.plugin(NanoStores) Alpine.start() ``` -------------------------------- ### x-nano-model Store Setup Source: https://context7.com/nanostores/alpine/llms.txt Expose `atom` stores as Alpine magics to be used with the `x-nano-model` directive for two-way data binding. ```javascript import { atom } from 'nanostores' const $email = atom('') const $agreed = atom(false) const $color = atom('blue') Alpine.magic('email', () => $email) Alpine.magic('agreed', () => $agreed) Alpine.magic('color', () => $color) ``` -------------------------------- ### Computed Store Setup for x-nano Source: https://context7.com/nanostores/alpine/llms.txt Define computed stores and expose them as Alpine magics for use with the `x-nano` directive. ```javascript import { atom, computed } from 'nanostores' const $base = atom(10) const $doubled = computed($base, v => v * 2) Alpine.magic('base', () => $base) Alpine.magic('doubled', () => $doubled) ``` -------------------------------- ### Combine Store Reads with Mutations Source: https://github.com/nanostores/alpine/blob/main/README.md Combine reading store values with mutations directly within Alpine.js template expressions. This example shows incrementing a counter store using its `set` method. ```html
``` -------------------------------- ### Create Alpine Components with Nano Stores using withStores Source: https://context7.com/nanostores/alpine/llms.txt The `withStores` helper creates an Alpine.js `x-data` factory that subscribes to Nano Stores. Store values are injected into the component's initial state and kept in sync. Subscriptions are managed automatically on component init and destroy. ```javascript import Alpine from 'alpinejs' import { withStores } from '@nanostores/alpine/with-stores' import { atom, map } from 'nanostores' // Define stores const $clicks = atom(0) const $lastAction = atom('') const $cart = atom([]) const $profile = map({ name: 'Alice', role: 'user' }) // Dashboard component with multiple stores Alpine.data( 'dashboard', withStores({ total: $clicks, lastAction: $lastAction }, () => ({ click(label) { $clicks.set($clicks.get() + 1) $lastAction.set(`Button ${label}`) }, reset() { $clicks.set(0) $lastAction.set('') } })) ) // Shopping cart component with methods Alpine.data( 'shoppingCart', withStores({ cart: $cart, profile: $profile }, ({ cart, profile }) => ({ // Initial values available in factory initialCount: cart.length, get cartCount() { return this.cart.length }, get greeting() { return `Hi, ${this.profile.name}` }, addItem(item) { $cart.set([...this.cart, item]) }, clearCart() { $cart.set([]) }, // Lifecycle hooks are preserved init() { console.log('Cart initialized with', this.cart.length, 'items') this.$watch('cart', (val) => { console.log('Cart changed:', val) }) }, destroy() { console.log('Cart component destroyed') } })) ) Alpine.start() ``` -------------------------------- ### Usage of Alpine Components Created with withStores Source: https://context7.com/nanostores/alpine/llms.txt Demonstrates how to use Alpine.js components defined with the `withStores` helper. The component's state, including store values and methods, is directly available within the `x-data` directive. ```html

Total clicks:

Last action:

``` -------------------------------- ### Register Individual Nano Stores Plugins Source: https://github.com/nanostores/alpine/blob/main/README.md Alternatively, register individual Nano Stores plugins for directives (x-nano, x-nano-model) or magic properties ($nano) separately. ```js import { directivePlugin, magicPlugin } from '@nanostores/alpine' // Registers x-nano and x-nano-model directives Alpine.plugin(directivePlugin) // Registers $nano magic property Alpine.plugin(magicPlugin) ``` -------------------------------- ### Inject Stores as Typed Initial State Source: https://github.com/nanostores/alpine/blob/main/README.md Use the `withStores` helper to inject Nano Stores as typed initial state into an Alpine.js component. This keeps store logic out of the template and provides reactive properties and methods. ```js import Alpine from 'alpinejs' import { withStores } from '@nanostores/alpine/with-stores' import { $cart, $profile } from './stores.js' Alpine.data( 'header', withStores({ cart: $cart, profile: $profile }, ({ cart, profile }) => ({ get cartCount() { return this.cart.length }, get greeting() { return `Hi, ${this.profile.name}` }, clearCart() { $cart.set([]) } })) ) ``` -------------------------------- ### x-nano Directive Usage Source: https://context7.com/nanostores/alpine/llms.txt Use the `x-nano` directive to bind Nano Stores to Alpine.js scopes. Subscriptions are automatically cleaned up. ```html

Count:

``` ```html

Name:

Role:

``` ```html
``` ```html
``` -------------------------------- ### Selective Registration of Alpine.js Plugins Source: https://context7.com/nanostores/alpine/llms.txt Register individual Nano Stores plugins for Alpine.js to optimize bundle size. Choose to register only the directive, the magic property, or the component helper as needed. ```javascript import Alpine from 'alpinejs' import { directivePlugin, modelDirectivePlugin, magicPlugin } from '@nanostores/alpine' import { withStores } from '@nanostores/alpine/with-stores' // Register only x-nano directive (378 B) Alpine.plugin(directivePlugin) // Register only x-nano-model directive Alpine.plugin(modelDirectivePlugin) // Register only $nano magic property (448 B) Alpine.plugin(magicPlugin) // Or combine specific plugins Alpine.plugin(directivePlugin) Alpine.plugin(magicPlugin) Alpine.start() ``` -------------------------------- ### Bind Multiple Stores to One Element Source: https://github.com/nanostores/alpine/blob/main/README.md Bind multiple Nano Stores to a single Alpine.js element using multiple `x-nano` directives. Each store is assigned to a distinct scope property. ```html
``` ```html
``` -------------------------------- ### Two-Way Binding with x-nano-model Source: https://github.com/nanostores/alpine/blob/main/README.md Use the `x-nano-model` directive for two-way binding between a form input and an atom store. Changes in the input update the store, and store changes update the input. ```html

``` -------------------------------- ### Alpine Component Using Injected Stores Source: https://github.com/nanostores/alpine/blob/main/README.md An Alpine.js component template that utilizes the properties and methods defined by the `withStores` helper, such as `greeting`, `cartCount`, and `clearCart`. ```html
``` -------------------------------- ### Bind Reactive Store to Element Scope Source: https://github.com/nanostores/alpine/blob/main/README.md Use the `x-nano` directive to bind a Nano Store to a named scope property on an Alpine.js element. This makes the store reactive and accessible to child expressions. ```html
Hi, !
``` -------------------------------- ### Inline Store Access with Magic Property Source: https://github.com/nanostores/alpine/blob/main/README.md Access Nano Store values inline within any Alpine.js expression using the `$nano()` magic property. This is useful for attribute bindings and one-off reads without `x-data`. ```html Cart is not empty ``` -------------------------------- ### Access Nano Store Values with $nano Magic Property Source: https://context7.com/nanostores/alpine/llms.txt Use the `$nano` magic property to access and display current store values directly within your Alpine.js templates. It automatically subscribes to changes, triggering reactive updates. Useful for attribute bindings and one-off reads. ```html ``` ```html ``` ```html

Cart is empty.

``` ```html ``` ```html
``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.