Menus/Command

Command

Fast, composable command menu for React.

Themed

When to Use

Use Command when you need to:

  • Provide a keyboard-first search interface for actions, pages, or settings
  • Build a command palette (Cmd+K / Ctrl+K) for power-user navigation
  • Create a filterable list of options grouped by category
  • Offer searchable menus with keyboard shortcut hints
  • Implement spotlight-style search that overlays the entire application

When Not to Use

Default

Full-featured command menu with groups, icons, keyboard shortcuts, and disabled items.

Dialog

Command menu rendered inside a dialog overlay, triggered by a keyboard shortcut.

Press J

Command Palette

Search for a command to run...

Simple

Minimal command menu without icons, suitable for basic text-only lists.

UX & Design Guidelines

Grouping & Organization

Use CommandGroup with descriptive heading props to organize items by category (e.g., "Suggestions", "Settings", "Recent"). Separate groups with CommandSeparator for clear visual boundaries. Limit groups to 5-8 items each to avoid overwhelming users.

Search & Filtering

The built-in fuzzy search filters items automatically. Always include a CommandEmpty component with a helpful "No results found" message. For custom filtering (e.g., async search), set shouldFilter={false} on the root Command and implement your own filter logic.

Responsive Behavior

On mobile, use the inline variant (without Dialog) since overlay command palettes are less discoverable on touch devices. When using CommandDialog, ensure the dialog content does not exceed viewport height by constraining CommandList with max-h-[300px] (the default).

Color & Contrast

The command menu uses bg-popover with text-popover-foreground for the container. Selected items highlight with bg-accent and text-accent-foreground. Group headings and shortcut text use text-muted-foreground for visual hierarchy without competing with item labels.

Accessibility

Keyboard Navigation

  • Arrow Up / Arrow Down — Navigate between items
  • Enter — Select the highlighted item
  • Escape — Close the command dialog
  • Cmd + K — Common shortcut to open the command palette
  • Type-ahead filtering begins immediately as the user types in the search input

Screen Reader Support

  • Built on cmdk which provides role="listbox" and role="option" semantics
  • Group headings are announced via role="group" with aria-label
  • Selected state is conveyed via aria-selected
  • Disabled items are announced with aria-disabled

Focus Management

When CommandDialog opens, focus is automatically placed on the search input. Focus is trapped within the dialog overlay, preventing interaction with background content. On close, focus returns to the element that triggered the dialog. The inline Command variant does not trap focus.

Dialog Labeling

The CommandDialog accepts title and description props that render as visually hidden text (using sr-only) for screen readers. Always provide meaningful values for these props.

ARIA Attributes

{/* Command with accessible labels */}
<CommandDialog
  open={open}
  onOpenChange={setOpen}
  title="Command Palette"
  description="Search for a command to run..."
>
  <CommandInput placeholder="Type a command or search..." />
  <CommandList>
    <CommandEmpty>No results found.</CommandEmpty>
    <CommandGroup heading="Actions">
      <CommandItem onSelect={() => handleAction("profile")}>
        <User />
        <span>Profile</span>
        <CommandShortcut>⌘P</CommandShortcut>
      </CommandItem>
      <CommandItem disabled>
        <Lock />
        <span>Admin Panel</span>
      </CommandItem>
    </CommandGroup>
  </CommandList>
</CommandDialog>

API Reference

The Command component is composed of multiple parts. Each sub-component accepts the following props in addition to standard HTML attributes.

Command

Root container with built-in fuzzy filtering.

PropTypeDefaultDescription
classNamestringAdditional CSS classes applied to the root container.
childrenrequiredReactNodeCommand content including input, list, groups, and items.
filter(value: string, search: string) => numberCustom filter function. Return 1 to keep, 0 to remove.
shouldFilterbooleantrueWhen false, disables built-in filtering for custom logic.
valuestringControlled value of the selected item.
onValueChange(value: string) => voidCallback when the selected item value changes.

CommandDialog

Command menu rendered inside a dialog overlay with focus trapping.

PropTypeDefaultDescription
openbooleanfalseWhether the dialog is open.
onOpenChange(open: boolean) => voidCallback fired when the dialog open state changes.
titlestring"Command Palette"Accessible title for the dialog (visually hidden).
descriptionstring"Search for a command..."Accessible description for the dialog (visually hidden).
showCloseButtonbooleantrueWhether to show the close button in the dialog.

CommandInput

Search input with a built-in search icon and automatic filtering.

PropTypeDefaultDescription
placeholderstringPlaceholder text displayed when the input is empty.
valuestringControlled value of the search input.
onValueChange(value: string) => voidCallback fired when the search input value changes.
classNamestringAdditional CSS classes applied to the input.

CommandItem

Selectable option within a command list or group.

PropTypeDefaultDescription
valuestringValue used for filtering and selection. Defaults to text content.
onSelect(value: string) => voidCallback fired when the item is selected via click or Enter.
disabledbooleanfalseWhen true, prevents selection and reduces opacity.
classNamestringAdditional CSS classes applied to the item.

CommandGroup

Groups related command items under a heading.

PropTypeDefaultDescription
headingstringHeading text displayed above the group of items.
classNamestringAdditional CSS classes applied to the group.