Back to list
chandima

alpine

by chandima

0🍴 0📅 Jan 21, 2026

SKILL.md


name: alpine description: Alpine.js patterns for lightweight reactivity including directives, magics, plugins, and state management. Use when adding interactivity without heavy frameworks.

Alpine.js

Lightweight reactivity for server-first applications. 15KB, handles 95% of interactive needs.

Core Philosophy

  • Use Alpine.js for simple interactivity (toggles, forms, filtering)
  • Reserve React/Vue Islands for complex pre-built components
  • Keep state close to the DOM
  • Extract complex logic to Alpine.data()

Essential Directives

x-data (State)

<div x-data="{ open: false, count: 0 }">
  <button @click="open = !open">Toggle</button>
  <div x-show="open">Content</div>
</div>

x-show / x-if (Conditional)

<!-- x-show: toggles display, stays in DOM -->
<div x-show="visible" x-transition>Content</div>

<!-- x-if: adds/removes from DOM -->
<template x-if="loggedIn">
  <nav>User navigation</nav>
</template>

x-for (Loops)

<template x-for="item in items" :key="item.id">
  <div x-text="item.name"></div>
</template>

x-model (Two-way Binding)

<input x-model="search" type="text">
<input x-model.debounce.300ms="query">
<input x-model.number="quantity">

x-on / @ (Events)

<button @click="handleClick">Click</button>
<form @submit.prevent="save">...</form>
<input @keydown.enter="search">
<div @click.outside="open = false">...</div>

x-bind / : (Attributes)

<div :class="{ active: isActive }">
<button :disabled="loading">Submit</button>
<img :src="imageUrl">

Magics

<!-- $el: current element -->
<button @click="$el.classList.toggle('active')">

<!-- $refs: element references -->
<input x-ref="search">
<button @click="$refs.search.focus()">

<!-- $dispatch: custom events -->
<button @click="$dispatch('notify', { message: 'Hello' })">

<!-- $store: global state -->
<span x-text="$store.user.name">

<!-- $nextTick: after DOM update -->
<button @click="open = true; $nextTick(() => $refs.input.focus())">

Plugins

Focus (Modal trapping)

<div x-show="open" x-trap.inert="open">
  <!-- Focus trapped inside -->
</div>

Collapse (Height animations)

<div x-show="open" x-collapse>
  Smooth height animation
</div>

Persist (LocalStorage)

<div x-data="{ theme: $persist('light') }">

Intersect (Visibility)

<div x-intersect="visible = true">
  Lazy load when visible
</div>

Reusable Components

<script>
  Alpine.data('dropdown', () => ({
    open: false,
    toggle() { this.open = !this.open },
    close() { this.open = false }
  }));
</script>

<div x-data="dropdown">
  <button @click="toggle">Menu</button>
  <div x-show="open" @click.outside="close">
    Content
  </div>
</div>

Global State

<script>
  Alpine.store('cart', {
    items: [],
    get total() {
      return this.items.reduce((sum, i) => sum + i.price, 0);
    },
    add(product) {
      this.items.push(product);
    }
  });
</script>

<span x-text="$store.cart.total"></span>

Astro Integration

---
const products = await getProducts();
---

<!-- Method 1: JSON in x-data -->
<div x-data={`{ products: ${JSON.stringify(products)} }`}>
  ...
</div>

<!-- Method 2: define:vars for stores -->
<script define:vars={{ products }}>
  Alpine.store('products', products);
</script>

Anti-Patterns

<!-- Bad: Too much inline logic -->
<div x-data="{ /* 50 lines */ }">

<!-- Good: Extract to Alpine.data() -->
<div x-data="myComponent">

<!-- Bad: Missing :key in loops -->
<template x-for="item in items">

<!-- Good: Always use :key -->
<template x-for="item in items" :key="item.id">

<!-- Bad: x-html with user input (XSS) -->
<div x-html="userContent">

<!-- Good: Use x-text -->
<div x-text="userContent">

When to Use Islands Instead

  • Rich text editors
  • Complex drag-and-drop
  • Data visualization (charts)
  • Pre-built React/Vue components
  • Heavy state management

References

See .apm/instructions/alpinejs.instructions.md for complete patterns including:

  • All directive modifiers
  • Plugin configuration
  • Advanced patterns

Score

Total Score

45/100

Based on repository quality metrics

SKILL.md

SKILL.mdファイルが含まれている

+20
LICENSE

ライセンスが設定されている

0/10
説明文

100文字以上の説明がある

0/10
人気

GitHub Stars 100以上

0/15
最近の活動

3ヶ月以内に更新がある

0/10
フォーク

10回以上フォークされている

0/5
Issue管理

オープンIssueが50未満

+5
言語

プログラミング言語が設定されている

0/5
タグ

1つ以上のタグが設定されている

0/5

Reviews

💬

Reviews coming soon