- Accordion
- Alert
- Alert Dialog
- Autocomplete
- Avatar
- Badge
- Breadcrumb
- Button
- Card
- Checkbox
- Checkbox Group
- Collapsible
- Combobox
- CommandNew
- Dialog
- Empty
- Field
- Fieldset
- Form
- Frame
- Group
- Input
- Input Group
- Kbd
- Label
- Menu
- Meter
- Number Field
- Pagination
- Popover
- Preview Card
- Progress
- Radio Group
- Scroll Area
- Select
- Separator
- Sheet
- Skeleton
- Slider
- Spinner
- Switch
- Table
- Tabs
- Textarea
- Toast
- Toggle
- Toggle Group
- Toolbar
- Tooltip
Command
A command palette component built with Dialog and Autocomplete for searching and executing commands.
"use client";
import { ArrowDownIcon, ArrowUpIcon, CornerDownLeftIcon } from "lucide-react";
import * as React from "react";
import { Button } from "@/components/ui/button";
import {
Command,
CommandCollection,
CommandDialog,
CommandDialogPopup,
CommandDialogTrigger,
CommandEmpty,
CommandFooter,
CommandGroup,
CommandGroupLabel,
CommandInput,
CommandItem,
CommandList,
CommandPanel,
CommandSeparator,
CommandShortcut,
} from "@/components/ui/command";
import { Kbd, KbdGroup } from "@/components/ui/kbd";
export interface Item {
value: string;
label: string;
shortcut?: string;
}
export interface Group {
value: string;
items: Item[];
}
export const suggestions: Item[] = [
{ label: "Linear", shortcut: "⌘L", value: "linear" },
{ label: "Figma", shortcut: "⌘F", value: "figma" },
{ label: "Slack", shortcut: "⌘S", value: "slack" },
{ label: "YouTube", shortcut: "⌘Y", value: "youtube" },
{ label: "Raycast", shortcut: "⌘R", value: "raycast" },
];
export const commands: Item[] = [
{ label: "Clipboard History", shortcut: "⌘⇧C", value: "clipboard-history" },
{ label: "Import Extension", shortcut: "⌘I", value: "import-extension" },
{ label: "Create Snippet", shortcut: "⌘N", value: "create-snippet" },
{ label: "System Preferences", shortcut: "⌘,", value: "system-preferences" },
{ label: "Window Management", shortcut: "⌘⇧W", value: "window-management" },
];
export const groupedItems: Group[] = [
{ items: suggestions, value: "Suggestions" },
{ items: commands, value: "Commands" },
];
export default function Particle() {
const [open, setOpen] = React.useState(false);
function handleItemClick(_item: Item) {
setOpen(false);
}
React.useEffect(() => {
const down = (e: KeyboardEvent) => {
if (e.key === "j" && (e.metaKey || e.ctrlKey)) {
e.preventDefault();
setOpen((open) => !open);
console.log("down");
}
};
document.addEventListener("keydown", down);
return () => document.removeEventListener("keydown", down);
}, []);
return (
<CommandDialog onOpenChange={setOpen} open={open}>
<CommandDialogTrigger render={<Button variant="outline" />}>
Open Command Palette
<Kbd>⌘J</Kbd>
</CommandDialogTrigger>
<CommandDialogPopup>
<Command items={groupedItems}>
<CommandInput placeholder="Search for apps and commands..." />
<CommandPanel>
<CommandEmpty>No results found.</CommandEmpty>
<CommandList>
{(group: Group, _index: number) => (
<React.Fragment key={group.value}>
<CommandGroup items={group.items}>
<CommandGroupLabel>{group.value}</CommandGroupLabel>
<CommandCollection>
{(item: Item) => (
<CommandItem
key={item.value}
onClick={() => handleItemClick(item)}
value={item.value}
>
<span className="flex-1">{item.label}</span>
{item.shortcut && (
<CommandShortcut>{item.shortcut}</CommandShortcut>
)}
</CommandItem>
)}
</CommandCollection>
</CommandGroup>
<CommandSeparator />
</React.Fragment>
)}
</CommandList>
</CommandPanel>
<CommandFooter>
<div className="flex items-center gap-4">
<div className="flex items-center gap-2">
<KbdGroup>
<Kbd>
<ArrowUpIcon />
</Kbd>
<Kbd>
<ArrowDownIcon />
</Kbd>
</KbdGroup>
<span>Navigate</span>
</div>
<div className="flex items-center gap-2">
<Kbd>
<CornerDownLeftIcon />
</Kbd>
<span>Open</span>
</div>
</div>
<div className="flex items-center gap-2">
<KbdGroup>
<Kbd>⌘</Kbd>
<Kbd>K</Kbd>
</KbdGroup>
<span>Close</span>
</div>
</CommandFooter>
</Command>
</CommandDialogPopup>
</CommandDialog>
);
}
Installation
pnpm dlx shadcn@latest add @coss/command
Usage
import {
Command,
CommandCollection,
CommandDialog,
CommandDialogPopup,
CommandDialogTrigger,
CommandEmpty,
CommandFooter,
CommandGroup,
CommandGroupLabel,
CommandInput,
CommandItem,
CommandList,
CommandPanel,
CommandSeparator,
CommandShortcut,
} from "@/components/ui/command"
import { Button } from "@/components/ui/button"const items = [
{ value: "linear", label: "Linear" },
{ value: "figma", label: "Figma" },
{ value: "slack", label: "Slack" },
]
<CommandDialog>
<CommandDialogTrigger render={<Button variant="outline" />}>
Open Command Palette
</CommandDialogTrigger>
<CommandDialogPopup>
<Command items={items}>
<CommandInput placeholder="Search..." />
<CommandEmpty>No results found.</CommandEmpty>
<CommandList>
{(item) => (
<CommandItem key={item.value} value={item.value}>
{item.label}
</CommandItem>
)}
</CommandList>
</Command>
</CommandDialogPopup>
</CommandDialog>API Reference
Command
The root component that wraps the autocomplete functionality. It's an alias for Autocomplete.Root with sensible defaults for command palette behavior: autoHighlight="always", keepHighlight={true}, and open={true}.
| Prop | Type | Default | Description |
|---|---|---|---|
items | readonly unknown[] | - | The array of items to display in the command |
open | boolean | true | Controls whether the command is open |
autoHighlight | boolean | "always" | "always" | Controls automatic highlighting of items |
keepHighlight | boolean | true | Whether to maintain highlight state |
...props | React.ComponentProps<typeof Command> | - | All Base UI Autocomplete props are supported |
CommandDialog
A wrapper component that provides the dialog root functionality. It's an alias for Dialog.Root from Base UI.
| Prop | Type | Description |
|---|---|---|
open | boolean | Controls whether the dialog is open |
onOpenChange | function | Callback fired when the open state changes |
...props | Base UI Dialog props | All standard dialog attributes are supported |
CommandDialogTrigger
The trigger button that opens the command dialog. Renders as a button by default.
| Prop | Type | Description |
|---|---|---|
render | React.ReactElement | Element to render as the trigger |
className | string | Additional CSS classes to apply to the component |
...props | Base UI Dialog Trigger props | All standard dialog trigger attributes are supported |
CommandDialogPopup
The popup content container that displays the command palette inside a dialog.
| Prop | Type | Description |
|---|---|---|
className | string | Additional CSS classes to apply to the component |
...props | Base UI Dialog Popup props | All standard dialog popup attributes are supported |
CommandInput
The search input field with an integrated search icon. Automatically includes a search icon via startAddon and is sized to lg by default.
| Prop | Type | Description |
|---|---|---|
placeholder | string | The placeholder text for the input |
className | string | Additional CSS classes to apply to the component |
...props | Base UI Autocomplete Input props | All standard autocomplete input attributes are supported |
CommandList
A scrollable container for command items. It wraps AutocompleteList with scroll functionality.
| Prop | Type | Description |
|---|---|---|
className | string | Additional CSS classes to apply to the component |
...props | Base UI Autocomplete List props | All standard autocomplete list attributes are supported |
CommandPanel
A container component that provides styling for command content outside of dialogs. Useful when building standalone command interfaces with a bordered, elevated appearance.
| Prop | Type | Description |
|---|---|---|
className | string | Additional CSS classes to apply to the component |
...props | React.ComponentProps<'div'> | All standard div attributes are supported |
CommandEmpty
Displays a message when no results are found.
| Prop | Type | Description |
|---|---|---|
className | string | Additional CSS classes to apply to the component |
...props | Base UI Autocomplete Empty props | All standard autocomplete empty attributes are supported |
CommandGroup
Groups related command items together. Wraps AutocompleteGroup.
| Prop | Type | Description |
|---|---|---|
items | readonly unknown[] | The array of items in this group |
className | string | Additional CSS classes to apply to the component |
...props | Base UI Autocomplete Group props | All standard autocomplete group attributes are supported |
CommandGroupLabel
Displays a label for a command group.
| Prop | Type | Description |
|---|---|---|
className | string | Additional CSS classes to apply to the component |
...props | Base UI Autocomplete Group Label props | All standard autocomplete group label attributes are supported |
CommandItem
An individual selectable command item. Extends AutocompleteItem.
| Prop | Type | Description |
|---|---|---|
value | unknown | The value of the item |
className | string | Additional CSS classes to apply to the component |
...props | Base UI Autocomplete Item props | All standard autocomplete item attributes are supported |
CommandSeparator
A visual separator between command groups or items. Includes default vertical spacing with my-2 className.
| Prop | Type | Description |
|---|---|---|
className | string | Additional CSS classes to apply to the component (default includes my-2) |
...props | Base UI Autocomplete Separator props | All standard autocomplete separator attributes are supported |
CommandShortcut
Displays keyboard shortcuts in a styled span element.
| Prop | Type | Description |
|---|---|---|
className | string | Additional CSS classes to apply to the component |
...props | React.ComponentProps<'span'> | All standard span attributes are supported |
CommandFooter
A footer section for displaying hints or additional keyboard shortcuts. Renders as a styled div with padding and border.
| Prop | Type | Description |
|---|---|---|
className | string | Additional CSS classes to apply to the component |
...props | React.ComponentProps<'div'> | All standard div attributes are supported |
CommandCollection
Used within CommandGroup to wrap items when using grouped data. It's an alias for AutocompleteCollection from the Autocomplete component.
| Prop | Type | Description |
|---|---|---|
...props | Base UI Autocomplete Collection props | All standard autocomplete collection attributes are supported |
Examples
With Keyboard Shortcut
You can add a keyboard shortcut to open the command palette:
React.useEffect(() => {
const down = (e: KeyboardEvent) => {
if (e.key === "k" && (e.metaKey || e.ctrlKey)) {
e.preventDefault()
setOpen((open) => !open)
}
}
document.addEventListener("keydown", down)
return () => document.removeEventListener("keydown", down)
}, [])With Grouped Items
const groupedItems = [
{
value: "Suggestions",
items: [
{ value: "linear", label: "Linear" },
{ value: "figma", label: "Figma" },
]
},
{
value: "Commands",
items: [
{ value: "clipboard", label: "Clipboard History" },
{ value: "settings", label: "System Preferences" },
]
},
]
<Command items={groupedItems}>
<CommandInput placeholder="Search..." />
<CommandEmpty>No results found.</CommandEmpty>
<CommandList>
{(group, index) => (
<React.Fragment key={group.value}>
<CommandGroup items={group.items}>
<CommandGroupLabel>{group.value}</CommandGroupLabel>
<CommandCollection>
{(item) => (
<CommandItem key={item.value} value={item.value}>
{item.label}
</CommandItem>
)}
</CommandCollection>
</CommandGroup>
{index < groupedItems.length - 1 && <CommandSeparator />}
</React.Fragment>
)}
</CommandList>
</Command>Standalone Command (Without Dialog)
You can use the Command component without a dialog wrapper:
<Command open items={items}>
<CommandInput placeholder="Type a command..." />
<CommandEmpty>No results found.</CommandEmpty>
<CommandList>
{(item) => (
<CommandItem key={item.value} value={item.value}>
{item.label}
</CommandItem>
)}
</CommandList>
</Command>On This Page