Command
React Command components are used to build fast, composable, and accessible command menus. They are essential for creating command palettes, search dialogs, and navigation shortcuts that improve user productivity.
This Command component is built with React and React Aria Components, ensuring high accessibility and smooth keyboard navigation. It supports filtering, grouping, keyboard shortcuts, and can be easily integrated into modal dialogs.
Installation
Install the component using the Tailgrids CLI.
npx @tailgrids/cli add commandAnatomy
Import the component and pass the required props.
import {
Command,
CommandDialog,
CommandEmpty,
CommandGroup,
CommandInput,
CommandItem,
CommandList,
CommandSeparator,
CommandShortcut
} from "@/registry/core/command";
export function CommandDemo() {
return (
<Command className="rounded-lg border shadow-md">
<CommandInput placeholder="Type a command or search..." />
<CommandList>
<CommandEmpty>No results found.</CommandEmpty>
<CommandGroup heading="Suggestions">
<CommandItem>
<Calendar className="mr-2 h-4 w-4" />
<span>Calendar</span>
</CommandItem>
<CommandItem>
<Smile className="mr-2 h-4 w-4" />
<span>Search Emoji</span>
</CommandItem>
<CommandItem>
<Calculator className="mr-2 h-4 w-4" />
<span>Calculator</span>
</CommandItem>
</CommandGroup>
<CommandSeparator />
<CommandGroup heading="Settings">
<CommandItem>
<User className="mr-2 h-4 w-4" />
<span>Profile</span>
<CommandShortcut>⌘P</CommandShortcut>
</CommandItem>
<CommandItem>
<CreditCard className="mr-2 h-4 w-4" />
<span>Billing</span>
<CommandShortcut>⌘B</CommandShortcut>
</CommandItem>
<CommandItem>
<Settings className="mr-2 h-4 w-4" />
<span>Settings</span>
<CommandShortcut>⌘S</CommandShortcut>
</CommandItem>
</CommandGroup>
</CommandList>
</Command>
);
}Examples
Dialog
The CommandDialog component allows you to wrap the command menu in a modal dialog.
Press ⌘J
Complex Menu
An example of a more complex command menu with icons, descriptions, and grouped actions. Note the use of textValue for filtering.
File Search
A search-centric example focused on finding files and folders with metadata.
API Reference
Command
The root component of the command menu.
| Prop | Type | Default | Description |
|---|---|---|---|
className | string | - | Additional CSS classes to apply to the command menu. |
filter | function | - | Custom filter function for items. Defaults to contains with base sensitivity. |
label | string | - | Accessible label for the command menu. |
shouldFilter | boolean | true | Whether the command menu should filter items. |
See useFilter for more information on filtration.
CommandInput
The search input field for the command menu.
| Prop | Type | Default | Description |
|---|---|---|---|
className | string | - | Additional CSS classes to apply to the command input. |
placeholder | string | - | Placeholder text for the input. |
disabled | boolean | false | Disables interaction with the input. |
CommandList
The container for the command items. It automatically detects and renders CommandEmpty if no results are found.
| Prop | Type | Default | Description |
|---|---|---|---|
className | string | - | Additional CSS classes to apply to the command list. |
renderEmptyState | (items) => ReactNode | - | Custom render function for the empty state. |
CommandDialog
A command menu wrapped in a modal dialog.
| Prop | Type | Default | Description |
|---|---|---|---|
className | string | - | Additional CSS classes to apply to the command menu. |
isOpen | boolean | - | Controls the open state of the dialog. |
onOpenChange | function | - | Event handler called when the open state changes. |
NOTE: CommandDialog includes built-in keyboard shortcuts: ⌘J (Mac) or Ctrl+J (Windows/Linux) to open, and Escape to close.
CommandEmpty
Displays a message when no search results are found.
| Prop | Type | Default | Description |
|---|---|---|---|
className | string | - | Additional CSS classes to apply to the command menu. |
CommandGroup
Groups related command items under a heading.
| Prop | Type | Default | Description |
|---|---|---|---|
heading | ReactNode | - | Optional heading for the group. |
className | string | - | Additional CSS classes to apply to the command group. |
CommandItem
An individual item in the command menu.
| Prop | Type | Default | Description |
|---|---|---|---|
className | string | - | Additional CSS classes to apply to the command item. |
disabled | boolean | false | Disables the item. |
onSelect | function | - | Event handler called when the item is selected. |
value | string | - | Unique value for the item. |
textValue | string | - | [Required] The text representation used for filtering when children contain complex elements (icons, spans, etc). |
CommandShortcut
Displays a keyboard shortcut hint for a command item.
| Prop | Type | Default | Description |
|---|---|---|---|
className | string | - | Additional CSS classes to apply to the command menu. |
CommandSeparator
A visual divider between items or groups.
| Prop | Type | Default | Description |
|---|---|---|---|
className | string | - | Additional CSS classes to apply to the command menu. |
Accessibility
- Keyboard Navigation: Use arrow keys to navigate between items.
- Filtering: Items are automatically filtered based on input.
- Screen Reader Support: Properly labeled with ARIA attributes.