8.7k

Command

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

API Reference

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}.

CommandDialog

A wrapper component that provides the dialog root functionality. It's an alias for Dialog.Root from Base UI.

CommandDialogTrigger

The trigger button that opens the command dialog. Renders as a button by default.

CommandDialogPopup

The popup content container that displays the command palette inside a dialog.

CommandInput

The search input field with an integrated search icon. Automatically includes a search icon via startAddon and is sized to lg by default.

CommandList

A scrollable container for command items. It wraps AutocompleteList with scroll functionality.

CommandPanel

A container component that provides styling for command content outside of dialogs. Useful when building standalone command interfaces with a bordered, elevated appearance.

CommandEmpty

Displays a message when no results are found.

CommandGroup

Groups related command items together. Wraps AutocompleteGroup.

CommandGroupLabel

Displays a label for a command group.

CommandItem

An individual selectable command item. Extends AutocompleteItem.

CommandSeparator

A visual separator between command groups or items. Includes default vertical spacing with my-2 className.

CommandShortcut

Displays keyboard shortcuts in a styled span element.

CommandFooter

A footer section for displaying hints or additional keyboard shortcuts. Renders as a styled div with padding and border.

CommandCollection

Used within CommandGroup to wrap items when using grouped data. It's an alias for AutocompleteCollection from the Autocomplete component.

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>

coss.com ui

Built by and for the team of Cal.com, Inc. — the leading commercial open source company (“coss”).