Command
A command palette built with Dialog and Autocomplete for searching and running commands.
"use client";
import { useState } from "react";
import { Button } from "@/components/ui/button";
import {
Command,
CommandCollection,
CommandDialog,
CommandDialogPopup,
CommandDialogTrigger,
CommandEmpty,
CommandGroup,
CommandGroupLabel,
CommandInput,
CommandItem,
CommandList,
CommandPanel,
} from "@/components/ui/command";
interface Item {
value: string;
label: string;
}
interface Group {
value: string;
items: Item[];
}
const groups: Group[] = [
{
items: [
{ label: "Calendar", value: "calendar" },
{ label: "Search Emoji", value: "search-emoji" },
{ label: "Calculator", value: "calculator" },
],
value: "Suggestions",
},
{
items: [
{ label: "Profile", value: "profile" },
{ label: "Billing", value: "billing" },
{ label: "Settings", value: "settings" },
],
value: "Account",
},
];
export default function Particle() {
const [open, setOpen] = useState(false);
return (
<CommandDialog onOpenChange={setOpen} open={open}>
<CommandDialogTrigger render={<Button variant="outline" />}>
Open Command
</CommandDialogTrigger>
<CommandDialogPopup aria-label="Command palette">
<Command items={groups}>
<CommandInput placeholder="Type a command or search…" />
<CommandPanel>
<CommandEmpty>No results found.</CommandEmpty>
<CommandList>
{(group: Group) => (
<CommandGroup items={group.items} key={group.value}>
<CommandGroupLabel>{group.value}</CommandGroupLabel>
<CommandCollection>
{(item: Item) => (
<CommandItem
key={item.value}
onClick={() => setOpen(false)}
value={item.value}
>
{item.label}
</CommandItem>
)}
</CommandCollection>
</CommandGroup>
)}
</CommandList>
</CommandPanel>
</Command>
</CommandDialogPopup>
</CommandDialog>
);
}
Installation
pnpm dlx shadcn@latest add @taut-ui/command
Usage
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"const items = [
{ value: "calendar", label: "Calendar" },
{ value: "search-emoji", label: "Search Emoji" },
{ value: "calculator", label: "Calculator" },
]
<CommandDialog>
<CommandDialogTrigger render={<Button variant="outline" />}>
Open Command
</CommandDialogTrigger>
<CommandDialogPopup aria-label="Command palette">
<Command items={items}>
<CommandInput placeholder="Type a command or search…" />
<CommandPanel>
<CommandEmpty>No results found.</CommandEmpty>
<CommandList>
{(item) => (
<CommandItem key={item.value} value={item.value}>
{item.label}
</CommandItem>
)}
</CommandList>
</CommandPanel>
</Command>
</CommandDialogPopup>
</CommandDialog>Examples
With Keyboard Shortcut
"use client";
import { useEffect, useState } from "react";
import { Button } from "@/components/ui/button";
import {
Command,
CommandDialog,
CommandDialogPopup,
CommandDialogTrigger,
CommandEmpty,
CommandInput,
CommandItem,
CommandList,
CommandPanel,
CommandShortcut,
} from "@/components/ui/command";
import { Kbd, KbdGroup } from "@/components/ui/kbd";
interface Item {
value: string;
label: string;
shortcut: string;
}
const items: Item[] = [
{ label: "New File", shortcut: "⌘N", value: "new-file" },
{ label: "Open Folder", shortcut: "⌘O", value: "open-folder" },
{ label: "Save", shortcut: "⌘S", value: "save" },
{ label: "Toggle Sidebar", shortcut: "⌘B", value: "toggle-sidebar" },
];
export default function Particle() {
const [open, setOpen] = useState(false);
useEffect(() => {
const down = (e: KeyboardEvent): void => {
if (e.key === "j" && (e.metaKey || e.ctrlKey)) {
e.preventDefault();
setOpen((prev) => !prev);
}
};
document.addEventListener("keydown", down);
return () => document.removeEventListener("keydown", down);
}, []);
return (
<CommandDialog onOpenChange={setOpen} open={open}>
<CommandDialogTrigger render={<Button variant="outline" />}>
Search
<KbdGroup>
<Kbd>⌘</Kbd>
<Kbd>J</Kbd>
</KbdGroup>
</CommandDialogTrigger>
<CommandDialogPopup aria-label="Command palette">
<Command items={items}>
<CommandInput placeholder="Search commands…" />
<CommandPanel>
<CommandEmpty>No results found.</CommandEmpty>
<CommandList>
{(item: Item) => (
<CommandItem
key={item.value}
onClick={() => setOpen(false)}
value={item.value}
>
<span className="flex-1">{item.label}</span>
<CommandShortcut>{item.shortcut}</CommandShortcut>
</CommandItem>
)}
</CommandList>
</CommandPanel>
</Command>
</CommandDialogPopup>
</CommandDialog>
);
}
API Reference
Command
The root component that wraps Autocomplete with command palette defaults: inline, open={true}, autoHighlight="always", and keepHighlight={true}.
| Prop | Type | Default | Description |
|---|---|---|---|
items | readonly unknown[] | - | Items (or groups) used to filter and render the list |
open | boolean | true | Controls whether the command is open |
autoHighlight | boolean | "always" | "always" | Controls automatic highlighting of items |
keepHighlight | boolean | true | Keeps the highlighted item while the list updates |
...props | Base UI Autocomplete Root props | - | All other Autocomplete props are supported |
CommandDialog
Alias for Dialog.Root from Base UI.
| Prop | Type | Description |
|---|---|---|
open | boolean | Controls whether the dialog is open |
onOpenChange | (open: boolean) => void | Callback fired when the open state changes |
...props | Base UI Dialog Root props | All standard dialog attributes are supported |
CommandDialogTrigger
The trigger button that opens the command dialog.
| Prop | Type | Description |
|---|---|---|
render | React.ReactElement | Element to render as the trigger |
className | string | Additional CSS classes |
...props | Base UI Dialog Trigger props | All standard dialog trigger attributes are supported |
CommandDialogPopup
The popup container that renders the command palette inside a dialog portal, backdrop, and viewport.
| Prop | Type | Description |
|---|---|---|
className | string | Additional CSS classes |
portalProps | Dialog.Portal.Props | Props forwarded to the internal portal |
...props | Base UI Dialog Popup props | All standard dialog popup attributes are supported |
CommandInput
The search input field with an integrated search icon.
| Prop | Type | Default | Description |
|---|---|---|---|
placeholder | string | - | Placeholder text for the input |
variant | "filled" | "outline" | "outline" | Visual variant of the input |
className | string | - | Additional CSS classes |
...props | Base UI Autocomplete Input props | - | All standard autocomplete input attributes are supported |
CommandPanel
The bordered container that wraps CommandList and CommandEmpty.
| Prop | Type | Description |
|---|---|---|
className | string | Additional CSS classes |
...props | React.ComponentProps<"div"> | All standard div attributes are supported |
CommandList
A scrollable container for command items. Wraps AutocompleteList.
| Prop | Type | Description |
|---|---|---|
className | string | Additional CSS classes |
...props | Base UI Autocomplete List props | All standard autocomplete list attributes are supported |
CommandEmpty
Displays a message when no results are found.
| Prop | Type | Description |
|---|---|---|
className | string | Additional CSS classes |
...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 |
...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 |
...props | Base UI Autocomplete Group Label props | All standard autocomplete group label attributes are supported |
CommandCollection
Used within CommandGroup to render grouped items. Alias for AutocompleteCollection.
| Prop | Type | Description |
|---|---|---|
...props | Base UI Autocomplete Collection props | All standard autocomplete collection attributes are supported |
CommandItem
An individual selectable command item. Wraps AutocompleteItem.
| Prop | Type | Description |
|---|---|---|
value | unknown | The value of the item |
className | string | Additional CSS classes |
...props | Base UI Autocomplete Item props | All standard autocomplete item attributes are supported |
CommandSeparator
A visual divider between command groups or items.
| Prop | Type | Description |
|---|---|---|
className | string | Additional CSS classes |
...props | Base UI Autocomplete Separator props | All standard autocomplete separator attributes are supported |
CommandShortcut
Displays keyboard shortcuts inside a CommandItem.
| Prop | Type | Description |
|---|---|---|
className | string | Additional CSS classes |
...props | React.ComponentProps<"kbd"> | All standard kbd attributes are supported |
CommandFooter
A footer section for displaying hints or keyboard shortcuts below CommandPanel.
| Prop | Type | Description |
|---|---|---|
className | string | Additional CSS classes |
...props | React.ComponentProps<"div"> | All standard div attributes are supported |