Skip to content
EspañolEnglish
All components (283)

Command palette

Layers · CommandPalette · .amset-palette

What it is and when to use it

The command palette, the classic Ctrl+K: it opens with the keyboard from anywhere, you type and jump wherever you want. Inside it is a modal dialog with a search field and the list of results in groups.

When to use it

  • An application with many screens or repeated actions: go to orders, create a customer, import.
  • aliases on a command for the words that should also find it: they enter the search but are not drawn.
  • shortcut on each command to show its key (it is informative: it does nothing by itself).
  • loop so the arrows wrap from the last option to the first and back.
  • filter with your own criterion (it receives the command and what was typed) instead of searchMode; what matches is highlighted in the label, except with highlight={false} or with your own filter, where it is not known which part matches.
  • shortcut={null} on the palette if only its consumer opens it, with open and onOpenChange, without Ctrl+K.

When not to

  • Choosing a value from a long list inside a form, typing to filter: SearchField.
  • The actions of a row or a button: ActionMenu.

Keyboard: Ctrl+K or Cmd+K opens it from anywhere, even with focus in a text field; the key is changed with the palette's shortcut. On opening, focus goes to the search field. Typing filters; Up Arrow and Down Arrow move through the results without leaving the field, skipping the disabled ones (with loop they wrap around at the ends); Enter chooses and closes. Escape closes. When it closes, what was typed is forgotten.

Accessibility: the dialog and the field are named with title ("Search and jump"), the list is named "Results" and each group carries its title. Focus never leaves the field: the highlighted row is announced with aria-activedescendant, and how many results remain is announced in a live region. Icons are decoration. A command with disabled is seen and cannot be chosen. If nothing matches, it says so (noResultsMessage).

Fine-tuning: CSS variables --amset-palette-width (28, 36 or 44 rem depending on the size), --amset-palette-height and --amset-palette-radius. The palette is the browser's <dialog> —the veil is its ::backdrop— and it is attached to the top, because centered vertically it ends up too low on a tall screen.

What it looks like

<CommandPalette
  groups={[{ key: 'ir', title: 'Go to', commands: [
    { key: 'orders', label: 'Orders', description: 'All orders' },
  ] }]}
  open={abierta}
  onOpenChange={setAbierta}
  shortcut={null}
  onSelect={ir}
/>

Props

Import it with import { CommandPalette } from '@amset/ui'. Required props are marked.

PropTypeWhat it does
groupsrequiredreadonly CommandGroup[]—
onSelectrequired(key: string) => void—
classNamestringMerged with the library classes; it does not replace them.
filter(command: PaletteCommand, text: string) => booleanYour own criterion to decide whether a command matches what was typed: it receives the command and the text as is. It takes precedence over `searchMode`, keeps the order of the groups and turns the highlight off, because it is not known which part matches.
footerReactNodeFooter with the key cheat sheet.
highlightbooleanHighlights in the label what matches what was typed. On by default.
loadingbooleanA search is in progress: it is announced and the list is marked busy instead of saying there is nothing.
loopbooleanWith the arrows, from the last option you go to the first and back. By default it stays at the ends.
noResultsMessagestring—
onOpenChange(open: boolean) => void—
onSearchChange(text: string) => voidCalled with the typed text on every keystroke, and with `''` when the palette closes. Use it to search on the server.
openboolean—
placeholderstring—
searchModeCommandSearchModeHow the typed text is searched: exact, fuzzy or unfiltered (results coming from the server).
shortcutstring | nullShortcut that opens it, in the `KeyboardEvent.key` format together with Ctrl or Cmd. `null` leaves it without its own shortcut (whoever uses it opens it).
size"sm" | "md" | "lg"The width of the palette: 28, 36 or 44 rem.
titlestring—

It also accepts the attributes of the element it renders: className, style, data-*, aria-* and its events.

CSS variables

These CSS variables are how you adapt it. Declare them on the component's selector, not on :root: the stylesheet redeclares them in the rule itself, and a declaration on the same element always wins.

VariableDefault
--amset-palette-width36rem
--amset-palette-height28rem
--amset-palette-radiusvar(--amset-radius-lg)

How to change it

.amset-palette {
  --amset-palette-width: ...;
  --amset-palette-height: ...;
}

In the test bench

Every state and variant, to try one at a time, on its page at storybook.amsetui.com: