suiss/ui · Component
Command
A searchable menu of commands, used inline or as a command palette in a dialog.
Command is an open-source component from suiss/ui for React. The @esuiss/ui CLI copies its source into your project, so you own the code.
Command is built on cmdk. The Command root filters and orders its items as people type into CommandInput. CommandList holds the results: CommandGroup sections with a heading, CommandItem rows, CommandSeparator dividers, and a CommandEmpty message shown when nothing matches. CommandShortcut displays a keyboard shortcut at the end of an item.
CommandDialog places a Command in a modal dialog for a command palette. Use a command menu for actions and navigation that people search by name. To pick a value for a form field, use a combobox instead.
Installation
CLI
Works in any project with a components.json. See Installation for the one-line registry setup. Or use the suiss/ui CLI:
Manual
- Install the dependencies and add the
cnhelper:
-
Add the pieces it builds on: Dialog,
use-platform. -
Copy the source into
components/ui/command.tsx:
- Update the import paths to match your project.
Usage
Compose CommandInput and CommandList inside Command, and put groups and items in the list. For a palette, wrap the Command in CommandDialog and control it with open and onOpenChange. Respond to a chosen item with its onSelect callback, and set disabled on items that are unavailable.
Set mod on CommandShortcut to prefix the shortcut with the platform modifier key: ⌘ on Apple platforms and Ctrl+ elsewhere.
Examples
Empty
A command menu with no items that always shows its empty message.
Dialog
A command palette in a dialog, opened from a button, that closes with Escape and returns focus to the button.
Dialog open
A command palette that is open when the page loads.
API Reference
Command
The root that filters and manages the items.
Also accepts the props of React.ComponentProps<typeof CommandPrimitive>.
CommandDialog
A modal dialog that presents a command menu as a palette.
| Prop | Type | Default | Description |
|---|---|---|---|
title | string | "Command Palette" | The dialog's accessible title. It is visually hidden. |
description | string | "Search for a command to run..." | The dialog's accessible description. It is visually hidden. |
className | string | - | Classes applied to the dialog's content panel. |
showCloseButton | boolean | false | Shows the dialog's close button. |
children | React.ReactNode | Required | The dialog's content, typically a Command. |
Also accepts the props of React.ComponentProps<typeof Dialog>.
CommandInput
The search field, shown with a magnifying glass icon.
Also accepts the props of React.ComponentProps<typeof CommandPrimitive.Input>.
CommandList
The scrollable container for results.
Also accepts the props of React.ComponentProps<typeof CommandPrimitive.List>.
CommandEmpty
Content shown when no items match the search.
Also accepts the props of React.ComponentProps<typeof CommandPrimitive.Empty>.
CommandGroup
A section of related items with an optional heading.
Also accepts the props of React.ComponentProps<typeof CommandPrimitive.Group>.
CommandSeparator
A divider between groups.
Also accepts the props of React.ComponentProps<typeof CommandPrimitive.Separator>.
CommandItem
A selectable command.
Also accepts the props of React.ComponentProps<typeof CommandPrimitive.Item>.
CommandShortcut
Displays a keyboard shortcut at the end of an item.
| Prop | Type | Default | Description |
|---|---|---|---|
mod | boolean | - | Prefixes the shortcut with the platform modifier key, ⌘ on Apple platforms and Ctrl+ elsewhere. |
Also accepts the props of React.ComponentProps<"span">.
Accessibility
- The input has the
comboboxrole and items have theoptionrole. The highlighted item setsaria-selectedand disabled items setaria-disabled. - The first item is highlighted by default. Arrow keys move the highlight and skip disabled items; Enter selects the highlighted item.
CommandDialogrenders adialognamed by itstitle, moves focus inside when it opens, closes with Escape, and returns focus to the element that opened it.CommandShortcutis hidden on small screens.
Notes
- Styling hooks:
data-slotvaluescommand,command-input-wrapper,command-input,command-list,command-empty,command-group,command-separator,command-itemandcommand-shortcut. - The highlighted item gets
data-selectedand is filled with theprimarycolor. Disabled items getdata-disabled="true". - An item with
data-checked="true"shows a check mark, unless it contains aCommandShortcut. - Inside a dialog,
Commanddrops its own background and rounded corners. The palette uses a translucent, blurredpopoverbackground.