
Alpine.js replaces Knockout.js and jQuery in Hyvä. It lets developers add behaviour directly in templates with a handful of attributes, which keeps interactive code close to the markup it controls and avoids a separate JavaScript application.
This guide explains the directives you will use most, the component pattern Hyvä follows, how components share data, what Content Security Policy changes, and how to keep interactivity fast.
Why Alpine suits Magento
Most storefront interactivity is modest: open a menu, switch a gallery image, choose a product option, update the mini cart. A full framework is excessive for this. Alpine provides reactive state and event handling in a small library, and it works happily with HTML rendered by PHP.
Because Alpine reads its instructions from attributes, a backend developer can read a template and understand what it does without opening separate JavaScript files.
The essential directives
Most Hyvä components use only a few directives:
| Directive | Purpose |
|---|---|
x-data | Declares a component and its state |
x-show / x-if | Shows, hides or removes elements |
@click (x-on:click) | Responds to events |
:class (x-bind) | Links state to attributes |
x-model | Two-way binding for form fields |
x-text | Sets an element's text from state |
x-transition | Adds simple enter and leave animations |
A simple toggle, such as a filter panel, needs only a few lines:
<div x-data="{ open: false }">
<button @click="open = !open" :aria-expanded="open">Filters</button>
<div x-show="open" x-transition>...</div>
</div>
The Hyvä component pattern
For anything beyond a toggle, Hyvä templates define a function in a script block and pass it to x-data. The function returns an object holding state and methods:
<script>
function initQuantity() {
return {
qty: 1,
increase() { this.qty++ },
decrease() { if (this.qty > 1) this.qty-- }
}
}
</script>
<div x-data="initQuantity()">
<button @click="decrease">−</button>
<span x-text="qty"></span>
<button @click="increase">+</button>
</div>
This keeps logic readable and lets PHP inject values, such as translated labels, prices or configuration, safely through the template using Magento's escaper.
Sharing data between components
Components do not reach into one another directly. They communicate through browser events. Alpine's $dispatch sends a custom event; another component listens with @event-name.window.
Customer-specific data, such as the cart contents and login state, arrives through Magento's private content sections, because full-page cached HTML cannot contain it. Hyvä fires a private-content-loaded event when that data is ready, and components such as the mini cart and header account link listen for it.
<div x-data="{ cart: {} }"
@private-content-loaded.window="cart = $event.detail.data.cart">
<span x-text="cart.summary_count || 0"></span>
</div>
Deferring components with x-defer
Since release 1.3.7, Hyvä includes a custom x-defer directive that postpones initialising a component. It accepts several values:
x-defer="intersect"waits until the component enters the viewport. This is the most common choice.x-defer="interact"waits for the first user interaction with the page.x-defer="idle"waits until the browser is idle, with a configurable timeout that defaults to four seconds.x-defer="event:name"waits for a named event, for example opening the cart drawer.
The default theme already uses x-defer in suitable templates. Be selective in your own work: deferring every component can increase blocking time rather than reduce it, so test the effect on each page type.
Content Security Policy
Magento is moving towards stricter Content Security Policy rules, particularly on payment pages, where PCI DSS 4.0 requirements apply. A separate CSP-compatible variant of the theme uses Alpine's CSP build, which cannot evaluate inline expressions.
In that variant, components are registered with Alpine.data() and templates refer to properties and methods by name only. An expression such as @click="open = !open" becomes @click="toggle", with toggle defined in the component. If a store may need strict CSP, adopting this style early avoids later rewrites.
Keeping components fast and accessible
- Keep each
x-datascope as small as the feature requires. - Avoid heavy work inside
x-init; fetch data only when it is needed. - Prefer CSS transitions to JavaScript animation loops.
- Add
x-cloakto elements that should stay hidden until Alpine has initialised, to avoid a flash of unstyled content. - Keep ARIA attributes, such as
aria-expanded, in sync with state so screen readers announce changes.
Forms and validation
Forms are where Alpine's two-way binding earns its keep. x-model links an input to component state, so other parts of the form can react immediately, for example showing a shipping estimate once a postcode is entered.
Hyvä also includes a JavaScript form validation helper that works with standard HTML validation attributes such as required, minlength and pattern, and adds custom rules where needed. Using native attributes first keeps forms accessible and means basic validation still works if a script fails to load.
Modals and dialogs
Modals, drawers and off-canvas menus are common in storefronts. Recent Hyvä releases include an x-htmldialog plugin that works with x-show to use the browser's native <dialog> element. Native dialogs handle focus management, the Escape key and the backdrop correctly, which removes much of the accessibility work that custom modals require.
Debugging Alpine components
A few habits make problems quick to find:
- Check the browser console first; Alpine reports expression errors with the element that caused them.
- Install the Alpine.js developer tools browser extension to inspect component state live.
- Temporarily add
x-text="JSON.stringify($data)"to an element to see the whole state at a glance. - If a component never initialises, check whether
x-deferorx-ignoreis delaying or blocking it.
Migrating Knockout code
When adapting a Luma extension, Knockout templates and UI components must be rewritten in Alpine. The concepts map reasonably well: Knockout observables become Alpine state, bindings such as visible and click become x-show and @click, and customerData subscriptions become listeners for the private-content-loaded event.
The rewrite is usually shorter than the original, because Alpine needs no module definitions or component configuration in layout XML. Start from the rendered HTML the shopper should see, then add behaviour, rather than translating the old code line by line.
Conclusion
Alpine.js gives Hyvä small, readable components that sit inside ordinary Magento templates. Use the function-based pattern for anything non-trivial, share data through events, defer components that are not needed immediately, and write CSP-friendly code if strict policies are on your roadmap. The result is interactivity that feels quick without the weight of a full framework.
Frequently Asked Questions
It is possible, but discouraged, because loading jQuery reintroduces much of the weight Hyvä removes. Most jQuery tasks are simpler in Alpine.
Current Hyvä releases use Alpine.js version 3. Very early 1.x releases used version 2.
It delays initialising an Alpine component until it is visible, the user interacts, the browser is idle or a named event fires.
Only if your store must enforce a strict Content Security Policy without unsafe-eval, which is increasingly expected on payment pages.


