{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "command-palette",
  "title": "Command Palette",
  "description": "A Cmd/Ctrl+K command palette with fuzzy search and match highlighting, grouped results, keyboard navigation, shortcuts and a portal overlay.",
  "dependencies": [
    "motion",
    "clsx",
    "tailwind-merge"
  ],
  "files": [
    {
      "path": "registry/new-york/command-palette/command-palette.tsx",
      "content": "\"use client\";\n\nimport * as React from \"react\";\nimport { createPortal } from \"react-dom\";\nimport { motion, AnimatePresence } from \"motion/react\";\nimport { clsx, type ClassValue } from \"clsx\";\nimport { twMerge } from \"tailwind-merge\";\n\nfunction cn(...inputs: ClassValue[]) {\n  return twMerge(clsx(inputs));\n}\n\nexport interface CommandItem {\n  id: string;\n  label: string;\n  group?: string;\n  icon?: React.ReactNode;\n  shortcut?: string[];\n  keywords?: string[];\n  disabled?: boolean;\n  onSelect?: () => void;\n}\n\nexport interface CommandPaletteProps {\n  items?: CommandItem[];\n  open?: boolean;\n  defaultOpen?: boolean;\n  onOpenChange?: (open: boolean) => void;\n  onSelectItem?: (item: CommandItem) => void;\n  placeholder?: string;\n  emptyText?: string;\n  /** Key that toggles the palette together with Cmd/Ctrl. Pass false to disable. */\n  hotkey?: string | false;\n  closeOnSelect?: boolean;\n  theme?: \"dark\" | \"light\";\n  contained?: boolean;\n  className?: string;\n}\n\nconst PALETTES = {\n  dark: { text: \"#F5F4F1\", muted: \"rgba(245,244,241,0.5)\", faint: \"rgba(245,244,241,0.3)\", bg: \"#0E0E0E\", border: \"rgba(255,255,255,0.1)\", divider: \"rgba(255,255,255,0.08)\", active: \"rgba(255,255,255,0.09)\", chip: \"rgba(255,255,255,0.08)\", chipBorder: \"rgba(255,255,255,0.12)\", overlay: \"rgba(0,0,0,0.6)\", mark: \"#FFFFFF\", shadow: \"0 32px 90px rgba(0,0,0,0.65)\" },\n  light: { text: \"#0A0A0A\", muted: \"rgba(10,10,10,0.5)\", faint: \"rgba(10,10,10,0.3)\", bg: \"#FFFFFF\", border: \"rgba(10,10,10,0.14)\", divider: \"rgba(10,10,10,0.08)\", active: \"rgba(10,10,10,0.06)\", chip: \"rgba(10,10,10,0.05)\", chipBorder: \"rgba(10,10,10,0.12)\", overlay: \"rgba(10,10,10,0.35)\", mark: \"#000000\", shadow: \"0 28px 80px rgba(0,0,0,0.22)\" },\n};\n\nconst GLYPHS: Record<string, string> = {\n  file: \"M5 2.5h6l3 3v11a1 1 0 0 1-1 1H5a1 1 0 0 1-1-1v-12a1 1 0 0 1 1-1ZM11 2.5v3h3\",\n  folder: \"M2.5 5.5a1 1 0 0 1 1-1h4l1.5 2h7a1 1 0 0 1 1 1v7a1 1 0 0 1-1 1h-12.5a1 1 0 0 1-1-1v-9Z\",\n  search: \"M8.5 14a5.5 5.5 0 1 0 0-11 5.5 5.5 0 0 0 0 11ZM12.5 12.5L16.5 16.5\",\n  user: \"M10 9.5a3 3 0 1 0 0-6 3 3 0 0 0 0 6ZM4 16.5c0-2.8 2.7-4.5 6-4.5s6 1.7 6 4.5\",\n  settings: \"M10 12.5a2.5 2.5 0 1 0 0-5 2.5 2.5 0 0 0 0 5ZM10 2.5v2M10 15.5v2M2.5 10h2M15.5 10h2M4.7 4.7l1.4 1.4M13.9 13.9l1.4 1.4M4.7 15.3l1.4-1.4M13.9 6.1l1.4-1.4\",\n  moon: \"M16 11.5A6.5 6.5 0 0 1 8.5 4a6.5 6.5 0 1 0 7.5 7.5Z\",\n  logout: \"M8 3.5H5a1 1 0 0 0-1 1v11a1 1 0 0 0 1 1h3M12 6.5l3.5 3.5L12 13.5M15.5 10H8\",\n  plus: \"M10 4v12M4 10h12\",\n  link: \"M8.5 11.5a3 3 0 0 0 4.2 0l2.5-2.5a3 3 0 0 0-4.2-4.2L9.8 5.8M11.5 8.5a3 3 0 0 0-4.2 0L4.8 11a3 3 0 0 0 4.2 4.2l1.2-1.2\",\n  home: \"M3 9.5L10 3.5l7 6M5 8.5v7a1 1 0 0 0 1 1h8a1 1 0 0 0 1-1v-7\",\n  bell: \"M5.5 8a4.5 4.5 0 0 1 9 0c0 4 1.5 5 1.5 5H4s1.5-1 1.5-5ZM8.5 16a1.5 1.5 0 0 0 3 0\",\n};\n\nexport function CommandGlyph({ name, size = 16 }: { name: keyof typeof GLYPHS | string; size?: number }) {\n  return (\n    <svg width={size} height={size} viewBox=\"0 0 20 20\" fill=\"none\" stroke=\"currentColor\" strokeWidth=\"1.5\" strokeLinecap=\"round\" strokeLinejoin=\"round\" aria-hidden=\"true\">\n      <path d={GLYPHS[name] ?? GLYPHS.file} />\n    </svg>\n  );\n}\n\nexport const DEFAULT_COMMANDS: CommandItem[] = [\n  { id: \"new-file\", label: \"New file\", group: \"Actions\", icon: <CommandGlyph name=\"plus\" />, shortcut: [\"N\"] },\n  { id: \"new-folder\", label: \"New folder\", group: \"Actions\", icon: <CommandGlyph name=\"folder\" />, shortcut: [\"⇧\", \"N\"] },\n  { id: \"search\", label: \"Search in project\", group: \"Actions\", icon: <CommandGlyph name=\"search\" />, shortcut: [\"⇧\", \"F\"], keywords: [\"find\"] },\n  { id: \"copy-link\", label: \"Copy share link\", group: \"Actions\", icon: <CommandGlyph name=\"link\" />, shortcut: [\"L\"] },\n  { id: \"home\", label: \"Go to dashboard\", group: \"Navigation\", icon: <CommandGlyph name=\"home\" />, shortcut: [\"G\", \"D\"] },\n  { id: \"files\", label: \"Go to files\", group: \"Navigation\", icon: <CommandGlyph name=\"file\" />, shortcut: [\"G\", \"F\"] },\n  { id: \"notifications\", label: \"Open notifications\", group: \"Navigation\", icon: <CommandGlyph name=\"bell\" />, keywords: [\"inbox\"] },\n  { id: \"profile\", label: \"Edit profile\", group: \"Settings\", icon: <CommandGlyph name=\"user\" /> },\n  { id: \"theme\", label: \"Toggle dark mode\", group: \"Settings\", icon: <CommandGlyph name=\"moon\" />, shortcut: [\"D\"], keywords: [\"theme\", \"appearance\"] },\n  { id: \"settings\", label: \"Preferences\", group: \"Settings\", icon: <CommandGlyph name=\"settings\" />, shortcut: [\",\"], keywords: [\"config\"] },\n  { id: \"logout\", label: \"Sign out\", group: \"Settings\", icon: <CommandGlyph name=\"logout\" /> },\n];\n\n/** Case-insensitive fuzzy match. Returns matched character indices and a score, or null. */\nfunction fuzzy(query: string, text: string): { score: number; indices: number[] } | null {\n  const q = query.toLowerCase();\n  const t = text.toLowerCase();\n  const at = t.indexOf(q);\n  if (at >= 0) {\n    return { score: 100 - at + (at === 0 ? 30 : t[at - 1] === \" \" ? 15 : 0), indices: Array.from({ length: q.length }, (_, i) => at + i) };\n  }\n  const indices: number[] = [];\n  let from = 0;\n  let score = 0;\n  for (const ch of q) {\n    const i = t.indexOf(ch, from);\n    if (i === -1) return null;\n    score += i === from ? 4 : 1;\n    indices.push(i);\n    from = i + 1;\n  }\n  return { score, indices };\n}\n\nfunction Highlight({ text, indices, color }: { text: string; indices: number[]; color: string }) {\n  if (indices.length === 0) return <>{text}</>;\n  const set = new Set(indices);\n  return (\n    <>\n      {Array.from(text).map((ch, i) =>\n        set.has(i) ? (\n          <span key={i} style={{ color, fontWeight: 700 }}>\n            {ch}\n          </span>\n        ) : (\n          <React.Fragment key={i}>{ch}</React.Fragment>\n        ),\n      )}\n    </>\n  );\n}\n\nconst subscribeNoop = () => () => {};\nconst isMacSnapshot = () => typeof navigator !== \"undefined\" && /Mac|iPhone|iPad/.test(navigator.platform);\n\nexport function CommandPalette({\n  items = DEFAULT_COMMANDS,\n  open,\n  defaultOpen = false,\n  onOpenChange,\n  onSelectItem,\n  placeholder = \"Type a command or search...\",\n  emptyText = \"No results found.\",\n  hotkey = \"k\",\n  closeOnSelect = true,\n  theme = \"dark\",\n  contained = false,\n  className,\n}: CommandPaletteProps) {\n  const p = PALETTES[theme];\n  const uid = React.useId();\n  const inputRef = React.useRef<HTMLInputElement>(null);\n  const listRef = React.useRef<HTMLDivElement>(null);\n  const [internal, setInternal] = React.useState(defaultOpen);\n  const [mounted, setMounted] = React.useState(contained);\n  const [query, setQuery] = React.useState(\"\");\n  const [activeIndex, setActiveIndex] = React.useState(0);\n  const controlled = open !== undefined;\n  const isOpen = controlled ? open : internal;\n  const isMac = React.useSyncExternalStore(subscribeNoop, isMacSnapshot, () => false);\n\n  const setOpen = React.useCallback(\n    (next: boolean) => {\n      if (!controlled) setInternal(next);\n      onOpenChange?.(next);\n      if (!next) {\n        setQuery(\"\");\n        setActiveIndex(0);\n      }\n    },\n    [controlled, onOpenChange],\n  );\n\n  const results = React.useMemo(() => {\n    const q = query.trim();\n    const rows = items\n      .map((item, order) => {\n        if (!q) return { item, order, score: 0, indices: [] as number[] };\n        const label = fuzzy(q, item.label);\n        if (label) return { item, order, score: label.score, indices: label.indices };\n        const kw = item.keywords?.map((k) => fuzzy(q, k)).find(Boolean);\n        return kw ? { item, order, score: kw.score * 0.4, indices: [] as number[] } : null;\n      })\n      .filter((r): r is { item: CommandItem; order: number; score: number; indices: number[] } => r !== null);\n    const groupOrder: string[] = [];\n    rows.forEach((r) => {\n      const g = r.item.group ?? \"\";\n      if (!groupOrder.includes(g)) groupOrder.push(g);\n    });\n    const groups = groupOrder.map((g) => ({\n      name: g,\n      rows: rows.filter((r) => (r.item.group ?? \"\") === g).sort((a, b) => (q ? b.score - a.score : a.order - b.order)),\n    }));\n    return { groups, flat: groups.flatMap((g) => g.rows) };\n  }, [items, query]);\n\n  const enabledIndexes = results.flat.map((r, i) => (r.item.disabled ? -1 : i)).filter((i) => i >= 0);\n  const safeActive = Math.min(activeIndex, Math.max(results.flat.length - 1, 0));\n\n  const run = (index: number) => {\n    const row = results.flat[index];\n    if (!row || row.item.disabled) return;\n    row.item.onSelect?.();\n    onSelectItem?.(row.item);\n    if (closeOnSelect) setOpen(false);\n  };\n\n  const move = (dir: 1 | -1) => {\n    if (enabledIndexes.length === 0) return;\n    const pos = enabledIndexes.indexOf(safeActive);\n    const next = pos === -1 ? 0 : (pos + dir + enabledIndexes.length) % enabledIndexes.length;\n    setActiveIndex(enabledIndexes[next]);\n  };\n\n  React.useEffect(() => {\n    if (isOpen && !mounted) {\n      const id = setTimeout(() => setMounted(true), 0);\n      return () => clearTimeout(id);\n    }\n  }, [isOpen, mounted]);\n\n  React.useEffect(() => {\n    if (hotkey === false) return;\n    const onKey = (e: KeyboardEvent) => {\n      if ((e.metaKey || e.ctrlKey) && e.key.toLowerCase() === hotkey.toLowerCase()) {\n        e.preventDefault();\n        setOpen(!isOpen);\n      }\n    };\n    document.addEventListener(\"keydown\", onKey);\n    return () => document.removeEventListener(\"keydown\", onKey);\n  }, [hotkey, isOpen, setOpen]);\n\n  React.useEffect(() => {\n    if (!isOpen) return;\n    const previous = document.activeElement as HTMLElement | null;\n    const t = setTimeout(() => inputRef.current?.focus({ preventScroll: true }), 30);\n    const prevOverflow = document.body.style.overflow;\n    if (!contained) document.body.style.overflow = \"hidden\";\n    return () => {\n      clearTimeout(t);\n      if (!contained) document.body.style.overflow = prevOverflow;\n      previous?.focus?.({ preventScroll: true });\n    };\n  }, [isOpen, contained]);\n\n  React.useEffect(() => {\n    listRef.current?.querySelector<HTMLElement>(`[data-index=\"${safeActive}\"]`)?.scrollIntoView({ block: \"nearest\" });\n  }, [safeActive, results.flat.length]);\n\n  const onInputKeyDown = (e: React.KeyboardEvent) => {\n    if (e.key === \"ArrowDown\") { e.preventDefault(); move(1); }\n    else if (e.key === \"ArrowUp\") { e.preventDefault(); move(-1); }\n    else if (e.key === \"Home\") { e.preventDefault(); if (enabledIndexes.length) setActiveIndex(enabledIndexes[0]); }\n    else if (e.key === \"End\") { e.preventDefault(); if (enabledIndexes.length) setActiveIndex(enabledIndexes[enabledIndexes.length - 1]); }\n    else if (e.key === \"Enter\") { e.preventDefault(); run(safeActive); }\n    else if (e.key === \"Escape\") { e.preventDefault(); e.stopPropagation(); setOpen(false); }\n    else if (e.key === \"Tab\") e.preventDefault();\n  };\n\n  const kbd = (k: string, i: number) => (\n    <kbd key={i} className=\"inline-flex items-center justify-center font-sans font-semibold\" style={{ minWidth: 20, height: 20, padding: \"0 5px\", borderRadius: 6, background: p.chip, border: `1px solid ${p.chipBorder}`, color: p.muted, fontSize: 11 }}>\n      {k}\n    </kbd>\n  );\n\n  const node = (\n    <AnimatePresence>\n      {isOpen && (\n        <motion.div\n          key=\"palette-root\"\n          className={cn(contained ? \"absolute\" : \"fixed\", \"inset-0 z-[9998] flex items-start justify-center px-4\", className)}\n          style={{ background: p.overlay, backdropFilter: \"blur(5px)\", WebkitBackdropFilter: \"blur(5px)\", paddingTop: contained ? 40 : \"14vh\" }}\n          initial={{ opacity: 0 }}\n          animate={{ opacity: 1 }}\n          exit={{ opacity: 0 }}\n          transition={{ duration: 0.16 }}\n          onMouseDown={(e) => {\n            if (e.target === e.currentTarget) setOpen(false);\n          }}\n        >\n          <motion.div\n            role=\"dialog\"\n            aria-label=\"Command palette\"\n            aria-modal={contained ? undefined : true}\n            className=\"flex w-full flex-col overflow-hidden\"\n            style={{ maxWidth: 560, maxHeight: contained ? \"calc(100% - 56px)\" : \"min(460px, 72vh)\", borderRadius: 18, background: p.bg, border: `1px solid ${p.border}`, boxShadow: p.shadow, color: p.text, fontFamily: \"Inter, sans-serif\" }}\n            initial={{ opacity: 0, scale: 0.96, y: -10 }}\n            animate={{ opacity: 1, scale: 1, y: 0 }}\n            exit={{ opacity: 0, scale: 0.97, y: -6 }}\n            transition={{ type: \"spring\", stiffness: 460, damping: 34 }}\n          >\n            <div className=\"flex shrink-0 items-center\" style={{ gap: 12, padding: \"0 18px\", height: 56, borderBottom: `1px solid ${p.divider}` }}>\n              <span style={{ color: p.muted }}>\n                <CommandGlyph name=\"search\" size={18} />\n              </span>\n              <input\n                ref={inputRef}\n                value={query}\n                placeholder={placeholder}\n                role=\"combobox\"\n                aria-expanded=\"true\"\n                aria-controls={`${uid}-list`}\n                aria-activedescendant={results.flat.length ? `${uid}-opt-${safeActive}` : undefined}\n                aria-autocomplete=\"list\"\n                autoComplete=\"off\"\n                spellCheck={false}\n                onChange={(e) => {\n                  setQuery(e.target.value);\n                  setActiveIndex(0);\n                }}\n                onKeyDown={onInputKeyDown}\n                className=\"min-w-0 flex-1 border-none bg-transparent p-0 font-medium outline-none placeholder:opacity-45\"\n                style={{ color: p.text, fontSize: 16, [\"--tw-placeholder-color\" as string]: p.muted }}\n              />\n              {kbd(\"esc\", 0)}\n            </div>\n\n            <div ref={listRef} id={`${uid}-list`} role=\"listbox\" aria-label=\"Commands\" className=\"min-h-0 flex-1 overflow-y-auto\" style={{ padding: 8, scrollbarWidth: \"thin\" }}>\n              {results.flat.length === 0 && (\n                <div className=\"text-center\" style={{ padding: \"36px 12px\", color: p.muted, fontSize: 14 }}>\n                  {emptyText}\n                </div>\n              )}\n              {results.groups.map((g) => (\n                <div key={g.name || \"_\"} role=\"group\" aria-label={g.name || undefined} style={{ marginBottom: 4 }}>\n                  {g.name && (\n                    <div className=\"font-semibold uppercase\" style={{ padding: \"10px 12px 6px\", color: p.faint, fontSize: 11, letterSpacing: \"0.08em\" }}>\n                      {g.name}\n                    </div>\n                  )}\n                  {g.rows.map((r) => {\n                    const index = results.flat.indexOf(r);\n                    const active = index === safeActive;\n                    return (\n                      <div\n                        key={r.item.id}\n                        id={`${uid}-opt-${index}`}\n                        data-index={index}\n                        role=\"option\"\n                        aria-selected={active}\n                        aria-disabled={r.item.disabled || undefined}\n                        onMouseMove={() => !r.item.disabled && !active && setActiveIndex(index)}\n                        onClick={() => run(index)}\n                        className=\"relative flex items-center\"\n                        style={{ gap: 12, height: 42, padding: \"0 12px\", borderRadius: 11, cursor: r.item.disabled ? \"not-allowed\" : \"pointer\", opacity: r.item.disabled ? 0.4 : 1 }}\n                      >\n                        {active && <motion.span layoutId={`${uid}-active`} aria-hidden=\"true\" className=\"absolute inset-0\" style={{ borderRadius: 11, background: p.active }} transition={{ type: \"spring\", stiffness: 600, damping: 42 }} />}\n                        {r.item.icon && (\n                          <span className=\"relative flex shrink-0 items-center justify-center\" style={{ width: 20, color: active ? p.text : p.muted, transition: \"color 0.12s ease\" }}>\n                            {r.item.icon}\n                          </span>\n                        )}\n                        <span className=\"relative min-w-0 flex-1 truncate\" style={{ fontSize: 14.5, fontWeight: 500, color: active ? p.text : p.muted, transition: \"color 0.12s ease\" }}>\n                          <Highlight text={r.item.label} indices={r.indices} color={p.mark} />\n                        </span>\n                        {r.item.shortcut && <span className=\"relative flex shrink-0 items-center\" style={{ gap: 4 }}>{r.item.shortcut.map(kbd)}</span>}\n                      </div>\n                    );\n                  })}\n                </div>\n              ))}\n            </div>\n\n            <div className=\"flex shrink-0 items-center justify-between\" style={{ padding: \"0 18px\", height: 40, borderTop: `1px solid ${p.divider}`, color: p.faint, fontSize: 12 }}>\n              <span className=\"flex items-center\" style={{ gap: 14 }}>\n                <span className=\"flex items-center\" style={{ gap: 5 }}>\n                  {kbd(\"↑\", 0)}\n                  {kbd(\"↓\", 1)} navigate\n                </span>\n                <span className=\"flex items-center\" style={{ gap: 5 }}>\n                  {kbd(\"↵\", 0)} select\n                </span>\n              </span>\n              {hotkey !== false && <span>{isMac ? \"⌘\" : \"Ctrl\"} {hotkey.toUpperCase()} to toggle</span>}\n            </div>\n          </motion.div>\n        </motion.div>\n      )}\n    </AnimatePresence>\n  );\n\n  if (contained) return node;\n  if (!mounted) return null;\n  return createPortal(node, document.body);\n}\n",
      "type": "registry:component"
    }
  ],
  "type": "registry:component"
}