Documentation menu
Getting Started
Core Concepts
Control
Interactivity
Pushing Data
HTMX Extensions
Miscellaneous
Configuration
Alpine Compat
The alpine-compat extension preserves Alpine.js state and bindings on matching nodes when htmx morphs content.
htmgo includes the extension in /public/htmgo.js. Its Alpine hooks run when window.Alpine is available.
Load Alpine and htmgo
Include both scripts in the page head. Use defer so Alpine initializes after the document is parsed.
1h.Head(
2 h.Script("/public/htmgo.js"),
3 h.Tag("script",
4 h.Src("https://unpkg.com/alpinejs@3.15.11/dist/cdn.min.js"),
5 h.Attribute("defer", ""),
6 ),
7)Add this CSS to keep x-cloak elements hidden until Alpine initializes them.
1[x-cloak] { display: none !important; }Load a popover on demand
Import Alpine directive helpers from framework/v2/ax. The popover loads its content when it first becomes visible.
1func Popover() *h.Element {
2 return h.Div(
3 ax.Data("{ open: false }"),
4 ax.OnKeydown("open = false", "escape", "window"),
5 h.Button(
6 h.Type("button"),
7 ax.OnClick("open = !open"),
8 ax.Bind("aria-expanded", "open"),
9 h.Text("Toggle"),
10 ),
11 h.Div(
12 ax.Show("open"),
13 ax.Cloak(),
14 ax.OnClickOutside("open = false"),
15 h.GetPartial(PopoverContent, "intersect once"),
16 h.Attribute(hx.SwapAttr, "innerMorph"),
17 h.Text("Loading…"),
18 ),
19 )
20}
21
22func PopoverContent(ctx *h.RequestContext) *h.Partial {
23 return h.NewPartial(h.P(h.Text("Popover content")))
24}Use innerMorph or outerMorph when htmx needs to update Alpine-managed content in place. The extension copies the existing Alpine data stack when htmx matches old and new nodes.