{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "command",
  "title": "Command",
  "description": "A command menu: type to find a command, arrows to move, Enter to run it, on a page or in a dialog from ⌘K. A command that needs a choice opens a page that pushes the last one out across the start edge, and the box moves to the height of what matches.",
  "dependencies": [
    "@ui-sdk/react"
  ],
  "registryDependencies": [
    "https://kinoui.com/r/refs.json",
    "https://kinoui.com/r/cut.json",
    "https://kinoui.com/r/filter-list.json",
    "https://kinoui.com/r/list-rows.json",
    "https://kinoui.com/r/flow.json",
    "https://kinoui.com/r/resize.json",
    "https://kinoui.com/r/panel.json",
    "https://kinoui.com/r/swap.json",
    "https://kinoui.com/r/scroll.json",
    "https://kinoui.com/r/button.json",
    "https://kinoui.com/r/dialog.json",
    "https://kinoui.com/r/input.json",
    "https://kinoui.com/r/theme.json"
  ],
  "files": [
    {
      "path": "registry/components/kino/command.tsx",
      "content": "'use client'\n\nimport { Button } from '@/registry/components/kino/button'\nimport { Dialog, DialogContent, DialogTitle } from '@/registry/components/kino/dialog'\nimport {\n  searchLineClassName,\n  searchLineIcon,\n  searchLineInputClassName,\n} from '@/registry/components/kino/input'\nimport { IconPlaceholder } from '@/registry/icons/icon-placeholder'\nimport { CutTooltip } from '@/registry/lib/cut'\nimport {\n  FilterListRows,\n  filterListGroupLabelClassName,\n  filterListItemClassName,\n  filterListMatchClassName,\n  filterListNoticeClassName,\n  filterListScrollerClassName,\n  filterListSeparatorClassName,\n} from '@/registry/lib/filter-list'\nimport { leaveAsCopy } from '@/registry/lib/flow'\nimport { RowShortcut } from '@/registry/lib/list-rows'\nimport { panelFooterTint } from '@/registry/lib/panel'\nimport { mergeRefs } from '@/registry/lib/refs'\nimport { followHeight, withoutTransition } from '@/registry/lib/resize'\nimport { useEdgeFades } from '@/registry/lib/scroll'\nimport { Swap } from '@/registry/lib/swap'\nimport {\n  Command as CommandPrimitive,\n  CommandEmpty as CommandEmptyPrimitive,\n  CommandGroup as CommandGroupPrimitive,\n  CommandGroupLabel as CommandGroupLabelPrimitive,\n  CommandInput as CommandInputPrimitive,\n  CommandItem as CommandItemPrimitive,\n  CommandItemIcon as CommandItemIconPrimitive,\n  CommandItemText as CommandItemTextPrimitive,\n  CommandList as CommandListPrimitive,\n  CommandPage as CommandPagePrimitive,\n  type CommandProps as CommandPrimitiveProps,\n  CommandSeparator as CommandSeparatorPrimitive,\n  CommandShortcut as CommandShortcutPrimitive,\n  CommandStatus as CommandStatusPrimitive,\n  createCommandController,\n} from '@ui-sdk/react/command'\nimport {\n  type ComponentProps,\n  createContext,\n  type ReactNode,\n  useContext,\n  useEffect,\n  useMemo,\n  useRef,\n  useState,\n} from 'react'\n\nimport { cn } from '@/lib/utils'\n\n/*\n * A command menu: ui-sdk's Command, its list in line under the search, on\n * its own or in a dialog. The rows, groups and the one highlight that glides\n * between them are the filter-list lib's, as on the Combobox and the\n * Autocomplete. A command that needs a choice opens a page (its `page`, a\n * `CommandPage`'s `value`, which is the page's name): the name sits at the\n * start of the search, and Escape or Backspace on an empty search goes back.\n * Pages are a stack: a new page pushes the one before it out across the\n * start edge, and going back pulls it in again. The box moves to the height\n * of what matches.\n */\n\ninterface CommandContextValue {\n  pages: readonly string[]\n  back: () => void\n  /** The pages have changed since the menu opened: a page that mounts now arrives. */\n  moved: boolean\n}\n\nconst CommandContext = createContext<CommandContextValue | null>(null)\n\nfunction useCommand() {\n  const context = useContext(CommandContext)\n  if (!context) throw new Error('Command parts must be inside <Command>.')\n  return context\n}\n\ntype CommandProps = Omit<CommandPrimitiveProps, 'className' | 'children'> & {\n  className?: string\n  children?: ReactNode\n  /** The first page's name, announced when the person goes back to it. */\n  label?: string\n}\n\n/**\n * The menu. It keeps the commands whose name or keywords hold what was\n * typed, as the Combobox does (`filter`), and the best match is highlighted,\n * so Enter runs it.\n * A command runs its `onSelect`, then the menu's `onAction`, from a click,\n * Enter or its shortcut.\n */\nfunction Command({\n  className,\n  label = 'Commands',\n  autoHighlight = true,\n  filter = 'contains',\n  pages,\n  defaultPages = [],\n  onPagesChange,\n  controller,\n  children,\n  ...props\n}: CommandProps) {\n  const [ownPages, setOwnPages] = useState<string[]>(defaultPages)\n  const shown = pages ?? ownPages\n  const [ownController] = useState(createCommandController)\n  const control = controller ?? ownController\n  const page = shown.join('\\u0000')\n  // The page last seen, and whether it has changed since the menu opened:\n  // a menu that opens on a page doesn't play a move.\n  const [seen, setSeen] = useState({ page, moved: false })\n  if (page !== seen.page) setSeen({ page, moved: true })\n  const context = useMemo(\n    () => ({ pages: shown, back: () => void control.pop(), moved: seen.moved }),\n    [shown, control, seen.moved],\n  )\n  return (\n    <CommandPrimitive\n      controller={control}\n      autoHighlight={autoHighlight}\n      filter={filter}\n      pages={shown}\n      onPagesChange={(next, details) => {\n        setOwnPages(next)\n        onPagesChange?.(next, details)\n      }}\n      // The filter-list lib’s rows look for the box they sit in, and their\n      // highlight follows it while it is open, which a menu always is.\n      data-filter-list-content=''\n      data-open=''\n      // In a dialog, its footer stays on the dialog's bottom edge as the height changes.\n      data-panel='parts'\n      className={cn(\n        'group/command bg-popover text-popover-foreground relative flex min-w-0 flex-col overflow-clip rounded-(--surface-radius) text-sm ring-1 ring-foreground/10 forced-colors:outline',\n        panelFooterTint,\n        // In a dialog, the dialog is the box.\n        'in-data-[slot=dialog-content]:rounded-none in-data-[slot=dialog-content]:bg-transparent in-data-[slot=dialog-content]:ring-0 in-data-[slot=dialog-content]:forced-colors:outline-0',\n        className,\n      )}\n      {...props}\n    >\n      <CommandContext value={context}>\n        {children}\n        {/* Says which page opened, since a name change on the search isn't read out. */}\n        <div role='status' className='sr-only'>\n          {seen.moved ? (shown.at(-1) ?? label) : null}\n        </div>\n      </CommandContext>\n    </CommandPrimitive>\n  )\n}\n\n/** The current page's name: a press on it goes back. */\nfunction CommandPageName({ page }: { page: string }) {\n  const { back } = useCommand()\n  // The button goes with its page, so focus goes back to the search first.\n  function goBack(event: { currentTarget: HTMLElement }) {\n    event.currentTarget\n      .closest('[data-slot=command]')\n      ?.querySelector<HTMLElement>('[data-slot=command-input]')\n      ?.focus()\n    back()\n  }\n  return (\n    <CutTooltip\n      render={\n        <Button\n          variant='secondary'\n          size='xs'\n          data-command-back=''\n          onClick={goBack}\n          className='min-w-0'\n        >\n          <IconPlaceholder\n            lucide='ChevronLeftIcon'\n            tabler='IconChevronLeft'\n            hugeicons='ArrowLeft01Icon'\n            phosphor='CaretLeftIcon'\n            remixicon='RiArrowLeftSLine'\n            data-icon='inline-start'\n            aria-hidden\n            className='rtl:-scale-x-100'\n          />\n          <span className='sr-only'>Back from </span>\n          <span data-cut className='truncate'>\n            {page}\n          </span>\n        </Button>\n      }\n    />\n  )\n}\n\n/**\n * The search, as the menu's first line. On a page it starts with the page's\n * name, which goes back when pressed. Escape empties a search first, then\n * goes back a page, and only then closes a dialog around it.\n */\nfunction CommandInput({\n  className,\n  ...props\n}: Omit<ComponentProps<typeof CommandInputPrimitive>, 'className'> & {\n  className?: string\n}) {\n  const { pages } = useCommand()\n  const page = pages.at(-1)\n  return (\n    <div data-command-input-group='' className={searchLineClassName}>\n      {/* The search's icon gives way to the page's name, and back. */}\n      <Swap align='start' className='-ms-1 max-w-[50%] shrink-0 ps-1'>\n        {page ? <CommandPageName page={page} /> : searchLineIcon}\n      </Swap>\n      <CommandInputPrimitive\n        className={cn(\n          searchLineInputClassName,\n          // Its forced-colours outline drawn inside, where the dialog's edge can't clip it.\n          'forced-colors:-outline-offset-2',\n          className,\n        )}\n        {...props}\n      />\n    </div>\n  )\n}\n\n/**\n * The rows, with the highlight that glides between them: `CommandPage`s,\n * groups and commands. It scrolls past 24rem. Inside a dialog the dialog\n * follows the height; on a page the list does. A page that shows something\n * above its rows, such as an answer or a preview, gives it as `lead`: the\n * lead scrolls with its rows held under it, and the page moves as one.\n */\nfunction CommandList({\n  className,\n  lead,\n  children,\n  ref,\n  ...props\n}: Omit<ComponentProps<typeof CommandListPrimitive>, 'className' | 'children'> & {\n  className?: string\n  children?: ReactNode\n  /** What the page shows above its rows. */\n  lead?: ReactNode\n}) {\n  const { pages, moved } = useCommand()\n  const page = pages.join('\\u0000')\n  // Which way the last change of page went: on to a deeper page, or back.\n  const [went, setWent] = useState({ depth: pages.length, back: false })\n  if (went.depth !== pages.length)\n    setWent({ depth: pages.length, back: pages.length < went.depth })\n  const frameRef = useRef<HTMLDivElement>(null)\n  const innerRef = useRef<HTMLDivElement>(null)\n  const layerRef = useRef<HTMLDivElement>(null)\n  const fades = useEdgeFades<HTMLDivElement>()\n  const listRef = useMemo(() => mergeRefs(ref, fades), [ref, fades])\n  useEffect(() => {\n    const frame = frameRef.current\n    if (!frame || !innerRef.current) return\n    if (frame.closest('[data-slot=dialog-content]')) return\n    return followHeight(frame, innerRef.current)\n  }, [])\n  const list = (\n    <CommandListPrimitive\n      ref={listRef}\n      className={cn(\n        filterListScrollerClassName,\n        // Collapsed rather than hidden while nothing matches, so the input's\n        // aria-controls still names a listbox, as an expanded combobox must.\n        'group-has-[[data-slot=command-empty][data-empty]]/command:max-h-0 group-has-[[data-slot=command-empty][data-empty]]/command:overflow-hidden',\n      )}\n      {...props}\n    >\n      <FilterListRows className={className}>{children}</FilterListRows>\n    </CommandListPrimitive>\n  )\n  return (\n    <div\n      ref={frameRef}\n      data-command-list-frame=''\n      data-page-direction={went.back ? 'back' : 'forward'}\n      // Clipped at its bottom only on a page, where it follows the height: in\n      // a dialog the dialog's own edge clips what leaves.\n      className='group/pages relative overflow-x-clip not-in-data-[slot=dialog-content]:overflow-y-clip transition-[height] duration-(--motion-layout-duration) ease-(--motion-layout-easing) motion-reduce:transition-none'\n    >\n      <div\n        ref={innerRef}\n        className={cn(\n          'flex flex-col',\n          // A page that leads with its own content, such as an answer, takes more of the screen.\n          lead ? 'max-h-[min(32rem,70dvh)]' : 'max-h-[min(24rem,60dvh)]',\n        )}\n      >\n        {/*\n         * A page of its own for each page of commands, around the list that\n         * scrolls: it moves without the list scrolling sideways to follow its\n         * highlighted row, and its highlight starts afresh on its first row.\n         */}\n        <div\n          key={page}\n          ref={pageMotion(layerRef, page)}\n          data-command-page={page}\n          data-fresh={moved ? '' : undefined}\n          className={pageClassName}\n        >\n          {lead ? <CommandLead lead={lead}>{list}</CommandLead> : list}\n        </div>\n      </div>\n      <div\n        ref={layerRef}\n        aria-hidden\n        className='pointer-events-none absolute inset-0 overflow-x-clip not-in-data-[slot=dialog-content]:overflow-y-clip'\n      />\n    </div>\n  )\n}\n\n/** A page's lead, which scrolls in the room its rows leave, and its rows under it at their full height. */\nfunction CommandLead({ lead, children }: { lead: ReactNode; children: ReactNode }) {\n  const fades = useEdgeFades<HTMLDivElement>()\n  return (\n    <div\n      data-command-lead-page=''\n      className='flex min-h-0 flex-1 flex-col *:[[role=listbox]]:flex-none *:[[role=listbox]]:not-has-[[role=option]]:hidden'\n    >\n      {/* A highlight that lands on a row never scrolls what the lead shows. */}\n      <div\n        ref={fades}\n        data-command-lead-scroller=''\n        className={cn(filterListScrollerClassName, 'relative')}\n      >\n        <div data-command-lead=''>{lead}</div>\n      </div>\n      {children}\n    </div>\n  )\n}\n\n/*\n * How a page of commands gives way to the next. A page's rows go the moment\n * it changes, so the old page leaves as a copy, through the flow lib's\n * `leaveAsCopy`, in a layer outside the list that scrolls. Kept outside\n * components: the React Compiler drops a forced reflow written inside one.\n */\n\n/** The properties a page moves on. */\nconst MOVING = ['opacity', 'translate', 'scale'] as const\n\n/** The values `element` is drawn with now, part way through a transition or not. */\nfunction drawnNow(element: Element) {\n  const style = getComputedStyle(element)\n  return MOVING.map((property) => [property, style.getPropertyValue(property)] as const)\n}\n\n/** Puts `drawn` on `element`, and returns the properties it set. */\nfunction hold(element: HTMLElement, drawn: ReturnType<typeof drawnNow>) {\n  for (const [property, value] of drawn) element.style.setProperty(property, value)\n  return drawn.map(([property]) => property)\n}\n\n/**\n * A ref for a page of commands. A page that arrives while a copy of it is\n * still leaving (a change turned round) takes over from where the copy is\n * drawn. A page that goes leaves as a copy in `layer`, from where it is\n * drawn; the copy goes once its leaving styles (`data-leaving`) have played.\n */\nfunction pageMotion(layer: { current: HTMLElement | null }, key: string) {\n  return (page: HTMLElement | null) => {\n    if (!page) return\n    const returning = layer.current?.querySelector<HTMLElement>(\n      `:scope > [data-command-page=\"${CSS.escape(key)}\"]`,\n    )\n    if (returning && page.hasAttribute('data-fresh')) {\n      const drawn = drawnNow(returning)\n      returning.remove()\n      let held: string[] = []\n      withoutTransition(page, () => {\n        held = hold(page, drawn)\n      })\n      // Nothing is leaving now, so it turns back at once, with no wait.\n      page.style.transitionDelay = '0s'\n      for (const property of held) page.style.removeProperty(property)\n    }\n    return () => {\n      if (layer.current) leavePage(page, layer.current)\n    }\n  }\n}\n\n/** What scrolls in a page: its rows, and its lead. */\nconst SCROLLERS = '[role=listbox], [data-command-lead-scroller]'\n\nfunction leavePage(page: HTMLElement, layer: HTMLElement) {\n  // Read now, while the page is still laid out: once removed it has no box.\n  const box = page.getBoundingClientRect()\n  const area = layer.getBoundingClientRect()\n  const drawn = drawnNow(page)\n  const scrolled = [...page.querySelectorAll(SCROLLERS)].map((list) => list.scrollTop)\n  // A ref let go but kept (React's second look in development), or a menu\n  // that closed with its page, has nothing leaving.\n  queueMicrotask(() => {\n    if (page.isConnected || !layer.isConnected) return\n    for (const node of page.querySelectorAll('[data-highlighted]'))\n      node.removeAttribute('data-highlighted')\n    page.removeAttribute('data-fresh')\n    leaveAsCopy(page, {\n      parent: layer,\n      style: {\n        insetInlineStart: '0',\n        top: `${box.top - area.top}px`,\n        width: `${box.width}px`,\n        height: `${box.height}px`,\n      },\n      from: (copy) => {\n        // A copy starts scrolled to the top: put its lists where they were.\n        for (const [index, list] of copy.querySelectorAll(SCROLLERS).entries())\n          list.scrollTo({ top: scrolled[index] ?? 0, behavior: 'instant' })\n        return hold(copy, drawn).map((property) => [copy, property] as const)\n      },\n    })\n  })\n}\n\n/*\n * Both pages move together on `layout`, the new one from the end edge going\n * on and from the start edge going back, and each fades as it travels, so a\n * shorter `--command-slide` (on the menu or anything above it) still reads as\n * one page giving way to the next. With less motion nothing travels: the new\n * page fades in and the old one goes at once.\n */\nconst pageClassName =\n  'min-h-0 [--page-from:1] [--page-to:-1] rtl:[--page-sign:-1] group-data-[page-direction=back]/pages:[--page-from:-1] group-data-[page-direction=back]/pages:[--page-to:1] flex flex-col transition-[translate,opacity] duration-(--motion-layout-duration) ease-(--motion-layout-easing) data-fresh:starting:translate-x-[calc(var(--command-slide,100%)*var(--page-from)*var(--page-sign,1))] data-fresh:starting:opacity-0 data-leaving:translate-x-[calc(var(--command-slide,100%)*var(--page-to)*var(--page-sign,1))] data-leaving:opacity-0 motion-reduce:transition-opacity motion-reduce:data-fresh:starting:translate-x-0 motion-reduce:data-leaving:translate-x-0 motion-reduce:data-leaving:transition-none'\n\n/** The commands one page holds; `value` is the page's name. The first page has none. */\nfunction CommandPage(props: ComponentProps<typeof CommandPagePrimitive>) {\n  return <CommandPagePrimitive {...props} />\n}\n\nfunction CommandGroup(props: ComponentProps<typeof CommandGroupPrimitive>) {\n  return <CommandGroupPrimitive {...props} />\n}\n\nfunction CommandGroupLabel({\n  className,\n  ...props\n}: Omit<ComponentProps<typeof CommandGroupLabelPrimitive>, 'className'> & {\n  className?: string\n}) {\n  return (\n    <CommandGroupLabelPrimitive\n      className={cn(filterListGroupLabelClassName, className)}\n      {...props}\n    />\n  )\n}\n\n/** A command: the menus' row. It runs on a click, on Enter or on its `shortcut`. */\nfunction CommandItem({\n  className,\n  ...props\n}: Omit<ComponentProps<typeof CommandItemPrimitive>, 'className'> & {\n  className?: string\n}) {\n  return (\n    <CommandItemPrimitive\n      data-menu-row=''\n      className={cn(\n        filterListItemClassName,\n        'data-pending:text-muted-foreground',\n        className,\n      )}\n      {...props}\n    />\n  )\n}\n\n/** A command's text, with the part that matched the search in bold. */\nfunction CommandItemText({\n  className,\n  ...props\n}: Omit<ComponentProps<typeof CommandItemTextPrimitive>, 'className'> & {\n  className?: string\n}) {\n  return (\n    <CommandItemTextPrimitive\n      className={cn(filterListMatchClassName, className)}\n      {...props}\n    />\n  )\n}\n\n/** A command's icon, before its text. */\nfunction CommandItemIcon({\n  className,\n  ...props\n}: Omit<ComponentProps<typeof CommandItemIconPrimitive>, 'className'> & {\n  className?: string\n}) {\n  return (\n    <CommandItemIconPrimitive\n      className={cn(\n        \"flex shrink-0 items-center [&_svg:not([class*='text-'])]:text-muted-foreground\",\n        className,\n      )}\n      {...props}\n    />\n  )\n}\n\n/** A command's shortcut, at the row's end: its `shortcut`, or `Kbd` keys, as in the menus. */\nfunction CommandShortcut(props: ComponentProps<typeof CommandShortcutPrimitive>) {\n  return <CommandShortcutPrimitive render={<RowShortcut />} {...props} />\n}\n\nfunction CommandSeparator({\n  className,\n  ...props\n}: Omit<ComponentProps<typeof CommandSeparatorPrimitive>, 'className'> & {\n  className?: string\n}) {\n  return (\n    <CommandSeparatorPrimitive\n      className={cn(filterListSeparatorClassName, className)}\n      {...props}\n    />\n  )\n}\n\n/** What shows when nothing matches, announced as it appears: say what was looked for. */\nfunction CommandEmpty({\n  className,\n  ...props\n}: Omit<ComponentProps<typeof CommandEmptyPrimitive>, 'className'> & {\n  className?: string\n}) {\n  return (\n    <CommandEmptyPrimitive\n      className={cn(filterListNoticeClassName, className)}\n      {...props}\n    />\n  )\n}\n\n/** Announces a search that is running, or how many commands it found. */\nfunction CommandStatus({\n  className,\n  ...props\n}: Omit<ComponentProps<typeof CommandStatusPrimitive>, 'className'> & {\n  className?: string\n}) {\n  return (\n    <CommandStatusPrimitive\n      className={cn(filterListNoticeClassName, className)}\n      {...props}\n    />\n  )\n}\n\n/** The line under the rows, for the keys that work here. */\nfunction CommandFooter({ className, ...props }: ComponentProps<'div'>) {\n  return (\n    <div\n      data-command-footer=''\n      data-panel='footer'\n      className={cn(\n        // Its own tint, over a page that leaves, so the rows pass under it as the box resizes; it rides\n        // the dialog's bottom edge on `layout`.\n        'relative bg-(--panel-footer) transition-[translate] duration-(--motion-layout-duration) ease-(--motion-layout-easing) motion-reduce:transition-none',\n        'text-muted-foreground flex min-h-(--control-height) shrink-0 flex-wrap items-center gap-x-4 gap-y-1 border-t px-[calc(var(--control-padding)+0.25rem)] py-1.5 text-xs',\n        className,\n      )}\n      {...props}\n    />\n  )\n}\n\n/** A key and what it does, in the footer. */\nfunction CommandHint({\n  className,\n  keys,\n  children,\n  ...props\n}: ComponentProps<'span'> & { keys: ReactNode }) {\n  return (\n    <span\n      data-command-hint=''\n      className={cn('inline-flex items-center gap-1.5', className)}\n      {...props}\n    >\n      {keys}\n      {children}\n    </span>\n  )\n}\n\n/**\n * The menu in a dialog, high on the screen so the search stays where it is\n * as the rows below it come and go. `title` names it for screen readers. It\n * opens and closes at once, with no move: a menu opened by a key many times\n * a day answers as fast as the key.\n */\nfunction CommandDialog({\n  title = 'Command menu',\n  className,\n  contentProps,\n  children,\n  ...props\n}: Omit<ComponentProps<typeof Dialog>, 'children'> & {\n  title?: string\n  className?: string\n  /** Props for the dialog's popup, such as its own `data-*` attributes. */\n  contentProps?: Omit<\n    ComponentProps<typeof DialogContent>,\n    'children' | 'className' | 'showCloseButton'\n  >\n  children?: ReactNode\n}) {\n  return (\n    <Dialog {...props}>\n      <DialogContent\n        {...contentProps}\n        data-instant=''\n        showCloseButton={false}\n        className={cn('sm:mt-[12vh] sm:mb-auto sm:max-w-xl', className)}\n      >\n        <DialogTitle className='sr-only'>{title}</DialogTitle>\n        {children}\n      </DialogContent>\n    </Dialog>\n  )\n}\n\nexport {\n  Command,\n  CommandDialog,\n  CommandEmpty,\n  CommandFooter,\n  CommandGroup,\n  CommandGroupLabel,\n  CommandHint,\n  CommandInput,\n  CommandItem,\n  CommandItemIcon,\n  CommandItemText,\n  CommandList,\n  CommandPage,\n  CommandSeparator,\n  CommandShortcut,\n  CommandStatus,\n}\nexport type { CommandProps }\n",
      "type": "registry:component",
      "target": "@components/kino/command.tsx"
    }
  ],
  "type": "registry:component"
}