Skip to content

Command

A command palette component built with cmdk and Dialog for searching and executing commands.

API Reference

Installation

pnpm dlx shadcn@latest add @frontal-labs/command

Usage

import {
  Command,
  CommandDialog,
  CommandEmpty,
  CommandGroup,
  CommandInput,
  CommandItem,
  CommandList,
  CommandSeparator,
  CommandShortcut,
} from "@/components/ui/command";

CommandDialog renders the dialog itself, so it is controlled with open and onOpenChange. Compose the palette from Command and its parts inside it:

const [open, setOpen] = useState(false);
 
return (
  <>
    <Button onClick={() => setOpen(true)} variant="outline">
      Open Command Palette
    </Button>
 
    <CommandDialog onOpenChange={setOpen} open={open}>
      <Command>
        <CommandInput placeholder="Search..." />
        <CommandList>
          <CommandEmpty>No results found.</CommandEmpty>
          <CommandItem value="linear">Linear</CommandItem>
          <CommandItem value="figma">Figma</CommandItem>
          <CommandItem value="slack">Slack</CommandItem>
        </CommandList>
      </Command>
    </CommandDialog>
  </>
);

API Reference

Command

The root component. It's a styled wrapper around cmdk's Command, which owns the filtering and keyboard navigation.

CommandDialog

Renders the command palette inside a Dialog. It provides the dialog root, a visually hidden header for accessibility, and the dialog content — so it is used directly rather than composed with a separate trigger and popup. Open it from any button by controlling open.

CommandInput

The search input field with an integrated search icon.

CommandList

A scrollable container for command items.

CommandEmpty

Displays a message when no results are found. Render it inside CommandList.

CommandGroup

Groups related command items together. Pass heading to label the group.

CommandItem

An individual selectable command item.

CommandSeparator

A visual separator between command groups or items.

CommandShortcut

Displays keyboard shortcuts in a styled span element.

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

Use CommandGroup with a heading for each section, and CommandSeparator between them:

import { Fragment } from "react"
 
const groups = [
  {
    heading: "Suggestions",
    items: [
      { value: "linear", label: "Linear" },
      { value: "figma", label: "Figma" },
    ],
  },
  {
    heading: "Commands",
    items: [
      { value: "clipboard", label: "Clipboard History" },
      { value: "settings", label: "System Preferences" },
    ],
  },
]
 
<Command>
  <CommandInput placeholder="Search..." />
  <CommandList>
    <CommandEmpty>No results found.</CommandEmpty>
    {groups.map((group, index) => (
      <Fragment key={group.heading}>
        <CommandGroup heading={group.heading}>
          {group.items.map((item) => (
            <CommandItem key={item.value} value={item.value}>
              {item.label}
            </CommandItem>
          ))}
        </CommandGroup>
        {index < groups.length - 1 && <CommandSeparator />}
      </Fragment>
    ))}
  </CommandList>
</Command>

With Shortcuts

CommandShortcut aligns a keyboard hint to the end of an item:

<CommandItem value="new-file">
  New File
  <CommandShortcut>⌘N</CommandShortcut>
</CommandItem>

Standalone Command (Without Dialog)

You can use the Command component without a dialog wrapper:

<Command className="rounded-md ring-1 ring-foreground/10">
  <CommandInput placeholder="Type a command..." />
  <CommandList>
    <CommandEmpty>No results found.</CommandEmpty>
    <CommandItem value="linear">Linear</CommandItem>
    <CommandItem value="figma">Figma</CommandItem>
  </CommandList>
</Command>