Menu
@ailura/alpinejs-menu
A dropdown or context menu. The plugin owns the open state, the roving tabindex, the arrow-key navigation, and closing on an outside click; you write the markup.
Install
Section titled “Install”pnpm add alpinejs @ailura/alpinejs-menuRegister the plugin
Section titled “Register the plugin”Do this once, before Alpine.start().
import Alpine from "alpinejs";import menuPlugin from "@ailura/alpinejs-menu";
Alpine.plugin(menuPlugin());
Alpine.start();That registers a menu store, so everything below is reachable at $store.menu.
Minimal example
Section titled “Minimal example”A row of actions behind a trigger button.
<div x-data="{ id: 'row-actions' }" x-init=" $store.menu.register(id); $store.menu.registerItem(id, 'rename'); $store.menu.registerItem(id, 'duplicate'); $store.menu.registerItem(id, 'delete'); " @keydown="$store.menu.handleKeydown(id, $event)" @click.outside="$store.menu.close(id)"> <button @click="$store.menu.toggle(id)" :aria-expanded="$store.menu.isOpen(id)" aria-haspopup="menu" > Actions </button>
<div x-bind="$store.menu.menuProps(id)" x-bind:aria-hidden="$store.menu.menuHidden(id)" x-show="$store.menu.isOpen(id)" > <template x-for="item in ['rename', 'duplicate', 'delete']" :key="item"> <button x-bind="$store.menu.itemProps(id, item)" x-bind:tabindex="$store.menu.itemTabIndex(id, item)" x-bind:aria-disabled="$store.menu.itemDisabled(id, item)" @click="$store.menu.selectItem(id, item)" > <span x-text="item"></span> </button> </template> </div></div>itemProps() returns role="menuitem" and a stable id. menuProps() returns
role="menu", the matching id, and aria-orientation.
close() returns focus to the trigger, so a menu closed by Escape or by
choosing an item does not strand the keyboard inside a hidden subtree. It looks for a
focusable element inside whatever bindTrigger() was given, so passing a wrapper <div>
around the button is fine.
Variants
Section titled “Variants”Keep the menu open after a choice. The default closes on select, which is right for commands. Turn it off for a menu of toggles.
$store.menu.register(id, { closeOnSelect: false });Turn arrow keys with the layout. Set orientation to vertical and up/down
navigate; leave it horizontal and left/right do.
$store.menu.register(id, { orientation: "vertical" });Group items under a parent. parentId records that an item belongs to a group, so
your own markup can nest or filter by it. The arrow keys walk the flat item list, so
drilling into a submenu is still your code.
$store.menu.registerItem(id, "export-pdf", { parentId: "export" });Lock an item. A disabled item is skipped by the arrow keys and reports
aria-disabled.
$store.menu.registerItem(id, "delete", { disabled: true });React to selections. onSelect fires with the item id, which is where you route
the command.
$store.menu.register(id, { onSelect: (itemId) => run(itemId) });API reference
Section titled “API reference”Reach for this once the menu already works and you need the exact signature.
| Name | Type | Purpose |
|---|---|---|
$store.menu.register(id, options?) |
method |
Create an instance. Options: orientation, closeOnSelect, onOpen, onClose, onSelect. |
$store.menu.unregister(id) |
method |
Drop an instance. |
$store.menu.registerItem(id, itemId, options?) |
method |
Add an item. Options: disabled, parentId. |
$store.menu.unregisterItem(id, itemId) |
method |
Remove an item. |
$store.menu.open(id) / close(id) / toggle(id) |
method |
Change the open state. |
$store.menu.isOpen(id) |
method |
Whether it is open. |
$store.menu.selectItem(id, itemId) |
method |
Activate an item, firing onSelect. |
$store.menu.handleKeydown(id, event) |
method |
Arrows, Home, End, Enter, Space. Required for keyboard. |
$store.menu.handleOutsideClick(id, event) |
method |
Click outside the menu. |
$store.menu.itemProps(id, itemId) |
method |
role="menuitem", id. Static: bind per attribute, not as an object. |
$store.menu.itemTabIndex(id, itemId) |
method |
0 for the active item, -1 otherwise. For x-bind:tabindex. |
$store.menu.itemDisabled(id, itemId) |
method |
Whether an item is locked. For x-bind:aria-disabled. |
$store.menu.menuProps(id) |
method |
role="menu", id, aria-orientation. Static. |
$store.menu.menuHidden(id) |
method |
true while closed. For x-bind:aria-hidden. |
$store.menu.instances |
store |
Reactive registry of every instance. |
Plugin options
Section titled “Plugin options”menuPlugin() takes options only if you already own the menu store name, if you
need the magic under a different key, or if you want more than one menu open at a time
(exclusive defaults to true).
menuPlugin({ id: "app-menu", storeKey: "appMenu", exclusive: false });