This page as markdown: /command-menu.md · Index for agents: /llms.txt · Everything in one file: /llms-full.txt

CommandMenu

Status: stable

A command palette (Cmd+K) built on our Dialog: a top-aligned wide surface with a search input and a scrollable, JS-filtered list of grouped commands. CommandMenuPanel renders the same palette in place, on a card in the page.

Keywords: command, command menu, palette, launcher, cmd k, search, quick action, spotlight, navigation, search trigger, header search, keyboard shortcut, inline command menu, embedded palette, settings search

Usage

Use CommandMenu for global or contextual keyboard-driven actions: navigation, creation, quick settings, or searchable commands. It should feel curated and fast; use Select, Menu, or Combobox for ordinary inline choices.

Best practices

Anatomy

Part Required Description
Trigger no CommandMenuTrigger — the search-field-shaped button in a header that opens the palette on click or on ⌘K / Ctrl+K. It sits outside CommandMenu; wire its onOpen to the palette's open state.
Dialog surface yes The modal shell that traps focus and positions the launcher. CommandMenuPanel replaces it with a static card in the page (border, card fill, no shadow).
Search input yes The combobox input that filters commands while focus remains in the field.
Groups no Labelled command sections that keep results scannable.
Items yes Keyboard-selectable command rows that run onSelect and close the palette.

Subcomponents

Name Type Description
CommandMenuTrigger { onOpen?: () => void; placeholder?: string; shortcut?: string | false; size?: "sm" | "md"; disabled?: boolean } The search field in a header that is really a button. Wears SearchInput's shell (the field fill and outline, leading magnifier, muted placeholder) on the control ladder, with a trailing Kbd: ⌘K on macOS, Ctrl K elsewhere. onOpen runs on click and on the shortcut, which it listens for page-wide (useCommandMenuShortcut). shortcut is the key (default "k"); false drops both the hint and the listener. size is sm in a header beside small buttons, md (default) beside Input and SearchInput. It sits outside CommandMenu, so the Dialog returns focus to it on close.
useCommandMenuShortcut (onShortcut: (event: KeyboardEvent) => void, key?: string | false) => void The page-wide ⌘K / Ctrl+K listener CommandMenuTrigger installs, for a palette with no visible trigger. Ignored while focus is in a text field, textarea, select or contenteditable (Ctrl+K belongs to the field there) except the palette's own input, so the callback may toggle. It calls preventDefault() on a match and skips an event already prevented, so two listeners on one page fire once. key defaults to "k"; false turns it off. Removed on unmount.
CommandMenuPanel { items?: CommandItem[]; query?: string; onQueryChange?: (query: string) => void; filter?: CommandFilter; listClassName?: string; placeholder?: string; emptyMessage?: string; title?: string; scope?: ReactNode; inputProps?: ComponentProps<'input'>; elevation?: 'resting' | 'raised'; className?: string; children?: ReactNode } The palette rendered in place: the same search header, filtered and grouped listbox, keyboard model and result-count live region, on a static card (border-border, bg-card, no shadow) instead of in a Dialog. elevation="raised" adds the card's soft resting shadow, for a palette shown as a card among cards; never a shadow-* className, and a palette that should float goes in a Popover. Takes every CommandMenu prop except open, onOpenChange, finalFocus and description; className lands on the card, title names the listbox. Focus is not trapped and the input does not take focus on mount (inputProps.autoFocus opts in). Esc clears the query, and passes through once it is empty, so a Popover around the panel still closes. A chosen row runs its onSelect and the panel stays as it was. The page-wide shortcut does not fire from its input.
CommandMenuGroup { label: string; children } A labelled group of command rows.
CommandMenuItem { onSelect?: () => void; icon?: string; detail?: ReactNode; stacked?: boolean; hint?: ReactNode; disabled?: boolean; id?: string; children } A selectable row. Self-filters by its text (unless the root was given a filter), runs onSelect, then closes the palette. detail is secondary text after the label, or under it with stacked; hint is the end-aligned trailing slot — a shortcut, a count, or a disabled row's reason. A disabled row stays listed, stays announced, and stays reachable by arrow key so its reason can be read, but is inert to Enter and to a click.

Props

Name Type Default Description
scope ReactNode — A scope chip in the header, between the search glyph and the input — "In: Backdrop". A scope belongs beside the field rather than in its value: put it in the value and the user deletes prose to widen their search, and your filter parses it back out. Pair with inputProps.onKeyDown handling Backspace on an empty query, or the only way out of a scope is the mouse.
inputProps Omit<ComponentProps<'input'>, 'role' | 'value' | 'onChange' | aria-*> — Extra props for the search input. onKeyDown runs BEFORE the palette's own handling and opts out of a key by calling preventDefault() — which is what makes a scope chip Backspace-dismissable. Running first is deliberate: Backspace happens not to be a key the palette claims today, so a plain passthrough would work until the palette grows a 'Backspace clears the query' affordance, at which point the consumer's behaviour breaks silently. The combobox wiring (role, aria relationships, controlled value) is not overridable; aria-label and className compose.
open boolean — Controlled open state of the palette.
onOpenChange (open: boolean) => void — Called on open/close (select, Esc, outside click).
items CommandItem[] — Data-driven commands ({ group, label, onSelect, id?, icon?, detail?, stacked?, hint?, disabled? }). stacked puts detail on its own line under the label, for results only their detail tells apart (two tickets with one subject); beside a long label an inline detail truncates first. Ignored when children are provided. id is optional identity: filter is handed these same objects, so it is how a caller that ranks over its own richer model maps the array back onto it — and it becomes the row's DOM id (namespaced to the palette) and React key, so two rows may share a label.
query string — Controlled query. Leave undefined to let the palette own it; either way it resets on close (a controlled caller gets onQueryChange('')).
onQueryChange (query: string) => void — Called on every query change. Required to control query, optional otherwise.
filter ((items: CommandItem[], query: string) => CommandItem[]) | null case-insensitive substring match on label Replaces or disables the built-in match. A function ranks and filters items itself and its order is kept verbatim; null renders items exactly as given. Either also stops declarative CommandMenuItem children self-filtering. The query arrives raw — trimming and case are policy, and this is the prop that hands policy over.
highlightMatches boolean true Paints the query wherever it appears in a row's label (not its detail or hint), on the highlight / highlight-foreground pair, through the CSS Custom Highlight API — no <mark> in the row, and the accessible name is unchanged. A literal, case-insensitive match, so under a fuzzy filter a row that matched as a subsequence keeps an unpainted label. false turns it off. Applies to CommandMenuPanel too.
listClassName string — Classes for the scrolling list. className lands on the dialog surface, so this is the seam for the list's own height cap — listClassName="max-h-[60vh]". The list is a Scroller, so the cap lands on the element that scrolls and the edge fades come free.
placeholder string "Type a command or search…" Placeholder for the search input.
emptyMessage string "No results found." Copy shown when the query matches nothing.
searchLabel string "Command search" Accessible name of the search field. inputProps["aria-label"] still wins when both are set.
clearLabel string "Clear search" Accessible name of the clear button shown while there is a query.
resultCountLabel (count: number) => string (count) => count === 0 ? "No results" : ${count} result${count === 1 ? "" : "s"} What the polite live region announces after the list changes. Translate the whole phrase, plural included.
title string "Command menu" Accessible name for the palette dialog (visually hidden).
description string "Search for a command and press Enter to run it." Accessible description for the palette dialog (visually hidden).
finalFocus RefObject | ((closeType) => HTMLElement | false) | false — Where focus goes when the palette closes. The Dialog default returns it to the trigger — the CommandMenuTrigger, when there is one. A palette opened by the keystroke alone has none, so pass a resolver to restore whatever had focus, or false to restore nothing.
children ReactNode — Declarative commands via CommandMenuGroup / CommandMenuItem. Takes precedence over items.

Tokens used

highlight, highlight-foreground, card, card-foreground, border, foreground, muted-foreground, accent, accent-foreground

Motion

Accessibility

Related

Examples

Data-driven

import { CommandMenu } from "@sableui/react"

<CommandMenu open={open} onOpenChange={setOpen} items={[{ group: 'Navigation', label: 'Go to feed', icon: 'home', onSelect: () => {} }, { group: 'Actions', label: 'New post', onSelect: () => {} }]} />

Declarative

import { CommandMenu, CommandMenuGroup, CommandMenuItem } from "@sableui/react"

<CommandMenu open={open} onOpenChange={setOpen}><CommandMenuGroup label='Navigation'><CommandMenuItem icon='home' onSelect={() => {}}>Go to feed</CommandMenuItem></CommandMenuGroup></CommandMenu>

Grouped launcher

import { CommandMenu, CommandMenuGroup, CommandMenuItem } from "@sableui/react"

<CommandMenu open={open} onOpenChange={setOpen}><CommandMenuGroup label="Create"><CommandMenuItem icon="plus" onSelect={() => createPost()}>New post</CommandMenuItem><CommandMenuItem icon="sparkles" onSelect={() => createProject()}>New project</CommandMenuItem></CommandMenuGroup><CommandMenuGroup label="Navigation"><CommandMenuItem icon="home" onSelect={() => navigate("/")}>Home</CommandMenuItem><CommandMenuItem icon="gear" onSelect={() => navigate("/settings")}>Settings</CommandMenuItem></CommandMenuGroup></CommandMenu>

Consumer-owned ranking

import { CommandMenu } from "@sableui/react"

<CommandMenu open={open} onOpenChange={setOpen} query={query} onQueryChange={setQuery} filter={(items, q) => rank(items, q)} items={items} />

Listed but unavailable

import { CommandMenu } from "@sableui/react"

<CommandMenu open={open} onOpenChange={setOpen} items={[{ group: 'Layers', label: 'Align left', disabled: true, hint: 'Select another layer to align to', onSelect: () => {} }]} />

Inline panel

import { CommandMenuPanel, Kbd } from "@sableui/react"

<CommandMenuPanel className="w-80" items={[{ group: 'Create', label: 'New project', icon: 'plus', hint: <Kbd>N</Kbd>, onSelect: () => {} }, { group: 'Settings', label: 'Open settings', icon: 'gear', onSelect: () => {} }]} />

Header search trigger

import { CommandMenu, CommandMenuTrigger } from "@sableui/react"

<CommandMenuTrigger size="sm" placeholder="Search docs" onOpen={() => setOpen(true)} className="w-56" />
<CommandMenu open={open} onOpenChange={setOpen} items={items} />

Keyboard shortcut with no visible trigger

import { CommandMenu, useCommandMenuShortcut } from "@sableui/react"

useCommandMenuShortcut(() => setOpen((value) => !value))
<CommandMenu open={open} onOpenChange={setOpen} items={items} />