Alpine.js Frontend Website Development
Alpine.js is a minimalist framework for adding reactivity straight in HTML markup. No build process, no virtual DOM, no component trees. x-data, x-bind, x-on attributes — and you have working interface. This is conscious choice for projects where jQuery is already excessive but React is obvious overkill.
Typical candidates: Laravel Blade + Alpine.js, PHP sites with server rendering, WordPress with custom blocks, static sites with minor interactivity.
What Alpine.js can do out of the box
Directives cover 90% of typical interface tasks:
| Directive | Purpose |
|---|---|
x-data |
Define component reactive state |
x-bind / : |
Bind attributes |
x-on / @ |
Handle events |
x-show |
Manage visibility (display) |
x-if |
Conditional render (mount/unmount) |
x-for |
Iterate array |
x-model |
Two-way binding for forms |
x-transition |
Entry/exit animations |
x-ref |
DOM element access |
$store |
Global store |
$fetch |
Built-in fetch with reactivity (via @alpinejs/morph) |
Architecture without build
For small projects Alpine.js connects via CDN:
<script defer src="https://cdn.jsdelivr.net/npm/[email protected]/dist/cdn.min.js"></script>
For production projects with Vite:
// vite.config.js
import { defineConfig } from 'vite';
export default defineConfig({
build: { rollupOptions: { input: 'resources/js/app.js' } }
});
// app.js
import Alpine from 'alpinejs';
import focus from '@alpinejs/focus';
import persist from '@alpinejs/persist';
Alpine.plugin(focus);
Alpine.plugin(persist);
window.Alpine = Alpine;
Alpine.start();
Example: modal window with animation
<div x-data="{ open: false }">
<button @click="open = true">Open</button>
<div
x-show="open"
x-transition:enter="transition ease-out duration-200"
x-transition:enter-start="opacity-0 scale-95"
x-transition:enter-end="opacity-100 scale-100"
x-transition:leave="transition ease-in duration-150"
x-transition:leave-start="opacity-100 scale-100"
x-transition:leave-end="opacity-0 scale-95"
@click.outside="open = false"
@keydown.escape.window="open = false"
class="fixed inset-0 flex items-center justify-center"
>
<div class="bg-white rounded-xl p-6 shadow-xl w-full max-w-md">
<h2 class="text-lg font-semibold">Title</h2>
<button @click="open = false">Close</button>
</div>
</div>
</div>
Global state via $store
Alpine.store('cart', {
items: Alpine.$persist([]).as('cart_items'),
get count() { return this.items.length; },
get total() { return this.items.reduce((s, i) => s + i.price * i.qty, 0); },
add(product) {
const existing = this.items.find(i => i.id === product.id);
if (existing) existing.qty++;
else this.items.push({ ...product, qty: 1 });
},
remove(id) {
this.items = this.items.filter(i => i.id !== id);
}
});
In template: <span x-text="$store.cart.count"></span> — cart updates everywhere automatically. @alpinejs/persist saves data to localStorage between sessions.
Integration with server render
Alpine.js fits perfectly with Laravel Blade:
{{-- resources/views/components/dropdown.blade.php --}}
<div x-data="dropdown()" class="relative">
<button @click="toggle" :aria-expanded="open">
{{ $label }}
<svg :class="{ 'rotate-180': open }" .../>
</button>
<ul x-show="open" @click.outside="close" x-transition>
@foreach($items as $item)
<li><a href="{{ $item['url'] }}">{{ $item['label'] }}</a></li>
@endforeach
</ul>
</div>
<script>
function dropdown() {
return {
open: false,
toggle() { this.open = !this.open; },
close() { this.open = false; }
}
}
</script>
Working with AJAX and HTMX patterns
Alpine.js has no built-in HTTP client but integrates with fetch natively:
<div
x-data="{
results: [],
query: '',
loading: false,
async search() {
if (this.query.length < 2) return;
this.loading = true;
const res = await fetch(`/api/search?q=${encodeURIComponent(this.query)}`);
this.results = await res.json();
this.loading = false;
}
}"
>
<input
x-model.debounce.300ms="query"
@input="search"
placeholder="Search..."
/>
<div x-show="loading">Loading...</div>
<ul>
<template x-for="item in results" :key="item.id">
<li x-text="item.title"></li>
</template>
</ul>
</div>
Timeline and work composition
- Week 1: build setup (Vite/CDN), backend template integration, basic interactive components
-
Weeks 2–3: forms with validation, cart/favorites via
$store, animations, AJAX requests - Week 4: optimization, cross-browser testing, component documentation
Resulting JS bundle for average site — 15–30 KB (Alpine core + plugins). Compare to 150+ KB for React app with same functionality.







