AlpineJS
Overview
We use Alpine.js to implement dynamic features in the front end of the site. It’s a lightweight declarative solution that simplifies workload and removes the need to handcode Javascript for a number of UI elements.
AlpineJS Introduction
Alpine.js is a rugged, minimal framework for composing JavaScript behavior in your markup. It offers a declarative way to add JavaScript functionality to your HTML with minimal overhead. Alpine.js provides you with an approachable API that’s similar to Vue.js or Angular, but with a much smaller footprint.
For complete documentation, visit the official Alpine.js website.
Usage Examples
Header Navigation
Overview
Alpine.js is used in the header for managing interactive UI elements, particularly the mobile navigation menu and account icons.
Key Implementations
1. Initial Setup
Alpine.js styles are included in the head to prevent FOUC (Flash of Unstyled Content):
<!-- Prevent FOUC -->
<style>
[x-cloak] { display: none !important; }
</style>
2. Mobile Menu State Management
The main page wrapper uses x-data to initialize the mobile menu state:
<div id="page" x-data="{ isMobileMenuOpen: false }">
This creates a boolean state variable that tracks if the mobile menu is open/closed.
3. Mobile Menu Toggle
The hamburger button uses @click to toggle the menu state:
<button @click="isMobileMenuOpen = !isMobileMenuOpen">
SVG icons use x-show to conditionally display open/close states.
4. Mobile Menu Transitions
The mobile navigation wrapper uses Alpine.js transitions:
x-show="isMobileMenuOpen"
- Controls visibilityx-transition
directives manage smooth enter/leave animations- Duration and timing settings provide a polished UX
5. Content Display
The main content area uses x-show to hide when mobile menu is open:
<div id="content" x-show="!isMobileMenuOpen">
Benefits
- Lightweight solution for interactive UI
- No jQuery dependency
- Declarative syntax in HTML
- Smooth transitions and animations
- Simple state management
Login Implementation
Overview
The login form implements Alpine.js for state management, form validation, and transitions.
Implementation Example
State Management
<div x-data="{
isLoggedIn: false,
email: '',
errors: []
}">
Form Validation
<form
@submit.prevent="validateForm()"
x-show="!isLoggedIn"
x-transition:enter="transition ease-out duration-300"
x-transition:enter-start="opacity-0"
x-transition:enter-end="opacity-100">
<input
type="email"
x-model="email"
@input="validateEmail($event.target.value)"
/>
</form>
Key Features
- Form State
- Manages login status
- Tracks form input values
- Handles validation errors
- Transitions
- Smooth form appearance/disappearance
- Loading state indicators
- Error message animations
- Validation
- Real-time email validation
- Password strength checking
- Error message display
- Benefits
- No page reload required
- Immediate user feedback
- Enhanced UX/UI
Premium Upgrade Implementation
Overview
The premium upgrade section in the TSJ subscription flow demonstrates conditional rendering and form field binding based on subscription type selection.
Implementation Details
<!-- Base subscription selector -->
<div x-data="{base: ''}">
<input type="radio" x-model="base" />
<!-- Premium upgrade section -->
<div class="premium-upgrade" x-show="base == 'annually'">
<input type="checkbox"
x-bind:disabled="base != 'annually'" />
</div>
</div>
Key Features
- x-show directive - Shows premium option only for annual subscriptions
- x-bind:disabled - Disables checkbox unless annual plan selected
- Reactive state management through x-model
- Parent-child state sharing
Benefits
- Clean conditional UI logic
- Automatic form validation
- Improved user experience
- Clear business rule implementation