Note: This pattern is extra experimental because it composes other HyperUX primitives.
Press to open from anywhere.
Command palette
No results found.
Alpine.js Command Palette
huxCommandPalette is a keyboard-first selector that composes huxDialog and huxCombobox behavior. It filters named commands and supports both link navigation and custom action dispatch.
Because this pattern composes other primitives, changes in dialog/combobox behavior may affect it. Treat the API as extra experimental.
The live demo on this page uses shortcutKey: 'p' so it does not conflict with the global site search shortcut.
API
huxCommandPalette(config)
Returns an Alpine data object with:
paletteItems: Array<{ label: string, href?: string, action?: string, target?: '_self' | '_blank' }>paletteId: string | nullshortcutKey: stringshortcutHint: stringhandleGlobalShortcut(event): voidfindPaletteItem(itemLabel: string): object | nullselectItem(itemLabel: string): object | null
Internal helper methods are private implementation details and are not part of the supported API contract.
Options
paletteItems: Array<{ label: string, href?: string, action?: string, target?: '_self' | '_blank' }>(default:[])paletteId: string(optional, scopes action event names)shortcutKey: string(default:'k')
Use hyphen-case for action names. Non-hyphen-case names will be ignored.
Quick Start
<div
x-data="huxCommandPalette({
paletteId: 'docs-palette',
paletteItems: [
{ label: 'Docs', href: '/patterns/command-palette' },
{ label: 'Open GitHub', href: 'https://github.com/markmead/hyperux', target: '_blank' },
{ label: 'Copy URL', action: 'copy-page-url' }
],
shortcutKey: 'p'
})"
x-on:keydown.window="handleGlobalShortcut($event)"
x-on:hux-command-palette:docs-palette:copy-page-url.window="navigator.clipboard.writeText(location.href).catch(() => {})"
>
...
</div>Common Usage Patterns
Internal + External + Action Items
huxCommandPalette({
paletteItems: [
{ label: 'Scroll Spy Docs', href: '/patterns/scroll-spy' },
{ label: 'Open GitHub', href: 'https://github.com/markmead/hyperux', target: '_blank' },
{ label: 'Copy URL', action: 'copy-page-url' },
],
})Change Shortcut Key
huxCommandPalette({
paletteItems: [{ label: 'Open Docs', href: '/patterns' }],
shortcutKey: 'p',
})Behavior Contract
paletteItemsare filtered to entries withlabeland eitherhreforaction.shortcutKeyis normalized to lowercase.- Shortcut only triggers when key matches and either Meta or Ctrl is pressed.
- Shortcut handling is skipped while typing in
input,textarea, or contenteditable fields. selectItem()returns selected item when found;nullwhen not found.- Link items navigate (
_blankuseswindow.openwith noopener/noreferrer).
Error Handling
- Missing/invalid items are filtered out without throwing.
- Unknown labels in
selectItem()returnnulland no-op. - Browser popup blocking may affect
_blanklinks; selection still resolves as successful item handling.
Accessibility Notes
- Keep the composed dialog markup with
role="dialog"andaria-modal="true". - Keep the palette input as
role="combobox"with properaria-expanded,aria-controls, andaria-activedescendantwiring. - Provide a clear accessible name for the search input (visible label or sr-only label).
- Keep result rows as
button type="button"elements so keyboard users can trigger selection reliably.
Notes
- Emitted action event:
hux-command-palette:{paletteId}:{event-name}hux-command-palette:{event-name}whenpaletteIdis missing
- Composed dependencies:
huxDialoghuxCombobox