asafefe.dev
Lab

Lab/05

Command Palette

Open it with the shortcut or a click, filter as you type, and move through results with the arrow keys.

  • Motion
  • Keyboard
Click or press ⌘/Ctrl K

Installation

Add Command Palette to your project with the shadcn CLI. The files land in components/lab, ready to edit.

$ pnpm dlx shadcn@latest add @soultel/lab-command-palette

Or by URL: https://asafefe.dev/r/lab-command-palette.json

Brings along

  • motion
  • lucide-react
  • utils
  • @soultel/use-reduced-motion-safe

Usage

example.tsx13 lines
import { CommandPalette } from "@/components/lab/command-palette"

export function Example() {
  return (
    <CommandPalette
      commands={[
        { id: "new", label: "New Project", group: "Project", shortcut: "N" },
        { id: "theme", label: "Toggle Theme", group: "UI", shortcut: "D" },
      ]}
      onSelect={(cmd) => console.log(cmd.id)}
    />
  )
}

Props

PropTypeDefaultDescription
commandsCommand[]-{ id, label, group?, shortcut?, color?, onSelect? } for each entry.
onSelect(command: Command) => void-Called with the chosen command.
open / onOpenChangeboolean / (open: boolean) => void-Optional controlled open state.
hotkeystring | null"k"Toggles with Cmd or Ctrl. null disables it.
triggerbooleantrueRender the search-bar trigger.
placeholderstring"Type a command..."Input placeholder.