GhostCommandPalette
GhostCommandPalette is a modal command surface for jumping to pages, running editor actions, or switching settings from one searchable list. Import it and its parts from @ghostvue/themes for the default scrim, elevation-4 surface, input, and option styles. It composes the GhostDialog shell (focus trap, scroll lock, inert background, dismissal, and focus restoration) with useGhostCombobox (item registry, keyboard highlight, loading and empty announcements), and follows the APG combobox with list autocomplete pattern inside a modal dialog.
You own the data and the behavior: you filter the items, load async results, run the command in a command handler, and register any keyboard shortcut. The palette ships no global listener, command registry, or fuzzy search. To style the parts yourself or window very large catalogs, see Advanced: headless composition.
Search and run commands
Themed palette from @ghostvue/themes opened from its trigger or an app-owned Mod+J shortcut, with grouped commands, GhostKbd hints, an empty message, and the last command shown on the page.
Every part in this example comes from @ghostvue/themes. Open the palette with Search commands or press Ctrl+J (⌘+J on macOS), type to filter, move with the arrow keys, and press Enter. The palette closes, focus returns to where it was, and the page shows the command you ran. Type a query that matches nothing, such as zzz, to see the empty message.
Render GhostCommandPaletteItem children for the commands that match the current query. Every item needs a stable, unique id. That id is the only value the command event carries ({ id }), so key your handlers and your command catalog by it. Two items with the same id log GHOST012 in development.
GhostCommandPaletteGroup with a GhostCommandPaletteLabel names a set of related commands. GhostCommandPaletteSeparator draws a divider with role="none", so it is never a highlight or focus target. Groups never filter or sort, and they do not nest. Item order in the DOM is the highlight order.
Open with a keyboard shortcut
The palette does not intercept keys while it is closed. Register the shortcut yourself. The example adds a keydown listener on window that calls the exposed open() from a template ref (setting v-model:open works too). It skips presses that another handler already claimed (event.defaultPrevented) or that belong to an IME composition, then calls preventDefault() so the browser does not also handle the key. Remove the listener when the component unmounts.
Mod+K is the usual binding. This docs site already uses Mod+K for its own search, so the examples bind Mod+J. Give each shortcut a single owner, so one press never opens two surfaces.
Focus returns to whatever was focused when the shortcut fired, even when that is not the trigger. Use returnFocus on GhostCommandPaletteContent to send it somewhere else. Advertise the shortcut with a visible GhostKbd hint and aria-keyshortcuts on the trigger, as the example does. The headless command-palette/shortcut example on the GhostCommandPalette reference page opens the palette from a text area and returns focus to it.
Empty results
Render GhostCommandPaletteEmpty inside the list. It shows only when no items are mounted and the palette is not loading. The list then drops its listbox role so the message reads as plain text, and the polite live region announces "No results available". Write the visible message for the task, for example by suggesting a shorter query.
Activation and close
Enter on the highlighted item, or a primary click, emits command synchronously. The event is not cancelable. With closeOnSelect (the default) the palette then requests close: it emits update:open with false, and uncontrolled palettes close. Focus returns to the opener.
Activation never commits a selection and never copies the item's text into the query. The query stays as typed for as long as the palette stays open, which covers closeOnSelect: false and a controlled open that the parent keeps true. When the palette actually closes, the query resets to defaultSearch and update:search and search are emitted once, so your filtered or async results match what the input shows on the next open. A parent that controls search can keep its own value.
Accessible name
The palette must have a name. Render GhostCommandPaletteTitle, or pass aria-label or aria-labelledby to GhostCommandPaletteContent. Without either, development builds log GHOST002. There is no hidden default title. For a design without a visible heading, render the title inside GhostVisuallyHidden. When the input has no aria-label of its own, it is labelled by the title as well.
Hints, footer, and theme tokens
The example shows GhostKbd hints inside items and a key-hint footer. The palette has no footer slot: render your own markup after GhostCommandPaletteList, outside the listbox.
Importing from @ghostvue/themes loads default.css. The themed overlay and content set data-themed, so the portaled palette uses --ghost-scrim, the elevation-4 surface tokens, the control tokens for the input, and the combobox option recipe for items. The highlighted item draws the token focus ring inset, because DOM focus stays in the input. Set --ghost-command-palette-list-max-size to change how tall the list grows before it scrolls (default min(50dvh, 22rem)).
Async search
Themed palette that searches pages through a fake server: visible and announced loading, an empty message that names the query, and a stale-response guard with useGhostCommandPaletteAsyncSearch.
The example searches pages through a fake server with the themed parts. Each query change emits search with { query, requestId }. The requestId increases monotonically, including the reset on close. Pass it to begin() from useGhostCommandPaletteAsyncSearch, and apply a response only while isLatest(requestId) is true. A slow response for an older query then never replaces newer results or clears loading early. The example answers shorter queries more slowly, so typing quickly resolves out of order.
Set loading while a request is in flight. The list then keeps any mounted items (stale results), sets aria-busy, hides GhostCommandPaletteEmpty, and announces "Loading results". The visible "Searching pages…" line in the example is hidden from assistive technology because the palette already announces the loading state. When loading ends with no items, the empty message appears and "No results available" is announced. Type zzz to see it.
Accessibility
| Key | Behavior |
|---|---|
ArrowDown / ArrowUp | Moves the highlight among enabled items (wraps); focus stays in the input. |
Home / End | Highlights the first or last enabled item. |
Enter | Activates the highlighted command. |
Escape | Closes the palette (unless closeOnEscape is false) and restores focus. |
Tab / Shift+Tab | Cycles inside a modal palette. |
| Printable keys | Edit the query; you update the items. |
- Focus moves to the input on open and stays there. The highlighted item is referenced with
aria-activedescendantand reportsaria-selected="true"; pressing items, labels, or separators does not move focus. - Arrow keys,
Home,End,Enter, andEscapebelong to an active input method editor (IME) composition. The palette ignores them until the composition ends. - The polite live region inside the content announces "Loading results", the enabled result count, or "No results available". Override the strings with
messages. - A modal palette stays open while a nested portaled overlay (for example a
GhostPopoveropened from an item) takes focus. A non-modal palette (modal: false) closes when focus leaves it, without pulling focus back, following the combobox focus-departure rules. A modal palette never closes on focus departure.
Advanced: headless composition
This section is for teams that bring their own design system or need to window a very large catalog. The headless parts from @ghostvue/vue render the same markup, roles, and keyboard behavior with no styles, so you write the CSS.
Headless palette
Headless palette with static grouped commands, a separator, and consumer-owned filtering.
Import the same parts from @ghostvue/vue instead of @ghostvue/themes. The example groups static commands with a separator and filters them itself. The headless overlay and content portal to <body> with no position or surface, so the example brings its own unscoped command-palette-demo stylesheet: a fixed scrim, an opaque surface near the top of the viewport, and item states keyed on data-highlighted and aria-disabled. It reads semantic tokens with system-color fallbacks, so it also works without a theme. The large command set and shortcut examples share the same block.
Large command sets
Consumer-driven windowing over 2,000 commands: only a slice of stable ids is mounted at a time.
There is no virtualized prop. You decide which items are mounted, so the same rules cover a capped slice (as in the example) and a scroll virtualizer:
- Keep each command's
idstable while the mounted slice changes. - The keyboard highlight,
aria-activedescendant, and the result-count announcement only cover mounted items. Unmounting the highlighted item clears the highlight; it never emitscommand. - Say how many matches are hidden in your own text outside the list, and keep the query narrowing the results.
The GhostCommandPalette reference page also renders this example with a link to its source.
Styling hooks
Every part carries data-part (trigger, overlay, content, title, input, list, item, group, label, separator, empty). Items also carry data-state (active or disabled) and data-highlighted while they are highlighted; the trigger and input carry data-state="open". The list sets aria-busy="true" while loading.
API
GhostCommandPalette (root)
| Prop | Type | Default | Description |
|---|---|---|---|
closeOnEscape | boolean | true | Whether Escape closes the palette. |
closeOnSelect | boolean | true | Requests close after a command is activated. |
defaultOpen | boolean | false | Initial open state when open is omitted. |
defaultSearch | string | "" | Initial query and the value it resets to on close. |
disabled | boolean | false | Blocks opening, search, and command activation. |
loading | boolean | false | Loading presentation: hides empty, sets aria-busy, announces loading. |
messages | GhostComboboxMessages | — | Overrides loading, empty, and results-count announcements. |
modal | boolean | true | Modal shell (focus trap, scroll lock, inert background, overlay). |
open | boolean | — | Controlled open state. Omit for uncontrolled mode. |
search | string | — | Controlled query text. Omit for uncontrolled mode. |
| Event | Payload | Description |
|---|---|---|
command | GhostCommandPaletteCommandPayload | Emitted synchronously with { id } when an enabled command is activated. Not cancelable; runs before any close request. |
search | GhostComboboxSearchPayload | Emitted after each query change, including the reset on close, with a monotonic requestId for stale-response guards. |
| Slot | Slot props | Description |
|---|---|---|
default | close: () => void, focusInput: () => void, loading: boolean, open: boolean, search: string | Palette parts composed by the consumer. |
@ghostvue/themes adds no props, events, or slots to the headless GhostCommandPalette API, which it forwards.
Events: update:open, update:search, search ({ query, requestId }), and command ({ id }). The default slot receives { open, search, loading, close, focusInput }. A template ref exposes open(), close(), focusInput(), and the { element, focus, blur } contract for the input.
useGhostCommandPalette(options) returns the same state and methods for a custom root. useGhostCommandPaletteAsyncSearch() returns { begin, isLatest }.
Parts
GhostCommandPaletteTrigger: same contract asGhostDialogTrigger(as,disabled). It is optional, and the rootdisableddisables it.GhostCommandPaletteOverlay: modal backdrop (as,portal,portalTo).GhostCommandPaletteContent: therole="dialog"surface. It takesas,closeOnEscape(overrides the root),closeOnOutsideClick(defaulttrue),portal,portalTo,focusReady,initialFocus(defaults to the input), andreturnFocus. It also hosts the polite live region.GhostCommandPaletteTitle: the accessible name (as, defaulth2).GhostCommandPaletteInput: therole="combobox"query field (as,disabled).placeholderand other attributes fall through.GhostCommandPaletteList: the in-flow listbox. Style its height and scrolling withclassorstyle.GhostCommandPaletteItem:id(required),disabled,textValue, andas.GhostCommandPaletteGroup,GhostCommandPaletteLabel,GhostCommandPaletteEmpty, andGhostCommandPaletteSeparator.
Every part merges your class and style and exposes { element, focus, blur }. The data-* styling hooks are listed under Styling hooks.
Specification
See the GhostCommandPalette specification for the full contract and resolved decisions.