Skip to content

Organisms · Command palette

Command palette

Every action in one place. Press Ctrl+K anywhere, type a few letters, move with the arrow keys and press Enter. Focus (where keyboard input goes) returns to where it was, and then the command runs.

Best for
Every action in one place behind Ctrl+K, with a visible button that opens it too.
Not for
Searching page content: use Search. A few actions on one control: use DropdownMenu.

This page as Markdown, a plain-text copy for a coding agent (an AI tool that writes code).

Examples

Store commands

Last command: none

Code
tsx
function StoreCommands() {  const [open, setOpen] = useState(false)  const [last, setLast] = useState('none')  const make = (id: string, label: string, group: string, icon: ReactNode, extra: Partial<Command> = {}): Command => ({    id,    label,    group,    icon,    onSelect: () => setLast(label),    ...extra,  })  const commands = [    make('orders', 'Go to orders', 'Navigate', <Package />, { keywords: ['sales'] }),    make('shipments', 'Go to shipments', 'Navigate', <Truck />),    make('returns', 'Go to returns', 'Navigate', <Inbox />),    make('new-shipment', 'New shipment', 'Create', <Plus />, { shortcut: 'N' }),    make('new-product', 'New product', 'Create', <Box />),    make('invite', 'Invite a teammate', 'Account', <Users />, { keywords: ['team', 'people'] }),    make('settings', 'Open settings', 'Account', <Settings />),  ]  return (    <div className="flex flex-wrap items-center gap-3">      <Button variant="secondary" aria-keyshortcuts="Control+K Meta+K" onClick={() => setOpen(true)}>        Open command palette      </Button>      <span aria-hidden="true" className="inline-flex gap-1">        <Kbd>Ctrl</Kbd>        <Kbd>K</Kbd>      </span>      <p role="status" className="font-mono text-xs text-[var(--color-text-secondary)]">        Last command: {last}      </p>      <CommandPalette commands={commands} open={open} onOpenChange={setOpen} />    </div>  )}​<StoreCommands />

Anatomy

PartWhat it is
DialogNamed 'Command palette', with the name hidden from view. Drawn on surface.base with the corner crosshair.
Search fieldA combobox, a text field tied to a list of choices. Focus stays here the whole time.
ListboxNamed 'Commands'. It's not there at all when nothing matches.
GroupNamed by its heading.
OptionThe active one gets the fill, and the search field points to it as its active descendant, so screen readers announce it while focus stays in the field.
CountA status line hidden from view but read by screen readers: '2 commands'.
Shortcut hintA Kbd that's hidden from assistive technology (screen readers and similar tools). It's for display only.

Props

commandsCommand[]
Required. Each has id, label, description (a quieter second line), group, keywords, shortcut, icon and onSelect.
openboolean
Open state when controlled, meaning your code decides whether it is open.
defaultOpenbooleanDefault false
Starting open state when uncontrolled, meaning the palette keeps track of it itself.
onOpenChange(open: boolean) => void
Called on every open and close, including Escape and a click on the scrim (the dimmed backdrop). If you refuse the close, the chosen command stays ready to run until the user does anything else in the palette. Closing it yourself later counts as accepting it.
shortcutbooleanDefault true
Ctrl+K or Cmd+K opens and closes the palette from anywhere.
labelstringDefault 'Command palette'
The dialog's accessible name (what a screen reader announces), hidden from view.
searchLabelstringDefault 'Search commands'
The search field's accessible name.
placeholderstringDefault 'Type a command or search'
Shown in the empty search field.
emptyMessagestringDefault 'No matching commands'
Shown when nothing matches the search.
filter(commands: readonly Command[], query: string) => Command[]Default filterCommands
Your own matching and ranking. Gets every command and the query, returns the ones to show in order; groups follow that order. The docs site search uses it to rank with the same scorer as the MCP server.
refRef<HTMLInputElement>
The search field, while the palette is open.

Keyboard

KeysResult
CtrlKOpens or closes it from anywhere on the page (Cmd+K on macOS).
↓Next command, wrapping round to the first.
↑Previous command, wrapping round to the last.
HomeFirst command.
EndLast command.
EnterRuns the active command.
EscCloses without running anything.

Do and don't

Do

  • Name commands as verbs: 'Go to orders', 'New shipment'.
  • Add keywords for the words people actually type.
  • Give it a visible button that opens it too. People won't find a shortcut on its own.

Don't

  • Don't put destructive commands in it without a dialog that asks for confirmation.
  • Don't show a shortcut hint the app doesn't actually set up.
  • Don't put more than one palette on a page. Each one listens for the same Ctrl+K/Cmd+K across the whole page, so a second one fights the first for the shortcut.