{
  "name": "command-palette",
  "type": "registry:ui",
  "framework": "react",
  "title": "Command Palette",
  "description": "A searchable quick switcher of grouped destinations and actions, driven from the keyboard.",
  "dependencies": [
    "radix-ui"
  ],
  "files": [
    {
      "path": "ui/command-palette.tsx",
      "content": "\"use client\"\n\nimport { Dialog as DialogPrimitive } from \"radix-ui\"\nimport * as React from \"react\"\n\nimport { cn } from \"@/lib/utils\"\n\n/** One row of the palette. */\nexport interface CommandItem {\n  /** Unique across the whole palette; handed back by `onSelect`. */\n  id: string\n  label: string\n  /** The quieter text beside the label. */\n  description?: string\n  /** A one-character sign before the label, such as `#` or `@`. */\n  prefix?: string\n  /** A key combination shown at the end of the row. */\n  shortcut?: string\n  /** Extra words the search should match that the row does not show. */\n  keywords?: readonly string[]\n}\n\nexport interface CommandGroup {\n  label: string\n  items: readonly CommandItem[]\n}\n\n/** The three lines of the footer. */\nexport interface CommandPaletteHints {\n  move: string\n  select: string\n  close: string\n}\n\nexport interface CommandPaletteProps extends Omit<\n  React.ComponentPropsWithoutRef<typeof DialogPrimitive.Content>,\n  \"onSelect\" | \"children\"\n> {\n  /** Whether the palette is shown. */\n  open?: boolean\n  /** Called with `false` on Escape, a press of the overlay or a selection. */\n  onOpenChange?: (open: boolean) => void\n  groups: readonly CommandGroup[]\n  /** Called with the chosen row; the palette then closes itself. */\n  onSelect?: (item: CommandItem) => void\n  /** Accessible name of the dialog. */\n  label?: string\n  placeholder?: string\n  /** Shown when the query matches nothing. */\n  emptyText?: string\n  /** The footer's wording. Pass `false` to leave the footer out. */\n  hints?: CommandPaletteHints | false\n  /**\n   * The element the overlay and panel are portalled into. Defaults to\n   * `document.body`. A palette opened below a `DirectionProvider` or a themed\n   * wrapper passes that element, so it keeps the direction and the palette.\n   */\n  container?: React.ComponentProps<typeof DialogPrimitive.Portal>[\"container\"]\n}\n\nconst defaultHints: CommandPaletteHints = {\n  move: \"to move\",\n  select: \"to select\",\n  close: \"to close\",\n}\n\nconst kbdClassName =\n  \"inline-flex h-5 min-w-5 items-center justify-center rounded-[var(--newt-radius-sm)] border border-newt-border bg-newt-bg-base px-1.5 font-[family-name:var(--newt-font-mono)] text-[11px] font-semibold text-newt-text-secondary\"\n\nfunction matches(item: CommandItem, query: string): boolean {\n  if (query === \"\") return true\n  return [item.label, item.description ?? \"\", ...(item.keywords ?? [])].some(\n    (text) => text.toLowerCase().includes(query)\n  )\n}\n\n/*\n * A search box over a list of places to go and things to do. The shell is a\n * Radix Dialog, which owns the focus trap, Escape and outside press, scroll\n * lock, and hiding the page from screen readers. Focus never leaves the\n * input: the arrow keys move a highlight through the list, and\n * `aria-activedescendant` tells a screen reader which row that is.\n */\nconst CommandPalette = React.forwardRef<\n  React.ComponentRef<typeof DialogPrimitive.Content>,\n  CommandPaletteProps\n>(\n  (\n    {\n      className,\n      open = false,\n      onOpenChange,\n      groups,\n      onSelect,\n      label = \"Command palette\",\n      placeholder = \"Where would you like to go?\",\n      emptyText = \"Nothing matches that.\",\n      hints = defaultHints,\n      container,\n      onOpenAutoFocus,\n      onCloseAutoFocus,\n      onEscapeKeyDown,\n      ...props\n    },\n    ref\n  ) => {\n    const id = React.useId()\n    const [query, setQuery] = React.useState(\"\")\n    const [active, setActive] = React.useState(0)\n    const close = React.useCallback(() => onOpenChange?.(false), [onOpenChange])\n    /* The element that had focus before the palette opened. */\n    const previousFocus = React.useRef<Element | null>(null)\n\n    /* Every opening starts from an empty query. */\n    const [wasOpen, setWasOpen] = React.useState(open)\n    if (open !== wasOpen) {\n      setWasOpen(open)\n      if (open) {\n        setQuery(\"\")\n        setActive(0)\n      }\n    }\n\n    const needle = query.trim().toLowerCase()\n    const shown = groups\n      .map((group) => ({\n        label: group.label,\n        items: group.items.filter((item) => matches(item, needle)),\n      }))\n      .filter((group) => group.items.length > 0)\n    const flat = shown.flatMap((group) => group.items)\n    const current = flat[Math.min(active, flat.length - 1)]\n    const optionId = (item: CommandItem) => `${id}-${item.id}`\n\n    /*\n     * The highlighted row is scrolled into view only when the keyboard moved\n     * it. The pointer moves the highlight too, and scrolling then would slide\n     * the list under the pointer and make it jitter.\n     */\n    const scrollToCurrent = React.useRef(false)\n    React.useEffect(() => {\n      if (!scrollToCurrent.current || current === undefined) return\n      scrollToCurrent.current = false\n      document\n        .getElementById(`${id}-${current.id}`)\n        ?.scrollIntoView({ block: \"nearest\" })\n    }, [current, id])\n\n    const choose = (item: CommandItem) => {\n      onSelect?.(item)\n      close()\n    }\n\n    return (\n      <DialogPrimitive.Root open={open} onOpenChange={onOpenChange}>\n        <DialogPrimitive.Portal container={container}>\n          <DialogPrimitive.Overlay className=\"fixed inset-0 z-[100] bg-newt-overlay\" />\n          <DialogPrimitive.Content\n            ref={ref}\n            aria-describedby={undefined}\n            className={cn(\n              \"fixed inset-x-0 top-[15vh] z-[100] mx-auto flex max-h-[70vh] w-[560px] max-w-[92vw] flex-col overflow-hidden rounded-[var(--newt-radius-lg)] border border-newt-border bg-newt-bg-elevated text-newt-text-primary shadow-elevation-high outline-none\",\n              className\n            )}\n            onOpenAutoFocus={(event) => {\n              // Save it first: the handler can move focus into the palette.\n              previousFocus.current = document.activeElement\n              onOpenAutoFocus?.(event)\n            }}\n            onCloseAutoFocus={(event) => {\n              onCloseAutoFocus?.(event)\n              if (event.defaultPrevented) return\n              /*\n               * The palette has no trigger for Radix to give focus back to,\n               * so it returns to the element that had it.\n               */\n              event.preventDefault()\n              if (previousFocus.current instanceof HTMLElement) {\n                previousFocus.current.focus()\n              }\n            }}\n            onEscapeKeyDown={(event) => {\n              onEscapeKeyDown?.(event)\n              /* While an input method composes, Escape cancels its candidate. */\n              if (event.isComposing) event.preventDefault()\n            }}\n            {...props}\n          >\n            <DialogPrimitive.Title className=\"sr-only\">\n              {label}\n            </DialogPrimitive.Title>\n            <input\n              type=\"text\"\n              role=\"combobox\"\n              aria-expanded=\"true\"\n              aria-controls={`${id}-list`}\n              aria-activedescendant={\n                current === undefined ? undefined : optionId(current)\n              }\n              aria-autocomplete=\"list\"\n              aria-label={placeholder}\n              placeholder={placeholder}\n              autoComplete=\"off\"\n              spellCheck={false}\n              value={query}\n              onChange={(event) => {\n                setQuery(event.target.value)\n                setActive(0)\n              }}\n              onKeyDown={(event) => {\n                /* While an input method composes, Enter confirms a candidate. */\n                if (event.nativeEvent.isComposing) return\n                if (event.key === \"Enter\") {\n                  event.preventDefault()\n                  if (current !== undefined) choose(current)\n                } else if (event.key === \"ArrowDown\" && flat.length > 0) {\n                  event.preventDefault()\n                  scrollToCurrent.current = true\n                  setActive(\n                    (flat.indexOf(current ?? flat[0]) + 1) % flat.length\n                  )\n                } else if (event.key === \"ArrowUp\" && flat.length > 0) {\n                  event.preventDefault()\n                  scrollToCurrent.current = true\n                  setActive(\n                    (flat.indexOf(current ?? flat[0]) - 1 + flat.length) %\n                      flat.length\n                  )\n                }\n              }}\n              className=\"mx-3 mt-3 rounded-[var(--newt-radius-sm)] border border-newt-border bg-newt-bg-input px-3.5 py-3 font-[family-name:var(--newt-font-sans)] text-base text-newt-text-primary placeholder:text-newt-text-muted focus:border-newt-brand focus:outline-none\"\n            />\n            <div\n              id={`${id}-list`}\n              role=\"listbox\"\n              aria-label={label}\n              className=\"flex-1 overflow-y-auto px-3 pb-3 pt-2\"\n            >\n              {shown.map((group) => (\n                <div key={group.label} role=\"group\" aria-label={group.label}>\n                  <div\n                    aria-hidden=\"true\"\n                    className=\"px-2 pb-1 pt-2.5 text-xs font-bold uppercase tracking-[0.02em] text-newt-text-muted\"\n                  >\n                    {group.label}\n                  </div>\n                  {group.items.map((item) => {\n                    const selected = item === current\n                    return (\n                      // oxlint-disable-next-line jsx-a11y/click-events-have-key-events -- options are driven from the input through aria-activedescendant; Enter there selects the highlighted one\n                      <div\n                        key={item.id}\n                        id={optionId(item)}\n                        role=\"option\"\n                        tabIndex={-1}\n                        aria-selected={selected}\n                        onClick={() => choose(item)}\n                        onPointerMove={() => setActive(flat.indexOf(item))}\n                        className={cn(\n                          \"flex cursor-pointer items-center gap-2 rounded-[var(--newt-radius-sm)] p-2 text-[15px] leading-5\",\n                          selected\n                            ? \"bg-newt-bg-active text-newt-text-primary\"\n                            : \"text-newt-text-secondary\"\n                        )}\n                      >\n                        <span\n                          aria-hidden=\"true\"\n                          className=\"w-5 shrink-0 text-center font-semibold text-newt-text-muted\"\n                        >\n                          {item.prefix}\n                        </span>\n                        <span className=\"truncate font-medium\">\n                          {item.label}\n                        </span>\n                        {item.description === undefined ? null : (\n                          <span className=\"truncate text-[13px] text-newt-text-muted\">\n                            {item.description}\n                          </span>\n                        )}\n                        {item.shortcut === undefined ? null : (\n                          <kbd className={cn(kbdClassName, \"ms-auto shrink-0\")}>\n                            {item.shortcut}\n                          </kbd>\n                        )}\n                      </div>\n                    )\n                  })}\n                </div>\n              ))}\n              {flat.length === 0 ? (\n                <p className=\"m-0 px-2 py-6 text-center text-sm text-newt-text-muted\">\n                  {emptyText}\n                </p>\n              ) : null}\n            </div>\n            {hints === false ? null : (\n              <footer\n                aria-hidden=\"true\"\n                className=\"flex flex-wrap gap-x-4 gap-y-1 border-t border-newt-border bg-newt-bg-surface px-4 py-2.5 text-xs text-newt-text-muted\"\n              >\n                <span className=\"inline-flex items-center gap-1\">\n                  <kbd className={kbdClassName}>↑</kbd>\n                  <kbd className={kbdClassName}>↓</kbd>\n                  {hints.move}\n                </span>\n                <span className=\"inline-flex items-center gap-1\">\n                  <kbd className={kbdClassName}>Enter</kbd>\n                  {hints.select}\n                </span>\n                <span className=\"inline-flex items-center gap-1\">\n                  <kbd className={kbdClassName}>Esc</kbd>\n                  {hints.close}\n                </span>\n              </footer>\n            )}\n          </DialogPrimitive.Content>\n        </DialogPrimitive.Portal>\n      </DialogPrimitive.Root>\n    )\n  }\n)\nCommandPalette.displayName = \"CommandPalette\"\n\nexport { CommandPalette }\n",
      "type": "registry:ui"
    }
  ]
}