Skip to Content

Command

Fast, composable command menu with search, groups and shortcuts.

import {
  Command,
  CommandEmpty,
  CommandGroup,
  CommandInput,
  CommandItem,
  CommandList,
  CommandSeparator,
} from "@dapplelight/signet-ui/components/command"
import { CreditCardIcon, FileTextIcon, PlusIcon, SettingsIcon, UserIcon } from "lucide-react"

export function Demo() {
  return (
    <Command className="max-w-sm rounded-lg border">
      <CommandInput placeholder="Type a command or search..." />
      <CommandList>
        <CommandEmpty>No results found.</CommandEmpty>
        <CommandGroup heading="Actions">
          <CommandItem value="new-invoice"><PlusIcon />New invoice</CommandItem>
          <CommandItem value="invoices"><FileTextIcon />View invoices</CommandItem>
        </CommandGroup>
        <CommandSeparator />
        <CommandGroup heading="Settings">
          <CommandItem value="profile"><UserIcon />Profile</CommandItem>
          <CommandItem value="billing"><CreditCardIcon />Billing</CommandItem>
          <CommandItem value="settings"><SettingsIcon />Settings</CommandItem>
        </CommandGroup>
      </CommandList>
    </Command>
  )
}

Installation

npm i @dapplelight/signet-ui

Usage

import {
  Command,
  CommandDialog,
  CommandEmpty,
  CommandGroup,
  CommandInput,
  CommandItem,
  CommandList,
  CommandSeparator,
  CommandShortcut,
} from "@dapplelight/signet-ui/components/command"
import { run } from "./commands"

<Command onAction={(value) => run(value)}>
  <CommandInput placeholder="Search..." />
  <CommandList>
    <CommandEmpty>No results.</CommandEmpty>
    <CommandGroup heading="Actions">
      <CommandItem value="new-invoice">New invoice</CommandItem>
    </CommandGroup>
  </CommandList>
</Command>

Examples

Shortcuts

import { CommandItem, CommandShortcut } from "@dapplelight/signet-ui/components/command"
import { UserIcon } from "lucide-react"

<CommandItem value="profile" shortcut="mod+p">
  <UserIcon />Profile<CommandShortcut />
</CommandItem>

Dialog

Ran: Nothing yet
import {
  CommandDialog,
  CommandEmpty,
  CommandGroup,
  CommandInput,
  CommandItem,
  CommandList,
} from "@dapplelight/signet-ui/components/command"
import { FileTextIcon, PlusIcon } from "lucide-react"
import * as React from "react"
import { run } from "./commands"

const [open, setOpen] = React.useState(false)

<CommandDialog open={open} onOpenChange={setOpen} onAction={(value) => run(value)}>
  <CommandInput placeholder="Type a command or search..." />
  <CommandList>
    <CommandEmpty>No results found.</CommandEmpty>
    <CommandGroup heading="Actions">
      <CommandItem value="new-invoice"><PlusIcon />New invoice</CommandItem>
      <CommandItem value="invoices"><FileTextIcon />View invoices</CommandItem>
    </CommandGroup>
  </CommandList>
</CommandDialog>

API Reference

Command

Typing filters and ranks, arrow keys move the highlight while focus stays in the input, Enter runs.

PropTypeDefaultDescription
onAction(value, details) => unknown-Runs when a command runs. Return a Promise to mark it pending.
filterfuzzy | contains | startsWith | function | falsefuzzyHow typing narrows the list.
query / onQueryChangestring / (query, details) => void-Control the search text.
value / onValueChangestring | null / (value, details) => void-Control the highlighted command.
defaultQuery / defaultValuestring / string | null-Initial search text and highlighted command when uncontrolled.
autoHighlightbooleantrueHighlight the first or best-matching command so Enter runs it.
loopbooleantrueWrap arrow keys at the ends.
itemsCommandData-Commands as data instead of children.
emptyItemsCommandData-Shown instead of items while the search is empty, such as recent commands. Needs items.
status"idle" | "loading" | "error""idle"Whether results are loading or failed.
onLoadMore() => void-Runs when the highlight reaches the last command.
pendingAnnouncementstring"Loading"What screen readers hear when an action stays pending.
classNamestring-Merged last.

CommandDialog

A Command inside a Dialog, toggled with Cmd+K or Ctrl+K. Takes every Command prop plus:

PropTypeDefaultDescription
openboolean-Controlled open state.
defaultOpenbooleanfalseInitial state when uncontrolled.
onOpenChange(open: boolean) => void-Runs when it opens or closes.
shortcutstring | null"mod+k"Toggles it from anywhere on the page. null turns it off.
titleReactNode"Command palette"Name for screen readers.
descriptionReactNode"Search for a command to run."Description for screen readers.
closeOnActionbooleantrueClose once a command runs.
contentClassNamestring-Classes for the dialog surface.

On a phone it becomes a bottom sheet.

CommandInput

PropTypeDefaultDescription
iconReactNodesearch iconReplaces the icon. null hides it.
wrapperClassNamestring-Classes for the row around the input.
classNamestring-Merged onto the input.

CommandItem

PropTypeDefaultDescription
valuestringgeneratedPassed to onAction. Identifies the command.
labelstring-Text the search matches when the content is not plain text.
keywordsstring[]-Extra words the search matches.
shortcutstring-Keys such as "mod+p" that run it while it is mounted.
disabledbooleanfalseKeeps it from running.
onSelect(event) => unknown-Runs before onAction. preventDefault() skips it.

CommandGroup

PropTypeDefaultDescription
headingReactNode-Label shown above the group.

CommandList, CommandEmpty, CommandSeparator, CommandShortcut

CommandList holds the items and scrolls. CommandEmpty shows only when nothing matches. CommandShortcut shows its item's shortcut as Cmd+P on Apple platforms and Ctrl+P elsewhere. All take className.