mcpField
A
alpinejs

alpinejs

github.com/brettatomsDevelopment3256 installsUpdated 11w ago
Repository

About alpinejs

A best-practices guide for developers building with AlpineJS, covering directives, magic properties, and component organization. It emphasizes keeping HTML attributes short and moving complex logic into functions, reusable components, or global stores.

Key features

  1. 01AlpineJS directive reference
  2. 02Magic property explanations
  3. 03Guidance for extracting complex logic
  4. 04Reusable component and global store patterns
  5. 05Form validation and event modifier examples

Use cases

  1. 01Refactor AlpineJS pages with complex inline logic
  2. 02Reuse components across pages
  3. 03Manage shared state or form interactions

Install

Copy the command and run it in a terminal.

$npx skills add https://github.com/brettatoms/agent-skills --skill alpinejs

After installing, type/alpinejsin Claude Code to confirm it loaded.

Details

From the project README

AlpineJS Best Practices

Golden Rule: Keep Attributes Short

Never put complex logic in HTML attributes. If your x-data, x-init, or any directive exceeds ~50 characters, extract it.

Directive Cheatsheet

DirectivePurposeExample
x-dataDeclare reactive component statex-data="{ open: false }"
x-initRun code on component initx-init="fetchData()"
x-showToggle visibility (CSS display)x-show="open"
x-ifConditional rendering (must wrap <template>)<template x-if="show">
x-forLoop (must wrap <template>)<template x-for="item in items">
x-bind: / :Bind attribute to expression:class="{ active: isActive }"
x-on: / @Listen to events@click="open = !open"
x-modelTwo-way bind form inputsx-model="email"
x-textSet text contentx-text="message"
x-htmlSet inner HTMLx-html="htmlContent"
x-refReference element via $refsx-ref="input"
x-cloakHide until Alpine initializesx-cloak (add CSS: [x-cloak] { display: none; })
x-transitionApply enter/leave transitionsx-transition or x-transition.duration.300ms
x-effectRun reactive side effectsx-effect="console.log(count)"
x-ignoreSkip Alpine initializationx-ignore
x-teleportMove element to another locationx-teleport="#modals"
x-modelableExpose property for external bindingx-modelable="value"

Magic Properties

PropertyDescription
$elCurrent DOM element
$refsAccess elements with x-ref
$storeAccess global Alpine stores
$watchWatch a property for changes
$dispatchDispatch custom events
$nextTickRun after DOM updates
$rootRoot element of component
$dataAccess component data object
$idGenerate unique IDs

Patterns

❌ BAD: Long Inline JavaScript
<!-- DON'T DO THIS -->
<div x-data="{ items: [], loading: true, error: null, async fetchItems() { this.loading = true; try { const res = await fetch('/api/items'); this.items = await res.json(); } catch (e) { this.error = e.message; } finally { this.loading = false; } } }" x-init="fetchItems()">
✅ GOOD: Extract to Function
<script>
function itemList() {
  return {
    items: [],
    loading: true,
    error: null,
    
    async fetchItems() {
      this.loading = true;
      try {
        const res = await fetch('/api/items');
        this.items = await res.json();
      } catch (e) {
        this.error = e.message;
      } finally {
        this.loading = false;
      }
    }
  };
}
</script>

<div x-data="itemList()" x-init="fetchItems()">
  <!-- template -->
</div>
✅ GOOD: Simple Inline State
<!-- Simple state is fine inline -->
<div x-data="{ open: false, count: 0 }">
  <button @click="open = !open">Toggle</button>
  <div x-show="open" x-transition>Content</div>
</div>
✅ GOOD: Global Store for Shared State
<script>
document.addEventListener('alpine:init', () => {
  Alpine.store('cart', {
    items: [],
    add(item) { this.items.push(item); },
    get total() { return this.items.reduce((sum, i) => sum + i.price, 0); }
  });
});
</script>

<div x-data>
  <span x-text="$store.cart.total"></span>
</div>
✅ GOOD: Reusable Component with Alpine.data()
<script>
document.addEventListener('alpine:init', () => {
  Alpine.data('dropdown', () => ({
    open: false,
    toggle() { this.open = !this.open; },
    close() { this.open = false; }
  }));
});
</script>

<div x-data="dropdown" @click.outside="close()">
  <button @click="toggle()">Menu</button>
  <ul x-show="open" x-transition>
    <li>Item 1</li>
  </ul>
</div>
✅ GOOD: Form with Validation
<script>
function contactForm() {
  return {
    email: '',
    message: '',
    errors: {},
    
    validate() {
      this.errors = {};
      if (!this.email.includes('@')) this.errors.email = 'Invalid email';
      if (this.message.length < 10) this.errors.message = 'Too short';
      return Object.keys(this.errors).length === 0;
    },
    
    submit() {
      if (this.validate()) {
        // submit logic
      }
    }
  };
}
</script>

<form x-data="contactForm()" @submit.prevent="submit()">
  <input x-model="email" type="email">
  <span x-show="errors.email" x-text="errors.email" class="error"></span>
  
  <textarea x-model="message"></textarea>
  <span x-show="errors.message" x-text="errors.message" class="error"></span>
  
  <button type="submit">Send</button>
</form>
Event Modifiers
@click.prevent     <!-- preventDefault() -->
@click.stop        <!-- stopPropagation() -->
@click.outside     <!-- Click outside element -->
@click.window      <!-- Listen on window -->
@click.document    <!-- Listen on document -->
@click.once        <!-- Fire once -->
@click.debounce    <!-- Debounce (default 250ms) -->
@click.throttle    <!-- Throttle -->
@keydown.enter     <!-- Specific key -->
@keydown.escape    <!-- Escape key -->
Transition Modifiers
x-transition                           <!-- Default fade -->
x-transition.duration.300ms            <!-- Custom duration -->
x-transition.opacity                   <!-- Opacity only -->
x-transition.scale.90                  <!-- Scale from 90% -->
x-transition:enter.duration.500ms      <!-- Enter duration -->
x-transition:leave.duration.200ms      <!-- Leave duration -->

Quick Decision Guide

  1. State is 1-3 simple properties? → Inline x-data="{ open: false }"
  2. Has methods or complex logic? → Extract to function componentName() { return {...} }
  3. Reused across pages? → Use Alpine.data('name', () => ({...}))
  4. Shared global state? → Use Alpine.store('name', {...})
  5. Long attribute string? → You're doing it wrong. Extract it.

Comments

Markdown supported