{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"command-menu","type":"registry:ui","title":"Command Menu","description":"A fast, nested command menu powered by kmenu.","dependencies":["@base-ui/react","@kmenu/react","kmenu","motion"],"docs":"Sound: the data-slot attributes on this component are read by the Sound layer, installed separately with `npx shadcn@latest add @kobra/sound`.\nKeep the data-slot and data-sound attributes as written — they are the wiring, not decoration.\nWrap your app once in <SoundEffects> from components/ui/sound.tsx and every Kobra component answers a press.","files":[{"path":"components/ui/command-menu.tsx","content":"import { IconPlaceholder } from '@/components/icon-placeholder'\nimport { useDirection } from '@base-ui/react/direction-provider'\nimport { Command as CommandRoot } from '@kmenu/react'\nimport {\n  CommandEmpty,\n  CommandGroup,\n  CommandInput,\n  CommandList,\n  CommandOption,\n  useCommand,\n} from '@kmenu/react'\nimport type { CommandOption as CommandOptionType, FilterFunction } from 'kmenu'\nimport { AnimatePresence, motion, useReducedMotion } from 'motion/react'\nimport { type ReactNode, useCallback, useEffect, useRef, useState } from 'react'\n\nimport { search } from '@/lib/command-filter'\nimport { cn } from '@/lib/utils'\n\nexport interface CommandMenuAction extends Omit<CommandOptionType, 'children'> {\n  icon?: ReactNode\n  shortcut?: Array<string>\n\n  hint?: string\n\n  badge?: string\n\n  crumb?: string\n\n  hidden?: boolean\n  children?: Array<CommandMenuAction>\n}\n\nconst asAction = (option: CommandOptionType<CommandMenuAction>): CommandMenuAction =>\n  option as CommandMenuAction\n\nconst filterActions: FilterFunction<CommandMenuAction> = (options, query) => {\n  const visible = query.trim() ? options : options.filter((option) => !asAction(option).hidden)\n  return search(visible, query)\n}\n\nfunction replay(element: HTMLElement | null | undefined, className: string) {\n  if (!element) return\n  element.classList.remove(className)\n  void element.offsetWidth\n  element.classList.add(className)\n}\n\nfunction CommandOptions() {\n  const { command, state } = useCommand<CommandMenuAction>()\n\n  const position = `${String(state.currentLevel)}:${state.breadcrumbs.map((crumb) => crumb.id).join('>')}`\n  const lastPosition = useRef(position)\n\n  const query = state.input\n  const topId = state.filtered[0]?.id\n  useEffect(() => {\n    if (topId) command?.setActiveById(topId)\n  }, [command, query, topId])\n\n  useEffect(() => {\n    if (lastPosition.current === position) return\n    lastPosition.current = position\n\n    const list = document.querySelector<HTMLElement>('.command-list')\n    replay(list, 'cmd-level-enter')\n    replay(list?.closest<HTMLElement>('.command-dialog'), 'cmd-level-shift')\n  }, [position])\n\n  const filtered = state.filtered.map(asAction)\n  const groups = [...new Set(filtered.map((action) => action.group))].filter(\n    (group) => group !== undefined,\n  )\n  const ungrouped = filtered.filter((action) => !action.group)\n\n  const renderOption = (action: CommandMenuAction) => (\n    <CommandOption\n      key={action.id ?? action.label}\n      value={action}\n      disabled={action.disabled}\n      className=\"command-option\"\n    >\n      <span className=\"command-option-content\">\n        {action.icon ? <span className=\"command-option-icon\">{action.icon}</span> : null}\n        <span className=\"command-option-label\">{action.label}</span>\n      </span>\n      {action.badge ? <span className=\"command-option-badge\">{action.badge}</span> : null}\n      {action.children ? (\n        <IconPlaceholder tabler=\"IconChevronRight\" lucide=\"ChevronRight\" hugeicons=\"ArrowRight01Icon\" phosphor=\"CaretRightIcon\" remixicon=\"RiArrowRightSLine\" className=\"command-option-more\" />\n      ) : action.shortcut ? (\n        <span className=\"command-option-shortcut\">\n          {action.shortcut.map((key) => (\n            <kbd key={key} className=\"command-shortcut-key\">\n              {key}\n            </kbd>\n          ))}\n        </span>\n      ) : action.hint ? (\n        <span className=\"command-option-hint\">{action.hint}</span>\n      ) : null}\n    </CommandOption>\n  )\n\n  if (state.currentLevel > 0) return <>{filtered.map(renderOption)}</>\n\n  return (\n    <>\n      {ungrouped.map(renderOption)}\n      {groups.map((group) => (\n        <CommandGroup key={group} heading={<div className=\"command-group-heading\">{group}</div>}>\n          {filtered.filter((action) => action.group === group).map(renderOption)}\n        </CommandGroup>\n      ))}\n    </>\n  )\n}\n\nfunction Breadcrumbs({\n  options,\n  scope,\n  onLeaveScope,\n}: {\n  options: Array<CommandMenuAction>\n  scope?: string\n\n  onLeaveScope: () => void\n}) {\n  const { command, state } = useCommand<CommandMenuAction>()\n  const reduced = useReducedMotion()\n  const direction = useDirection()\n\n  const labels = new Map<string, string>()\n  const walk = (actions: Array<CommandMenuAction>) => {\n    for (const action of actions) {\n      if (action.id) labels.set(action.id, action.crumb ?? action.label)\n      if (action.children) walk(action.children)\n    }\n  }\n  walk(options)\n\n  const behind = direction === 'rtl' ? 8 : -8\n  const hidden = reduced ? { opacity: 0 } : { opacity: 0, x: behind, filter: 'blur(4px)' }\n\n  const backTo = (depth: number) => {\n    for (let left = state.breadcrumbs.length; left > depth; left -= 1) {\n      if (!command?.goBack()) break\n    }\n  }\n\n  const here = state.breadcrumbs.length\n\n  return (\n    <div className=\"command-breadcrumbs\">\n      {here === 0 && !scope ? (\n        <span>Home</span>\n      ) : (\n        <button\n          type=\"button\"\n          onClick={() => {\n            backTo(0)\n            onLeaveScope()\n          }}\n        >\n          Home\n        </button>\n      )}\n      {scope ? (\n        here === 0 ? (\n          <span>{scope}</span>\n        ) : (\n          <button\n            type=\"button\"\n            onClick={() => {\n              backTo(0)\n            }}\n          >\n            {scope}\n          </button>\n        )\n      ) : null}\n      <AnimatePresence initial={false}>\n        {state.breadcrumbs.map(({ id, label }, index) =>\n          index === here - 1 ? (\n            <motion.span\n              key={id}\n              initial={hidden}\n              animate={{ opacity: 1, x: 0, filter: 'blur(0px)' }}\n              exit={{ ...hidden, transition: { duration: 0.13, ease: [0.23, 1, 0.32, 1] } }}\n              transition={{ duration: 0.18, ease: [0.23, 1, 0.32, 1] }}\n            >\n              {labels.get(id) ?? label}\n            </motion.span>\n          ) : (\n            <motion.button\n              key={id}\n              type=\"button\"\n              initial={hidden}\n              animate={{ opacity: 1, x: 0, filter: 'blur(0px)' }}\n              exit={{ ...hidden, transition: { duration: 0.13, ease: [0.23, 1, 0.32, 1] } }}\n              transition={{ duration: 0.18, ease: [0.23, 1, 0.32, 1] }}\n              onClick={() => {\n                backTo(index + 1)\n              }}\n            >\n              {labels.get(id) ?? label}\n            </motion.button>\n          ),\n        )}\n      </AnimatePresence>\n    </div>\n  )\n}\n\nfunction ScopeBackspace({ onLeaveScope }: { onLeaveScope: () => void }) {\n  const { state } = useCommand<CommandMenuAction>()\n  const depth = state.breadcrumbs.length\n\n  useEffect(() => {\n    const onKeyDown = (event: KeyboardEvent) => {\n      if (event.key !== 'Backspace' || depth > 0) return\n      const target = event.target\n      if (target instanceof HTMLInputElement && target.value !== '') return\n      event.preventDefault()\n      onLeaveScope()\n    }\n    window.addEventListener('keydown', onKeyDown, true)\n    return () => window.removeEventListener('keydown', onKeyDown, true)\n  }, [depth, onLeaveScope])\n\n  return null\n}\n\nconst OPEN_EVENT = 'kobra:open-command-menu'\n\nexport function openCommandMenu(scope?: string) {\n  window.dispatchEvent(new CustomEvent(OPEN_EVENT, { detail: { scope } }))\n}\n\ndeclare global {\n  interface WindowEventMap {\n    [OPEN_EVENT]: CustomEvent<{ scope?: string }>\n  }\n}\n\nconst OPEN_OVERLAY =\n  ':is([role=\"dialog\"],[role=\"alertdialog\"],[role=\"menu\"],[role=\"listbox\"]):is([data-open],[data-state=\"open\"])'\n\nexport function CommandMenu({\n  actions,\n  scopes,\n  placeholder = 'Type a command or search…',\n}: {\n  actions: Array<CommandMenuAction>\n\n  scopes?: Record<\n    string,\n    { actions: Array<CommandMenuAction>; placeholder: string; crumb?: string }\n  >\n  placeholder?: string\n}) {\n  const [open, setOpen] = useState(false)\n  const [scope, setScope] = useState<string | null>(null)\n  const [isClosing, setIsClosing] = useState(false)\n  const [ready, setReady] = useState(false)\n  const [sized, setSized] = useState(false)\n  const overlayRef = useRef<HTMLDivElement>(null)\n  const dialogRef = useRef<HTMLDivElement>(null)\n  const sizerRef = useRef<HTMLDivElement>(null)\n\n  useEffect(() => {\n    const dialog = dialogRef.current\n    const sizer = sizerRef.current\n    if (!open || !dialog || !sizer) {\n      setSized(false)\n      return\n    }\n\n    const style = getComputedStyle(dialog)\n\n    const chrome =\n      Number.parseFloat(style.borderTopWidth) + Number.parseFloat(style.borderBottomWidth)\n\n    let measured = false\n    const observer = new ResizeObserver(([entry]) => {\n      const box = entry?.borderBoxSize?.[0]\n      if (!box) return\n\n      dialog.style.height = `${box.blockSize + chrome}px`\n\n      if (measured) return\n      measured = true\n      requestAnimationFrame(() => setSized(true))\n    })\n\n    observer.observe(sizer)\n    return () => {\n      observer.disconnect()\n      dialog.style.removeProperty('height')\n    }\n  }, [open])\n\n  useEffect(() => {\n    if (!open) {\n      setReady(false)\n      return\n    }\n\n    const timeout = window.setTimeout(() => setReady(true), 180)\n    return () => window.clearTimeout(timeout)\n  }, [open])\n\n  const leaveScope = useCallback(() => {\n    setScope(null)\n  }, [])\n\n  const handleClose = useCallback(() => {\n    if (isClosing) return\n\n    setIsClosing(true)\n    overlayRef.current?.classList.add('closing')\n    dialogRef.current?.classList.add('closing')\n\n    window.setTimeout(() => {\n      setOpen(false)\n      setIsClosing(false)\n      overlayRef.current?.classList.remove('closing')\n      dialogRef.current?.classList.remove('closing')\n    }, 180)\n  }, [isClosing])\n\n  const openMenu = useCallback((next: string | null = null) => {\n    setScope(next)\n\n    const overlay = document.querySelector(OPEN_OVERLAY)\n    if (!overlay) {\n      setOpen(true)\n      return\n    }\n\n    document.dispatchEvent(new KeyboardEvent('keydown', { key: 'Escape', bubbles: true }))\n\n    const start = performance.now()\n    const openWhenGone = () => {\n      if (!overlay.isConnected || performance.now() - start > 500) setOpen(true)\n      else requestAnimationFrame(openWhenGone)\n    }\n    requestAnimationFrame(openWhenGone)\n  }, [])\n\n  useEffect(() => {\n    const onKeyDown = (event: KeyboardEvent) => {\n      if ((event.metaKey || event.ctrlKey) && event.key.toLowerCase() === 'k') {\n        event.preventDefault()\n        if (open) setOpen(false)\n        else openMenu()\n      } else if (event.key === 'Escape' && open && !isClosing) {\n        event.preventDefault()\n        handleClose()\n      }\n    }\n\n    const onOpen = (event: WindowEventMap[typeof OPEN_EVENT]) =>\n      openMenu(event.detail?.scope ?? null)\n\n    window.addEventListener('keydown', onKeyDown)\n    window.addEventListener(OPEN_EVENT, onOpen)\n    return () => {\n      window.removeEventListener('keydown', onKeyDown)\n      window.removeEventListener(OPEN_EVENT, onOpen)\n    }\n  }, [handleClose, isClosing, open, openMenu])\n\n  if (!open) return null\n\n  const scoped = scope ? scopes?.[scope] : undefined\n  const options = scoped?.actions ?? actions\n\n  return (\n    <div ref={overlayRef} className=\"command-overlay\" role=\"dialog\" aria-modal=\"true\">\n      <button\n        type=\"button\"\n        aria-label=\"Close command menu\"\n        className=\"command-backdrop\"\n        onClick={() => !isClosing && handleClose()}\n      />\n\n      <div\n        ref={dialogRef}\n        className={cn('command-dialog', ready && 'cmd-ready', sized && 'cmd-sized')}\n      >\n\n        <div ref={sizerRef}>\n          <CommandRoot\n            open={open}\n            onOpenChange={() => undefined}\n            options={options}\n            filter={filterActions}\n            onSelect={handleClose}\n            className=\"command-root\"\n          >\n            <ScopeBackspace onLeaveScope={leaveScope} />\n\n            <div className=\"command-header\">\n              <CommandInput\n                placeholder={scoped?.placeholder ?? placeholder}\n                className=\"command-input\"\n              />\n              <button type=\"button\" onClick={handleClose}>\n                Esc\n              </button>\n            </div>\n            <Breadcrumbs options={options} scope={scoped?.crumb} onLeaveScope={leaveScope} />\n            <CommandList className=\"command-list\" indicatorOffsetY={-8}>\n              <CommandEmpty className=\"command-empty\">No results found.</CommandEmpty>\n              <CommandOptions />\n            </CommandList>\n          </CommandRoot>\n        </div>\n      </div>\n    </div>\n  )\n}\n","type":"registry:ui","target":"components/ui/command-menu.tsx"},{"path":"lib/command-filter.ts","content":"export interface Searchable {\n  label: string\n  keywords?: Array<string>\n  disabled?: boolean\n}\n\nfunction subsequence(text: string, query: string) {\n  let at = -1\n  for (const char of query) {\n    at = text.indexOf(char, at + 1)\n    if (at === -1) return false\n  }\n  return true\n}\n\nexport function matches(option: Searchable, query: string) {\n  if (option.disabled) return false\n  if (subsequence(option.label.toLowerCase(), query)) return true\n  return option.keywords?.some((keyword) => keyword.toLowerCase().includes(query)) ?? false\n}\n\nexport function rank(label: string, query: string) {\n  const name = label.toLowerCase()\n  if (name === query) return 0\n  if (name.startsWith(query)) return 1\n  if (name.includes(query)) return 2\n  return 3\n}\n\nexport function search<T extends Searchable>(options: Array<T>, query: string): Array<T> {\n  const needle = query.trim().toLowerCase()\n  if (!needle) return options\n\n  return options\n    .filter((option) => matches(option, needle))\n    .toSorted((a, b) => rank(a.label, needle) - rank(b.label, needle))\n}\n","type":"registry:lib","target":"lib/command-filter.ts"}],"cssVars":{"light":{"--badge-blue-bg":"oklch(0.62 0.19 258 / 0.12)","--badge-blue-fg":"oklch(0.48 0.17 258)"},"dark":{"--badge-blue-bg":"oklch(0.62 0.19 258 / 0.2)","--badge-blue-fg":"oklch(0.72 0.145 258)"}},"css":{".command-overlay":{"position":"fixed","inset":"0","z-index":"80","display":"flex","align-items":"flex-start","justify-content":"center","padding-top":"18vh","background":"linear-gradient(to bottom, rgb(0 0 0 / 20%), rgb(0 0 0 / 25%))","backdrop-filter":"blur(2px)","animation":"command-overlay-in 200ms cubic-bezier(0.16, 1, 0.3, 1) forwards"},".command-overlay.closing":{"animation":"command-overlay-out 150ms cubic-bezier(0.4, 0, 1, 1) forwards"},".command-backdrop":{"position":"fixed","inset":"0","cursor":"default","border":"0","background":"transparent"},".command-dialog":{"position":"relative","width":"90%","max-width":"620px","overflow":"hidden","border":"1px solid var(--border)","border-radius":"14px","background":"var(--popover)","box-shadow":"var(--shadow-2xl)","--command-level-scale":"0.99","animation":"command-dialog-in 250ms cubic-bezier(0.16, 1, 0.3, 1) forwards"},".command-dialog.cmd-level-shift":{"animation":"command-level-shift 240ms cubic-bezier(0.23, 1, 0.32, 1)"},".command-dialog.closing":{"animation":"command-dialog-out 180ms cubic-bezier(0.4, 0, 1, 1) forwards"},".command-dialog.cmd-sized":{"transition":"height 240ms cubic-bezier(0.16, 1, 0.3, 1)"},"@media (prefers-reduced-motion: reduce)":{".command-dialog.cmd-sized":{"transition":"none"},".command-breadcrumbs > button":{"transition":"none"},".command-overlay, .command-dialog, .command-dialog.cmd-level-shift, .command-list.cmd-level-enter":{"animation":"none !important"},".command-dialog.cmd-ready .command-active-indicator":{"transition":"none"}},".command-root":{"display":"flex","flex-direction":"column"},".command-breadcrumbs":{"order":"-1","display":"flex","align-items":"center","gap":"0.5rem","padding":"0.75rem 1rem 0","color":"var(--muted-foreground)","font-size":"0.75rem"},".command-breadcrumbs > span, .command-breadcrumbs > button":{"padding":"0.25rem 0.5rem","border-radius":"0.375rem","background":"var(--muted)","color":"inherit","font":"inherit","border":"0"},".command-breadcrumbs > button":{"cursor":"pointer","transition":"color 150ms ease-out"},".command-breadcrumbs > button:hover, .command-breadcrumbs > button:focus-visible":{"color":"var(--foreground)"},".command-breadcrumbs > button:focus-visible":{"outline":"2px solid var(--ring)","outline-offset":"1px"},".command-header":{"display":"flex","align-items":"center","justify-content":"space-between","gap":"0.75rem","padding":"0.75rem 1rem","border-bottom":"1px solid var(--border)"},".command-header button":{"cursor":"pointer","border":"1px solid var(--border)","border-radius":"0.375rem","padding":"0.25rem 0.5rem","background":"transparent","color":"var(--muted-foreground)","font-size":"0.75rem"},".command-header button:hover":{"background":"var(--accent)","color":"var(--accent-foreground)"},".command-input":{"flex":"1","border":"0","outline":"0","background":"transparent","color":"var(--popover-foreground)","font-size":"0.95rem"},".command-input::placeholder":{"color":"var(--muted-foreground)"},".command-list":{"position":"relative","max-height":"380px","overflow-y":"auto","padding":"0.5rem"},".command-list.cmd-level-enter":{"animation":"command-level-enter 220ms cubic-bezier(0.23, 1, 0.32, 1)"},".command-active-indicator":{"position":"absolute","z-index":"0","border-radius":"8px","background":"var(--accent)","pointer-events":"none"},".command-dialog.cmd-ready .command-active-indicator":{"transition":"transform 150ms cubic-bezier(0.16, 1, 0.3, 1), width 150ms cubic-bezier(0.16, 1, 0.3, 1), height 150ms cubic-bezier(0.16, 1, 0.3, 1)"},".command-group-heading":{"position":"relative","z-index":"1","padding":"0.5rem 0.625rem 0.25rem","color":"var(--muted-foreground)","font-size":"0.75rem","font-weight":"500"},".command-option":{"position":"relative","z-index":"1","display":"flex","cursor":"pointer","align-items":"center","justify-content":"space-between","border-radius":"8px","padding":"0.625rem"},".command-option[aria-disabled='true']":{"cursor":"not-allowed","opacity":"0.4"},".command-option-content":{"display":"flex","min-width":"0","flex":"1","align-items":"center","gap":"0.625rem"},".command-option-icon":{"display":"flex","flex-shrink":"0","color":"var(--muted-foreground)"},".command-option-icon > svg":{"width":"18px","height":"18px","stroke-width":"1.8"},".command-option-label":{"min-width":"0","overflow":"hidden","color":"var(--popover-foreground)","font-size":"0.9rem","text-overflow":"ellipsis","white-space":"nowrap"},".command-option-shortcut":{"display":"flex","flex-shrink":"0","gap":"0.25rem","padding-inline-start":"0.5rem"},".command-shortcut-key":{"display":"flex","min-width":"1.4rem","height":"1.4rem","align-items":"center","justify-content":"center","border-radius":"0.3rem","padding":"0 0.35rem","background":"var(--muted)","color":"var(--muted-foreground)","font-family":"inherit","font-size":"0.7rem"},".command-option-hint":{"flex-shrink":"0","padding-inline-start":"0.5rem","color":"var(--muted-foreground)","font-size":"0.75rem"},".command-option-badge":{"flex-shrink":"0","margin-inline-start":"0.5rem","border-radius":"0.375rem","padding":"0.125rem 0.4rem","background":"var(--badge-blue-bg)","color":"var(--badge-blue-fg)","font-size":"0.6875rem","font-weight":"500","line-height":"1.4"},".command-option-more":{"width":"1rem","height":"1rem","flex-shrink":"0","color":"var(--muted-foreground)"},".command-option-more:dir(rtl)":{"scale":"-1 1"},".command-empty":{"display":"flex","height":"6rem","align-items":"center","justify-content":"center","color":"var(--muted-foreground)","font-size":"0.875rem"},"@keyframes command-level-enter":{"from":{"opacity":"0.5"},"to":{"opacity":"1"}},"@keyframes command-level-shift":{"0%":{"scale":"1","animation-timing-function":"cubic-bezier(0.32, 0, 0.24, 1)"},"40%":{"scale":"var(--command-level-scale)","animation-timing-function":"cubic-bezier(0.16, 1, 0.3, 1)"},"100%":{"scale":"1"}},"@keyframes command-overlay-in":{"from":{"opacity":"0"},"to":{"opacity":"1"}},"@keyframes command-overlay-out":{"from":{"opacity":"1"},"to":{"opacity":"0"}},"@keyframes command-dialog-in":{"from":{"transform":"scale(0.96) translateY(-4px)","opacity":"0"},"to":{"transform":"scale(1) translateY(0)","opacity":"1"}},"@keyframes command-dialog-out":{"from":{"transform":"scale(1)","opacity":"1"},"to":{"transform":"scale(0.97)","opacity":"0"}}}}