Command
A searchable list of commands, inline or in a dialog.
Installation
npx shadcn@latest add @oakoss/commandThis also installs Dialog and Input Group, and the item parts it shares with Dropdown Menu.
Usage
import {
Command,
CommandInput,
CommandItem,
CommandList,
} from '#/components/ui/overlays/command';
<Command>
<CommandInput placeholder="Type a command" />
<CommandList aria-label="Commands" onAction={(key) => run(key)}>
<CommandItem id="new" keywords={['create']} shortcut="⌘N">
New file
</CommandItem>
</CommandList>
</Command>;What's typed filters the list, ignoring case and accents, and matches an
item's label and its keywords. Items take Dropdown Menu's item props:
shortcut, description, intent and href. CommandGroup sections the
list under a CommandHeader, each section after the first drawing a line
above it.
Examples
In a dialog
CommandDialog puts a command in a modal, named by title and focused on
the input when it opens. This demo also opens it with ⌘J or Ctrl + J
while it's on the page; the library binds no shortcut itself, so an app picks
one that its own keys don't use.
Descriptions and links
description adds a line under the label, and href makes an item a link.
Accessibility
React Aria links a search field to a menu of results:
- Focus: focus stays in the input. The highlighted item is the input's
aria-activedescendant, so screen readers read it as the arrow keys move. - Names: the input is named
Searchunless you name it witharia-labeloraria-labelledby, and the list by its ownaria-label. A group is named by itsCommandHeader, and a shortcut or description describes its item. - Results: items are
menuitems, since they run commands. When nothing matches, the list showsemptyLabel.
In Windows High Contrast the highlighted item takes the system's selection colors.
| Key | Action |
|---|---|
| Typing | Filters the list |
| ↓, ↑ | Moves the highlight through the results |
Enter | Runs the highlighted command |
Escape | Clears the search, then closes a CommandDialog |
API
Command
A div around React Aria's
Autocomplete, which links
the input to the list.
Props
Prop
Type
Also accepts all React Aria Autocomplete props,
such as a controlled inputValue and a filter that replaces the default.
CommandList
Extends React Aria's Menu. It
scrolls past 18rem.
Props
Prop
Type
Also accepts all React Aria Menu props,
such as onAction and items.
CommandItem
Dropdown Menu's item, with
keywords.
Props
Prop
Type
Data attributes
| Attribute | Present when |
|---|---|
data-slot | Always command-item |
data-intent | Always: neutral or destructive |
data-focused | The item is highlighted |
data-selected | The item is checked in a selection group |
data-selection-mode | The item is in a selection group: single, multiple |
data-disabled | The item is disabled |
CSS variables
| Variable | Set by | Used for |
|---|---|---|
--menu-text | intent="destructive" | The item's text |
--menu-subtle | intent="destructive" | The highlighted item's fill |
CommandDialog
Dialog, with no close button and the panel
nearer the top from sm up.
Props
Prop
Type
Other parts
| Part | Extends | data-slot |
|---|---|---|
CommandInput | React Aria's SearchField, with an InputGroup | command-input-wrapper |
CommandGroup | React Aria's MenuSection | command-group |
CommandHeader | React Aria's Header, with inset | command-label |
CommandSeparator | React Aria's Separator | command-separator |
CommandInput's input is command-input, and Command is command.
CommandInput takes React Aria's SearchField props except value and
defaultValue: Command owns what's typed, through inputValue,
defaultInputValue and onInputChange.