Command

A searchable menu of commands, with keyboard shortcuts.

Installation

  1. Add the registryOnce per project
  2. Add command

Usage

Pass the items to Command and render each one from the function inside CommandList. Typing filters them by label; items without one need an itemToStringValue. CommandEmpty shows when nothing matches. An item's onSelect runs on click or Enter.

The list never scrolls sideways. Text too long for its row ends in an ellipsis, whether it's an item's own text, a plain span inside it or a group's heading. Components inside an item, such as a Badge, keep their size, and the row cuts off whatever doesn't fit.

To group items, pass an array of groups, each with its own items, and render each as a CommandGroup holding a CommandCollection. Put a CommandSeparator between groups.

Composition

Examples

In a dialog

CommandDialog puts the menu in a Dialog for a command palette. CommandDialogTrigger opens it, and shortcut on CommandDialog takes a chord such as "Mod+K" that opens and closes it from anywhere on the page. CommandDialogContent holds the Command, with a title and description for screen readers. Choosing an item with an onSelect closes the dialog.

shortcut on an item takes a chord too, shows it, and runs onSelect when it's pressed inside the menu.

The trigger shows its chord with useShortcutDisplay, which writes it the way the reader's platform does, such as ⌘ K on a Mac and Ctrl K elsewhere. The Kbd page covers it, along with ShortcutChord, the type of each shortcut, which comes from the same file.

CommandFooter goes last in Command, under the list, and stays at the bottom while the list scrolls or CommandEmpty shows. It holds hints about the keys the menu takes, or anything else that belongs under the results. CommandHint is one hint: shortcut takes a chord, or an array of them such as ["ArrowUp", "ArrowDown"], and shows each as keys before the label, written the way the reader's platform writes them. It only shows the keys, since the menu already handles the arrows, Enter and each item's own shortcut. Give a hint className="ml-auto" to push it, and any after it, to the far end, as the dialog above does with Esc.

Like useShortcutDisplay, a hint can't show its keys until the page hydrates, so it stays hidden until then. Hints are for keyboard users: screen readers skip them, since the menu's roles already tell how to move through it. The examples show the footer at every width, but a menu that's mostly opened on touch screens can hide it on narrow ones with className="max-sm:hidden".

Props

The tables leave out className, style and render. Parts built on Base UI take them as its styling and composition guides describe.

Command

CommandDialog

CommandDialogTrigger

CommandDialogContent

CommandInput

CommandList

CommandGroup

CommandCollection

CommandSeparator

CommandItem

CommandHint