{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "listbox",
  "title": "Listbox",
  "description": "A list of options that is always in view, by the WAI-ARIA listbox pattern: arrows, Home, End and typing a name move focus, and one option or many can be picked, with Shift for a range. Its rows and tick are the Select’s, and one highlight glides from row to row as in the Tree.",
  "dependencies": [
    "@ui-sdk/react"
  ],
  "registryDependencies": [
    "https://kinoui.com/r/scroll.json",
    "https://kinoui.com/r/highlight.json",
    "https://kinoui.com/r/list-rows.json",
    "https://kinoui.com/r/resize.json",
    "https://kinoui.com/r/theme.json"
  ],
  "files": [
    {
      "path": "registry/components/kino/listbox.tsx",
      "content": "'use client'\n\nimport {\n  followPointer,\n  itemMatching,\n  pickedRowClassName,\n  rowHighlightClassName,\n  tintedRowTextClassName,\n} from '@/registry/lib/highlight'\nimport {\n  listIndicatorClassName,\n  menuIndicatorSlot,\n  menuGutterClassName,\n  menuLabelClassName,\n  menuNoticeClassName,\n  menuRowVariants,\n  menuSeparatorClassName,\n  MenuTick,\n} from '@/registry/lib/list-rows'\nimport { followHeight } from '@/registry/lib/resize'\nimport { edgeFadeClassName, useEdgeFades } from '@/registry/lib/scroll'\nimport {\n  Listbox as ListboxPrimitive,\n  ListboxGroup as ListboxGroupPrimitive,\n  ListboxGroupLabel as ListboxGroupLabelPrimitive,\n  ListboxItem as ListboxItemPrimitive,\n  ListboxItemIcon as ListboxItemIconPrimitive,\n  ListboxItemIndicator as ListboxItemIndicatorPrimitive,\n  type ListboxProps,\n  ListboxSeparator as ListboxSeparatorPrimitive,\n} from '@ui-sdk/react/listbox'\nimport {\n  type ComponentProps,\n  createContext,\n  type ReactNode,\n  use,\n  useEffect,\n  useId,\n  useLayoutEffect,\n  useRef,\n  useState,\n} from 'react'\nimport { createPortal } from 'react-dom'\n\nimport { cn } from '@/lib/utils'\n\n/*\n * A list of options that is always in view: ui-sdk's Listbox, with the\n * Select's rows and tick, and the Tree's gliding highlight and picked tint.\n */\n\n/** Where `ListboxEmpty` shows, beside the list, which holds only options and groups, and whether the list has none. */\nconst EmptyContext = createContext<{\n  slot: HTMLElement | null\n  empty: boolean\n}>({\n  slot: null,\n  empty: false,\n})\n\nconst optionSelector = '[role=option]'\n\n/**\n * In a Field, the list takes its label, description, error, name and\n * state, as ui-sdk's controls do. Arrows move focus, and Space, Enter or a\n * press picks (`activation='manual'`); with `activation='automatic'` the\n * pick follows focus. `className` goes to the box around the list; give it\n * a `max-h-` and it scrolls.\n */\nfunction Listbox<Value = string, Multiple extends boolean | undefined = false>({\n  className,\n  children,\n  activation = 'manual',\n  ...props\n}: ListboxProps<Value, Multiple> & {\n  className?: string\n  children?: ReactNode\n}) {\n  const [slot, setSlot] = useState<HTMLDivElement | null>(null)\n  const [empty, setEmpty] = useState(false)\n  const emptyId = useId()\n  const fades = useEdgeFades<HTMLDivElement>()\n  const areaRef = useRef<HTMLDivElement>(null)\n  const contentRef = useRef<HTMLDivElement>(null)\n  const layerRef = useRef<HTMLSpanElement>(null)\n\n  // Whether it has no options, before it paints, so its notice never arrives late.\n  useLayoutEffect(() => {\n    const list = contentRef.current?.querySelector('[role=listbox]')\n    if (!list) return\n    const count = () => setEmpty(!list.querySelector(optionSelector))\n    count()\n    const rows = new MutationObserver(count)\n    rows.observe(list, { subtree: true, childList: true })\n    return () => rows.disconnect()\n  }, [])\n\n  useEffect(() => {\n    const area = areaRef.current\n    const content = contentRef.current\n    const layer = layerRef.current\n    if (!area || !content || !layer) return\n    // One highlight for every row, so it glides from row to row. The\n    // options' clip follows their height, inside the box that scrolls:\n    // options not yet shown, and the copies of ones still leaving, are\n    // clipped as the height glides, never something to scroll to.\n    const stopHeight = followHeight(area, content)\n    const stopHighlight = followPointer(area, layer, {\n      itemOf: itemMatching(area, optionSelector),\n      focus: true,\n    })\n    return () => {\n      stopHighlight()\n      stopHeight()\n    }\n  }, [])\n\n  return (\n    <div\n      data-listbox-box=''\n      className={cn(\n        // A box of rows, as a menu is, in the border every field has. It\n        // moves to the height of its options as they come and go, as the\n        // Combobox's box does, and they swap in place.\n        'relative flex min-w-0 flex-col overflow-hidden rounded-(--list-radius) border border-input bg-transparent text-sm shadow-xs transition-[border-color,box-shadow] duration-(--motion-press-duration) ease-(--motion-press-fade-easing) has-focus-visible:border-ring has-aria-invalid:border-destructive has-[[role=listbox][data-disabled]]:opacity-50 dark:bg-input/30 dark:has-aria-invalid:border-destructive/70 forced-colors:has-aria-invalid:outline-2 forced-colors:has-aria-invalid:outline-offset-1 forced-colors:has-aria-invalid:outline-dashed',\n        className,\n      )}\n    >\n      <div\n        ref={fades}\n        className={cn(\n          'scrollbar-thin min-h-0 flex-1 overflow-x-hidden overflow-y-auto overscroll-contain',\n          edgeFadeClassName,\n        )}\n      >\n        <div\n          ref={areaRef}\n          className='group/listbox relative isolate overflow-clip transition-[height] duration-(--motion-layout-duration) ease-(--motion-layout-easing) motion-reduce:transition-none'\n        >\n          <div ref={contentRef}>\n            <ListboxPrimitive<Value, Multiple>\n              activation={activation}\n              aria-describedby={empty ? emptyId : undefined}\n              // The 4px around the rows is the highlight's margin, as in a\n              // menu, so it stays the same at every density.\n              className={cn(\n                'group/menu flex min-w-0 flex-col p-1 focus-visible:outline-hidden',\n                menuGutterClassName,\n              )}\n              {...(props as ListboxProps<Value, Multiple>)}\n            >\n              <EmptyContext value={{ slot, empty }}>{children}</EmptyContext>\n            </ListboxPrimitive>\n            {/* Mounted from the start, so what it says when the list empties is heard. */}\n            <div\n              ref={setSlot}\n              id={emptyId}\n              role='status'\n              data-listbox-notice=''\n              className={menuNoticeClassName}\n            />\n          </div>\n          <span\n            ref={layerRef}\n            aria-hidden\n            data-listbox-highlight=''\n            className={rowHighlightClassName}\n          />\n        </div>\n      </div>\n    </div>\n  )\n}\n\n/**\n * An option: the Select's row, with its tick in the gutter before what it\n * shows. `label` is what typing its name matches, when its content isn't\n * plain text.\n */\nfunction ListboxItem<Value = string>({\n  className,\n  children,\n  ...props\n}: Omit<ComponentProps<typeof ListboxItemPrimitive<Value>>, 'className' | 'children'> & {\n  className?: string\n  children?: ReactNode\n}) {\n  return (\n    <ListboxItemPrimitive<Value>\n      data-menu-row=''\n      className={cn(\n        menuRowVariants(),\n        'group/menu-row transition-[color,background-color,box-shadow] duration-(--motion-press-duration) ease-(--motion-press-fade-easing)',\n        // The ring is inset, so the list's edge never clips it.\n        'focus-visible:ring-ring/50 focus-visible:ring-3 focus-visible:ring-inset focus-visible:outline-hidden forced-colors:[&[data-selected]]:focus-visible:outline-[CanvasText]',\n        // A picked row takes the Tree's tint, and picked rows next to each\n        // other join into one block.\n        pickedRowClassName,\n        '[&[data-selected]:has(+[data-selected])]:rounded-b-none [[data-selected]+&[data-selected]]:rounded-t-none',\n        // Muted text in a tinted row is darker, so it keeps 4.5:1 on the tint.\n        tintedRowTextClassName,\n        className,\n      )}\n      {...props}\n    >\n      {/* Picked shows by more than its tint: the Select's tick, in a slot kept for it. */}\n      <span data-menu-indicator='' className={menuIndicatorSlot}>\n        <ListboxItemIndicatorPrimitive keepMounted className={listIndicatorClassName}>\n          <MenuTick />\n        </ListboxItemIndicatorPrimitive>\n      </span>\n      {children}\n    </ListboxItemPrimitive>\n  )\n}\n\n/** An option's icon, before its text. */\nfunction ListboxItemIcon({\n  className,\n  ...props\n}: Omit<ComponentProps<typeof ListboxItemIconPrimitive>, 'className'> & {\n  className?: string\n}) {\n  return (\n    <ListboxItemIconPrimitive\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/** Options under one heading, named by its `ListboxGroupLabel`. */\nfunction ListboxGroup({\n  className,\n  ...props\n}: Omit<ComponentProps<typeof ListboxGroupPrimitive>, 'className'> & {\n  className?: string\n}) {\n  return <ListboxGroupPrimitive className={cn('flex flex-col', className)} {...props} />\n}\n\n/** A group's heading, lined up with the rows' text. */\nfunction ListboxGroupLabel({\n  className,\n  ...props\n}: Omit<ComponentProps<typeof ListboxGroupLabelPrimitive>, 'className'> & {\n  className?: string\n}) {\n  return (\n    <ListboxGroupLabelPrimitive\n      className={cn(menuLabelClassName, className)}\n      {...props}\n    />\n  )\n}\n\n/** A line between groups, across the list's 4px margin. */\nfunction ListboxSeparator({\n  className,\n  ...props\n}: Omit<ComponentProps<typeof ListboxSeparatorPrimitive>, 'className'> & {\n  className?: string\n}) {\n  return (\n    <ListboxSeparatorPrimitive\n      className={cn(menuSeparatorClassName, className)}\n      {...props}\n    />\n  )\n}\n\n/**\n * What the list says when it has no options, as when a search beside it\n * finds none: say what was looked for. It shows only while there are none,\n * beside the list, which may hold only options and groups.\n */\nfunction ListboxEmpty({ className, ...props }: ComponentProps<'span'>) {\n  const { slot, empty } = use(EmptyContext)\n  // Before the list is on the page (rendered on the server), it sits in the\n  // list and shows by CSS alone, so it is there when the page paints.\n  if (!slot) {\n    return (\n      <span\n        role='none'\n        data-listbox-empty=''\n        className={cn(\n          menuNoticeClassName,\n          'hidden group-[:not(:has([role=option]))]/menu:flex',\n          className,\n        )}\n        {...props}\n      />\n    )\n  }\n  if (!empty) return null\n  return createPortal(\n    <span data-listbox-empty='' className={className} {...props} />,\n    slot,\n  )\n}\n\nexport {\n  Listbox,\n  ListboxEmpty,\n  ListboxGroup,\n  ListboxGroupLabel,\n  ListboxItem,\n  ListboxItemIcon,\n  ListboxSeparator,\n}\n",
      "type": "registry:component",
      "target": "@components/kino/listbox.tsx"
    }
  ],
  "type": "registry:component"
}