--- name: alpinejs description: AlpineJS best practices and patterns. Use when writing HTML with Alpine.js directives to avoid common mistakes like long inline JavaScript strings. --- # 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 | Directive | Purpose | Example | |-----------|---------|---------| | `x-data` | Declare reactive component state | `x-data="{ open: false }"` | | `x-init` | Run code on component init | `x-init="fetchData()"` | | `x-show` | Toggle visibility (CSS display) | `x-show="open"` | | `x-if` | Conditional rendering (must wrap `