{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "opentui/command-palette",
  "title": "Command Palette",
  "description": "VS Code–style command palette with fuzzy search",
  "dependencies": [
    "@opentui/react"
  ],
  "registryDependencies": [
    "https://termcn.dev/r/opentui/use-theme.json"
  ],
  "files": [
    {
      "path": "registry/opentui/ui/command-palette.tsx",
      "content": "/* @jsxImportSource @opentui/react */\nimport { useKeyboard } from \"@opentui/react\";\nimport React, { useState } from \"react\";\n\nimport { useTheme } from \"@/hooks/use-theme\";\n\nexport interface Command {\n  id: string;\n  label: string;\n  description?: string;\n  shortcut?: string;\n  onSelect?: () => void;\n  group?: string;\n}\n\nexport interface CommandPaletteProps {\n  commands: Command[];\n  isOpen: boolean;\n  onClose?: () => void;\n  placeholder?: string;\n  maxItems?: number;\n}\n\nconst fuzzyMatch = (str: string, query: string): boolean => {\n  if (!query) {\n    return true;\n  }\n  const s = str.toLowerCase();\n  const q = query.toLowerCase();\n  let qi = 0;\n  for (let i = 0; i < s.length && qi < q.length; i += 1) {\n    if (s[i] === q[qi]) {\n      qi += 1;\n    }\n  }\n  return qi === q.length;\n};\n\nconst fuzzyScore = (str: string, query: string): number => {\n  if (!query) {\n    return 0;\n  }\n  const s = str.toLowerCase();\n  const q = query.toLowerCase();\n  let score = 0;\n  let qi = 0;\n  let lastMatchIdx = -1;\n\n  for (let i = 0; i < s.length && qi < q.length; i += 1) {\n    if (s[i] === q[qi]) {\n      score += i - lastMatchIdx - 1;\n      lastMatchIdx = i;\n      qi += 1;\n    }\n  }\n\n  return score;\n};\n\nexport const CommandPalette = ({\n  commands,\n  isOpen,\n  onClose,\n  placeholder = \"Type a command...\",\n  maxItems = 8,\n}: CommandPaletteProps) => {\n  const theme = useTheme();\n  const [query, setQuery] = useState(\"\");\n  const [cursor, setCursor] = useState(0);\n\n  const filtered = commands\n    .filter((c) => fuzzyMatch(c.label, query))\n    .toSorted((a, b) => fuzzyScore(a.label, query) - fuzzyScore(b.label, query))\n    .slice(0, maxItems);\n\n  useKeyboard((key) => {\n    if (!isOpen) {\n      return;\n    }\n    if (key.name === \"escape\") {\n      setQuery(\"\");\n      setCursor(0);\n      onClose?.();\n      return;\n    }\n    if (key.name === \"up\") {\n      setCursor((c) => Math.max(0, c - 1));\n      return;\n    }\n    if (key.name === \"down\") {\n      setCursor((c) => Math.min(filtered.length - 1, c + 1));\n      return;\n    }\n    if (key.name === \"return\") {\n      const cmd = filtered[cursor];\n      if (cmd) {\n        cmd.onSelect?.();\n        setQuery(\"\");\n        setCursor(0);\n        onClose?.();\n      }\n      return;\n    }\n    if (key.name === \"backspace\" || key.name === \"delete\") {\n      setQuery((q) => q.slice(0, -1));\n      setCursor(0);\n      return;\n    }\n    if (key.name === \"tab\") {\n      return;\n    }\n    if (key.name.length > 1) {\n      return;\n    }\n    setQuery((q) => q + key.name);\n    setCursor(0);\n  });\n\n  if (!isOpen) {\n    return null;\n  }\n\n  const groups = new Map<string | undefined, typeof filtered>();\n  for (const cmd of filtered) {\n    const g = cmd.group;\n    if (!groups.has(g)) {\n      groups.set(g, []);\n    }\n    groups.get(g)?.push(cmd);\n  }\n\n  let flatIdx = 0;\n\n  return (\n    <box\n      flexDirection=\"column\"\n      borderStyle=\"rounded\"\n      borderColor={theme.colors.focusRing}\n      paddingLeft={1}\n      paddingRight={1}\n    >\n      <box\n        borderStyle=\"single\"\n        borderColor={theme.colors.border}\n        paddingLeft={1}\n        paddingRight={1}\n      >\n        <text fg={theme.colors.mutedForeground}>{\"⌘ \"}</text>\n        <text\n          fg={query ? theme.colors.foreground : theme.colors.mutedForeground}\n        >\n          {query || placeholder}\n        </text>\n        <text fg={theme.colors.focusRing}>█</text>\n      </box>\n\n      {filtered.length === 0 ? (\n        <box paddingLeft={1} paddingRight={1} paddingTop={0} paddingBottom={0}>\n          <text fg=\"#666\">No commands found</text>\n        </box>\n      ) : (\n        <box flexDirection=\"column\">\n          {[...groups.entries()].map(([group, cmds]) => (\n            <box key={group ?? \"_\"} flexDirection=\"column\">\n              {group && (\n                <box paddingLeft={1} paddingRight={1}>\n                  <text fg=\"#666\">\n                    <b>{group}</b>\n                  </text>\n                </box>\n              )}\n              {cmds.map((cmd) => {\n                const idx = (flatIdx += 1);\n                const isCursor = idx === cursor;\n                return (\n                  <box key={cmd.id} paddingLeft={1} paddingRight={1}>\n                    <box flexGrow={1}>\n                      <text\n                        fg={\n                          isCursor\n                            ? theme.colors.selectionForeground\n                            : theme.colors.foreground\n                        }\n                        inverse={isCursor}\n                      >\n                        {isCursor ? <b>{cmd.label}</b> : cmd.label}\n                      </text>\n                      {cmd.description && (\n                        <text fg=\"#666\">{` — ${cmd.description}`}</text>\n                      )}\n                    </box>\n                    {cmd.shortcut && (\n                      <text fg={theme.colors.accent}>{cmd.shortcut}</text>\n                    )}\n                  </box>\n                );\n              })}\n            </box>\n          ))}\n        </box>\n      )}\n\n      <text fg=\"#666\">↑↓: navigate · Enter: run · Esc: close</text>\n    </box>\n  );\n};\n",
      "type": "registry:ui",
      "target": "components/ui/command-palette.tsx"
    }
  ],
  "categories": [
    "navigation"
  ],
  "type": "registry:ui"
}