{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "command-menu",
  "type": "registry:ui",
  "title": "Command Menu",
  "author": "Rishabh Bose <https://theb0se.vercel.app/>",
  "description": "Spotlight-style Cmd+K command palette portaled above the viewport with grouped actions, live keyword filtering, and full keyboard navigation.",
  "dependencies": [
    "framer-motion",
    "lucide-react",
    "clsx",
    "tailwind-merge"
  ],
  "files": [
    {
      "path": "components/ui/command-menu.tsx",
      "content": "\"use client\";\n\nimport React, { useState, useEffect, useMemo, useRef } from \"react\";\nimport { createPortal } from \"react-dom\";\nimport { AnimatePresence, motion, useReducedMotion } from \"framer-motion\";\nimport {\n  Search,\n  Command,\n  CornerDownLeft,\n  ArrowUp,\n  ArrowDown,\n  FileCode2,\n  Terminal,\n  Sparkles,\n  Settings,\n  Layers,\n  Moon,\n  Copy,\n  CheckCircle2,\n  X,\n} from \"lucide-react\";\nimport { cn } from \"@/lib/utils\";\n\nexport interface CommandItem {\n  id: string;\n  label: string;\n  group: string;\n  shortcut?: string;\n  keywords?: string[];\n  icon?: React.ReactNode;\n  onSelect?: () => void;\n}\n\nexport interface CommandMenuProps {\n  /** Array of command items grouped by their `group` property */\n  items?: CommandItem[];\n  /** Search input placeholder */\n  placeholder?: string;\n  /** Label on the trigger button */\n  triggerLabel?: string;\n  /** Enable global Cmd+K / Ctrl+K shortcut listener. Default: true */\n  enableGlobalShortcut?: boolean;\n  /** Controlled open state */\n  open?: boolean;\n  /** Callback when open state changes */\n  onOpenChange?: (open: boolean) => void;\n  /** Callback fired when any command item is selected */\n  onSelect?: (item: CommandItem) => void;\n  /** Empty state message when no commands match */\n  emptyMessage?: string;\n  className?: string;\n}\n\nconst DEFAULT_COMMANDS: CommandItem[] = [\n  {\n    id: \"new-component\",\n    label: \"Create New Component\",\n    group: \"Quick Actions\",\n    shortcut: \"⌘ N\",\n    keywords: [\"add\", \"generate\", \"ui\", \"template\"],\n    icon: <Sparkles className=\"h-4 w-4\" />,\n  },\n  {\n    id: \"copy-cli\",\n    label: \"Copy CLI Install Command\",\n    group: \"Quick Actions\",\n    shortcut: \"⌘ C\",\n    keywords: [\"npx\", \"shadcn\", \"registry\", \"terminal\"],\n    icon: <Terminal className=\"h-4 w-4\" />,\n  },\n  {\n    id: \"copy-theme\",\n    label: \"Copy Tailwind Config Tokens\",\n    group: \"Quick Actions\",\n    shortcut: \"⇧ ⌘ C\",\n    keywords: [\"css\", \"styles\", \"tokens\"],\n    icon: <Copy className=\"h-4 w-4\" />,\n  },\n  {\n    id: \"nav-components\",\n    label: \"Browse All Components\",\n    group: \"Navigation\",\n    shortcut: \"G C\",\n    keywords: [\"catalog\", \"gallery\", \"list\"],\n    icon: <Layers className=\"h-4 w-4\" />,\n  },\n  {\n    id: \"nav-docs\",\n    label: \"Open Documentation & Guides\",\n    group: \"Navigation\",\n    shortcut: \"G D\",\n    keywords: [\"help\", \"readme\", \"setup\"],\n    icon: <FileCode2 className=\"h-4 w-4\" />,\n  },\n  {\n    id: \"pref-theme\",\n    label: \"Toggle Dark / Light Appearance\",\n    group: \"Preferences\",\n    shortcut: \"⌘ J\",\n    keywords: [\"dark mode\", \"light mode\", \"theme\"],\n    icon: <Moon className=\"h-4 w-4\" />,\n  },\n  {\n    id: \"pref-settings\",\n    label: \"Workspace Settings\",\n    group: \"Preferences\",\n    shortcut: \"⌘ ,\",\n    keywords: [\"config\", \"account\", \"preferences\"],\n    icon: <Settings className=\"h-4 w-4\" />,\n  },\n];\n\nexport function CommandMenu({\n  items = DEFAULT_COMMANDS,\n  placeholder = \"Type a command or search...\",\n  triggerLabel = \"Search commands...\",\n  enableGlobalShortcut = true,\n  open: controlledOpen,\n  onOpenChange,\n  onSelect,\n  emptyMessage = \"No matching commands found.\",\n  className,\n}: CommandMenuProps) {\n  const shouldReduceMotion = useReducedMotion() ?? false;\n  const [mounted, setMounted] = useState(false);\n  const [internalOpen, setInternalOpen] = useState(false);\n  const [query, setQuery] = useState(\"\");\n  const [activeIndex, setActiveIndex] = useState(0);\n  const [lastExecuted, setLastExecuted] = useState<string | null>(null);\n\n  const inputRef = useRef<HTMLInputElement>(null);\n  const listRef = useRef<HTMLDivElement>(null);\n\n  const isControlled = controlledOpen !== undefined;\n  const isOpen = isControlled ? controlledOpen : internalOpen;\n\n  useEffect(() => {\n    setMounted(true);\n  }, []);\n\n  const setOpen = (next: boolean) => {\n    if (!isControlled) {\n      setInternalOpen(next);\n    }\n    if (!next) {\n      setQuery(\"\");\n      setActiveIndex(0);\n    }\n    onOpenChange?.(next);\n  };\n\n  const filteredItems = useMemo(() => {\n    const q = query.trim().toLowerCase();\n    if (!q) return items;\n    return items.filter(\n      (item) =>\n        item.label.toLowerCase().includes(q) ||\n        item.group.toLowerCase().includes(q) ||\n        (item.keywords?.some((k) => k.toLowerCase().includes(q)) ?? false)\n    );\n  }, [items, query]);\n\n  const groupedItems = useMemo(() => {\n    const groups: { group: string; entries: { item: CommandItem; flatIndex: number }[] }[] = [];\n    let counter = 0;\n\n    filteredItems.forEach((item) => {\n      let existing = groups.find((g) => g.group === item.group);\n      if (!existing) {\n        existing = { group: item.group, entries: [] };\n        groups.push(existing);\n      }\n      existing.entries.push({ item, flatIndex: counter });\n      counter += 1;\n    });\n\n    return groups;\n  }, [filteredItems]);\n\n  useEffect(() => {\n    setActiveIndex(0);\n  }, [query]);\n\n  // Global Cmd+K / Ctrl+K listener\n  useEffect(() => {\n    if (!enableGlobalShortcut) return;\n    const handleGlobalKeyDown = (e: KeyboardEvent) => {\n      if ((e.metaKey || e.ctrlKey) && e.key.toLowerCase() === \"k\") {\n        e.preventDefault();\n        setOpen(!isOpen);\n      }\n    };\n    window.addEventListener(\"keydown\", handleGlobalKeyDown);\n    return () => window.removeEventListener(\"keydown\", handleGlobalKeyDown);\n  }, [enableGlobalShortcut, isOpen]);\n\n  // Lock body scroll and focus input when open\n  useEffect(() => {\n    if (!isOpen) return;\n    const prevOverflow = document.body.style.overflow;\n    document.body.style.overflow = \"hidden\";\n    const timer = setTimeout(() => inputRef.current?.focus(), 20);\n    return () => {\n      document.body.style.overflow = prevOverflow;\n      clearTimeout(timer);\n    };\n  }, [isOpen]);\n\n  // Scroll active command into view\n  useEffect(() => {\n    if (!isOpen || !listRef.current) return;\n    const activeEl = listRef.current.querySelector<HTMLElement>(\n      `[data-cmd-index=\"${activeIndex}\"]`\n    );\n    activeEl?.scrollIntoView({ block: \"nearest\" });\n  }, [activeIndex, isOpen]);\n\n  const handleSelect = (item: CommandItem) => {\n    item.onSelect?.();\n    onSelect?.(item);\n    setLastExecuted(item.label);\n    setOpen(false);\n  };\n\n  const handleKeyDown = (e: React.KeyboardEvent<HTMLInputElement>) => {\n    if (e.key === \"ArrowDown\") {\n      e.preventDefault();\n      if (filteredItems.length > 0) {\n        setActiveIndex((prev) => (prev + 1) % filteredItems.length);\n      }\n    } else if (e.key === \"ArrowUp\") {\n      e.preventDefault();\n      if (filteredItems.length > 0) {\n        setActiveIndex(\n          (prev) => (prev - 1 + filteredItems.length) % filteredItems.length\n        );\n      }\n    } else if (e.key === \"Enter\" && filteredItems[activeIndex]) {\n      e.preventDefault();\n      handleSelect(filteredItems[activeIndex]);\n    } else if (e.key === \"Escape\") {\n      e.preventDefault();\n      setOpen(false);\n    }\n  };\n\n  return (\n    <div className={cn(\"inline-flex flex-col items-center gap-2.5\", className)}>\n      {/* Search Bar Trigger */}\n      <button\n        type=\"button\"\n        onClick={() => setOpen(true)}\n        className=\"group flex h-11 w-full min-w-[260px] max-w-sm items-center justify-between gap-4 rounded-xl border border-neutral-200 bg-white px-3.5 text-xs text-neutral-500 shadow-sm transition-all hover:border-black hover:text-black focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-black dark:border-[#262626] dark:bg-[#0A0A0A] dark:text-neutral-400 dark:hover:border-white dark:hover:text-white dark:focus-visible:ring-white\"\n      >\n        <span className=\"flex items-center gap-2.5\">\n          <Search className=\"h-3.5 w-3.5 text-neutral-400 transition-colors group-hover:text-black dark:group-hover:text-white\" />\n          <span className=\"font-medium\">{triggerLabel}</span>\n        </span>\n        <kbd className=\"inline-flex items-center gap-0.5 rounded-md border border-neutral-200 bg-neutral-100 px-1.5 py-0.5 font-mono text-[10px] font-semibold text-neutral-600 dark:border-[#262626] dark:bg-[#141414] dark:text-neutral-400\">\n          <Command className=\"h-2.5 w-2.5\" />K\n        </kbd>\n      </button>\n\n      {lastExecuted && (\n        <div className=\"flex items-center gap-1.5 text-[11px] text-neutral-500 dark:text-neutral-400\">\n          <CheckCircle2 className=\"h-3.5 w-3.5 text-black dark:text-white\" />\n          <span>\n            Ran: <strong className=\"font-medium text-black dark:text-white\">{lastExecuted}</strong>\n          </span>\n        </div>\n      )}\n\n      {mounted &&\n        createPortal(\n          <AnimatePresence>\n            {isOpen && (\n              <div className=\"fixed inset-0 z-[9999] flex items-start justify-center p-4 pt-[12vh] sm:pt-[16vh]\">\n                {/* Backdrop */}\n                <motion.div\n                  initial={{ opacity: 0 }}\n                  animate={{ opacity: 1 }}\n                  exit={{ opacity: 0 }}\n                  onClick={() => setOpen(false)}\n                  className=\"fixed inset-0 bg-black/50 backdrop-blur-xs\"\n                />\n\n                {/* Command Palette Dialog */}\n                <motion.div\n                  role=\"dialog\"\n                  aria-modal=\"true\"\n                  aria-label=\"Command Menu\"\n                  initial={\n                    shouldReduceMotion\n                      ? { opacity: 0 }\n                      : { opacity: 0, scale: 0.95, y: -12 }\n                  }\n                  animate={{ opacity: 1, scale: 1, y: 0 }}\n                  exit={\n                    shouldReduceMotion\n                      ? { opacity: 0 }\n                      : { opacity: 0, scale: 0.96, y: -8 }\n                  }\n                  transition={{ type: \"spring\", stiffness: 420, damping: 30 }}\n                  className=\"relative z-10 flex w-full max-w-lg flex-col overflow-hidden rounded-2xl border border-neutral-200 bg-white shadow-2xl dark:border-[#262626] dark:bg-[#0A0A0A]\"\n                >\n                  {/* Search Input Header */}\n                  <div className=\"flex items-center gap-3 border-b border-neutral-200 px-4 py-3.5 dark:border-[#1A1A1A]\">\n                    <Search className=\"h-4 w-4 shrink-0 text-neutral-400\" />\n                    <input\n                      ref={inputRef}\n                      type=\"text\"\n                      value={query}\n                      onChange={(e) => setQuery(e.target.value)}\n                      onKeyDown={handleKeyDown}\n                      placeholder={placeholder}\n                      className=\"w-full bg-transparent text-sm text-black placeholder:text-neutral-400 focus:outline-none dark:text-white dark:placeholder:text-neutral-500\"\n                    />\n                    {query && (\n                      <button\n                        type=\"button\"\n                        aria-label=\"Clear search\"\n                        onClick={() => {\n                          setQuery(\"\");\n                          inputRef.current?.focus();\n                        }}\n                        className=\"rounded-md p-1 text-neutral-400 hover:bg-neutral-100 hover:text-black dark:hover:bg-[#161616] dark:hover:text-white\"\n                      >\n                        <X className=\"h-3.5 w-3.5\" />\n                      </button>\n                    )}\n                    <button\n                      type=\"button\"\n                      onClick={() => setOpen(false)}\n                      className=\"rounded-md border border-neutral-200 bg-neutral-100 px-1.5 py-0.5 font-mono text-[10px] font-medium text-neutral-500 hover:text-black dark:border-[#262626] dark:bg-[#141414] dark:text-neutral-400 dark:hover:text-white\"\n                    >\n                      ESC\n                    </button>\n                  </div>\n\n                  {/* Grouped Results List */}\n                  <div\n                    ref={listRef}\n                    role=\"listbox\"\n                    className=\"max-h-[320px] overflow-y-auto p-2\"\n                  >\n                    {groupedItems.length === 0 ? (\n                      <div className=\"py-10 text-center text-xs text-neutral-400\">\n                        {emptyMessage}\n                      </div>\n                    ) : (\n                      groupedItems.map((groupBlock) => (\n                        <div key={groupBlock.group} className=\"mb-2 last:mb-0\">\n                          <div className=\"px-2.5 py-1.5 font-mono text-[10px] font-semibold uppercase tracking-wider text-neutral-400 dark:text-neutral-500\">\n                            {groupBlock.group}\n                          </div>\n                          {groupBlock.entries.map(({ item, flatIndex }) => {\n                            const isActive = flatIndex === activeIndex;\n                            return (\n                              <button\n                                key={item.id}\n                                type=\"button\"\n                                role=\"option\"\n                                data-cmd-index={flatIndex}\n                                aria-selected={isActive}\n                                onMouseEnter={() => setActiveIndex(flatIndex)}\n                                onClick={() => handleSelect(item)}\n                                className={cn(\n                                  \"flex w-full items-center justify-between rounded-xl px-3 py-2.5 text-left text-sm transition-colors\",\n                                  isActive\n                                    ? \"bg-neutral-100 text-black dark:bg-[#161616] dark:text-white\"\n                                    : \"text-neutral-600 hover:bg-neutral-50 dark:text-neutral-400 dark:hover:bg-[#121212]\"\n                                )}\n                              >\n                                <span className=\"flex items-center gap-3 font-medium\">\n                                  <span\n                                    className={cn(\n                                      \"flex h-7 w-7 items-center justify-center rounded-lg border transition-colors\",\n                                      isActive\n                                        ? \"border-black bg-black text-white dark:border-white dark:bg-white dark:text-black\"\n                                        : \"border-neutral-200 bg-neutral-50 text-neutral-500 dark:border-[#262626] dark:bg-[#121212] dark:text-neutral-400\"\n                                    )}\n                                  >\n                                    {item.icon ?? <Command className=\"h-3.5 w-3.5\" />}\n                                  </span>\n                                  <span>{item.label}</span>\n                                </span>\n\n                                {item.shortcut && (\n                                  <kbd className=\"rounded-md border border-neutral-200 bg-white px-1.5 py-0.5 font-mono text-[10px] text-neutral-500 dark:border-[#262626] dark:bg-[#0A0A0A] dark:text-neutral-400\">\n                                    {item.shortcut}\n                                  </kbd>\n                                )}\n                              </button>\n                            );\n                          })}\n                        </div>\n                      ))\n                    )}\n                  </div>\n\n                  {/* Keyboard Hint Footer */}\n                  <div className=\"flex items-center justify-between border-t border-neutral-200 bg-neutral-50/70 px-4 py-2.5 text-[11px] text-neutral-400 dark:border-[#1A1A1A] dark:bg-[#0D0D0D]\">\n                    <div className=\"flex items-center gap-3\">\n                      <span className=\"inline-flex items-center gap-1\">\n                        <ArrowUp className=\"h-3 w-3\" />\n                        <ArrowDown className=\"h-3 w-3\" />\n                        Navigate\n                      </span>\n                      <span className=\"inline-flex items-center gap-1\">\n                        <CornerDownLeft className=\"h-3 w-3\" />\n                        Run\n                      </span>\n                    </div>\n                    <span className=\"font-mono text-[10px]\">\n                      {filteredItems.length} command{filteredItems.length === 1 ? \"\" : \"s\"}\n                    </span>\n                  </div>\n                </motion.div>\n              </div>\n            )}\n          </AnimatePresence>,\n          document.body\n        )}\n    </div>\n  );\n}\n\nexport default CommandMenu;\n",
      "type": "registry:ui",
      "target": "components/ui/command-menu.tsx"
    },
    {
      "path": "lib/utils.ts",
      "content": "import { type ClassValue, clsx } from \"clsx\";\nimport { twMerge } from \"tailwind-merge\";\n\nexport function cn(...inputs: ClassValue[]) {\n  return twMerge(clsx(inputs));\n}\n\nexport async function copyToClipboard(text: string): Promise<boolean> {\n  if (typeof window === \"undefined\") return false;\n\n  try {\n    if (navigator.clipboard && window.isSecureContext) {\n      await navigator.clipboard.writeText(text);\n      return true;\n    }\n\n    const textArea = document.createElement(\"textarea\");\n    textArea.value = text;\n    textArea.style.position = \"fixed\";\n    textArea.style.left = \"-999999px\";\n    textArea.style.top = \"-999999px\";\n    document.body.appendChild(textArea);\n    textArea.focus();\n    textArea.select();\n    const succeeded = document.execCommand(\"copy\");\n    textArea.remove();\n    return succeeded;\n  } catch {\n    return false;\n  }\n}\n",
      "type": "registry:lib",
      "target": "lib/utils.ts"
    }
  ]
}
