Skip to content

Command menu

command-menu

A command palette opened with ⌘K or Ctrl K, with a search field, grouped items and matched letters highlighted as you type. Ranking prefers whole-word matches, and cmdk's list runs inside a Base UI dialog for the focus trap.

Install

$npxshadcnadd@forme/command-menu

Adds the component, its registry dependencies, and the token stylesheet you import once.

Usage

import {
  CommandMenu, CommandMenuEmpty, CommandMenuGroup, CommandMenuInput, CommandMenuItem, CommandMenuList,
} from "@/components/forme/command-menu/command-menu";

<CommandMenu open={open} onOpenChange={setOpen}>
  <CommandMenuInput placeholder="Search…" />
  <CommandMenuList>
    <CommandMenuEmpty>Nothing found.</CommandMenuEmpty>
    <CommandMenuGroup heading="Projects">
      <CommandMenuItem description="Design system" onSelect={() => go("/forme-web")}>
        forme-web
      </CommandMenuItem>
    </CommandMenuGroup>
  </CommandMenuList>
</CommandMenu>

When to use

Reach for it

  • Finding a page or running a command by name, keyword or a few letters in order, with the matching letters highlighted as you type.

Use something else

  • A handful of actions on one object; a Dropdown is closer to hand.
  • The only way to reach something. Everything in the palette should also be reachable without it.

API reference

Read from the source, so it is the API of the file you install. A prop marked * is required.

CommandMenu

PropTypeDefaultDescription
children*ReactNode—
labelstring"Command menu"Names the palette for screen readers.
openboolean—Pass these when the items need to close the palette on select. Left out, the menu keeps its own state and closes on Esc or a click outside.
onOpenChange(open: boolean) => void—
shouldFilterbooleantrueLeave on to let the palette filter and rank items as you type. Turn off when you filter yourself — a search index with its own scoring — and render only the results, in the order you want them.
filterCommandMenuFiltercommandMenuFilterScores one item against the query. Return 0 to hide it. Defaults to word-start matching with an ordered-subsequence fallback, where every word of the query has to match.

CommandMenuItem

PropTypeDefaultDescription
descriptionstring—A short note after the label. Searched like a keyword.
shortcutreadonly string[]—Keycaps drawn at the end of the row. Labels only: bind the keys yourself.

Also takes every prop of ComponentProps<typeof CommandItem>.

The other parts

  • CommandMenuInputTakes ComponentProps<typeof CommandInput>
  • CommandMenuListTakes ComponentProps<typeof CommandList>
  • CommandMenuEmptyTakes ComponentProps<typeof CommandEmpty>
  • CommandMenuGroupTakes ComponentProps<typeof CommandGroup>
  • CommandMenuSeparatorTakes ComponentProps<typeof CommandSeparator>

Accessibility

  • Focus stays in the input while the arrow keys move the selection and Enter runs it.
  • Escape clears the query first and closes on the second press; the result count is announced as it changes.
  • label names the palette and the search field for screen readers.

Motion

  • The panel condenses in — fade, 96% scale, a slight blur — over 150ms and leaves on the exit curve; the selection tint glides between rows in 150ms. Reduced motion collapses both to an instant change.