{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "select",
  "title": "Select",
  "description": "A select that opens over its trigger, with the picked option on the value, and unfolds out of the trigger's own outline. One highlight glides between options; groups, long lists that scroll and multiple values.",
  "dependencies": [
    "class-variance-authority",
    "@ui-sdk/react"
  ],
  "registryDependencies": [
    "https://kinoui.com/r/panel.json",
    "https://kinoui.com/r/cut.json",
    "https://kinoui.com/r/scroll.json",
    "https://kinoui.com/r/list-rows.json",
    "https://kinoui.com/r/highlight.json",
    "https://kinoui.com/r/popup.json",
    "https://kinoui.com/r/input.json",
    "https://kinoui.com/r/refs.json",
    "https://kinoui.com/r/theme.json"
  ],
  "files": [
    {
      "path": "registry/components/kino/select.tsx",
      "content": "'use client'\n\nimport { InputInvalidIcon } from '@/registry/components/kino/input'\nimport { IconPlaceholder } from '@/registry/icons/icon-placeholder'\nimport { CutTooltip } from '@/registry/lib/cut'\nimport { followRows } from '@/registry/lib/highlight'\nimport {\n  listIndicatorClassName,\n  MenuHighlight,\n  menuIndicatorSlot,\n  menuGutterClassName,\n  menuLabelClassName,\n  menuRowVariants,\n  menuSeparatorClassName,\n  MenuTick,\n} from '@/registry/lib/list-rows'\nimport {\n  PanelHandle,\n  panelSheetBackdropClassName,\n  popupSheetClassName,\n  popupSheetPanelClassName,\n} from '@/registry/lib/panel'\nimport {\n  POPUP_COLLISION_PADDING,\n  popupGrowBoxClassName,\n  popupGrowContentClassName,\n  popupPlacedClassName,\n  popupSurfaceClassName,\n  usePopupPresentation,\n} from '@/registry/lib/popup'\nimport { mergeRefs } from '@/registry/lib/refs'\nimport { edgeFadeClassName, followEdges } from '@/registry/lib/scroll'\nimport {\n  Select as SelectPrimitive,\n  SelectContent as SelectContentPrimitive,\n  SelectGroup as SelectGroupPrimitive,\n  SelectGroupLabel as SelectGroupLabelPrimitive,\n  SelectItem as SelectItemPrimitive,\n  SelectItemIcon as SelectItemIconPrimitive,\n  SelectItemIndicator as SelectItemIndicatorPrimitive,\n  SelectList as SelectListPrimitive,\n  type SelectProps,\n  SelectSeparator as SelectSeparatorPrimitive,\n  SelectTrigger as SelectTriggerPrimitive,\n  SelectValue as SelectValuePrimitive,\n  SelectValueIcon as SelectValueIconPrimitive,\n} from '@ui-sdk/react/select'\nimport { cva, type VariantProps } from 'class-variance-authority'\nimport {\n  type ComponentProps,\n  createContext,\n  type ReactNode,\n  type RefCallback,\n  use,\n  useMemo,\n  useRef,\n} from 'react'\n\nimport { cn } from '@/lib/utils'\n\n/*\n * A select that opens over its trigger, with the picked row's text on the\n * trigger's value, and unfolds out of the trigger's own outline. Where there\n * is no room above and below, with nothing picked, or on touch, it opens\n * under the trigger and grows as the Popover does.\n */\n\ntype TriggerContextValue = {\n  /** The trigger, read when the list opens. */\n  trigger: { current: HTMLButtonElement | null }\n  /** Tells the presentation where the trigger is, to read the motion preference there. */\n  setTrigger: (element: HTMLElement | null) => void\n}\n\nconst TriggerContext = createContext<TriggerContextValue | null>(null)\n\n/**\n * In a Field, it takes the Field's label, name and state, as ui-sdk's\n * controls do. With `presentation='auto'`, a small touch screen opens it\n * as a sheet, unless motion is reduced around its trigger\n * (`popupPresentation`).\n */\nfunction Select<Value = string, Multiple extends boolean | undefined = false>(\n  props: SelectProps<Value, Multiple>,\n) {\n  const trigger = useRef<HTMLButtonElement>(null)\n  const [shown, setTrigger] = usePopupPresentation(props.presentation ?? 'popover')\n  const context = useMemo(() => ({ trigger, setTrigger }), [setTrigger])\n  return (\n    <TriggerContext value={context}>\n      <SelectPrimitive<Value, Multiple>\n        // Focus stays on the trigger, as on a native select and in the APG's\n        // select-only combobox; a sheet moves it into the rows, for touch screen readers.\n        virtualFocus={shown !== 'sheet'}\n        {...props}\n        presentation={shown}\n      />\n    </TriggerContext>\n  )\n}\n\nfunction SelectValue({\n  className,\n  ...props\n}: Omit<ComponentProps<typeof SelectValuePrimitive>, 'className'> & {\n  className?: string\n}) {\n  return (\n    <SelectValuePrimitive\n      data-cut\n      className={cn(\n        // A bare label truncates with an ellipsis; an icon and a label sit in a row.\n        'block min-w-0 flex-1 truncate data-placeholder:text-muted-foreground has-[>*]:flex has-[>*]:items-center has-[>*]:gap-2',\n        className,\n      )}\n      {...props}\n    />\n  )\n}\n\n/** The picked row's `SelectItemIcon`, before the value in the trigger. */\nfunction SelectValueIcon({\n  className,\n  ...props\n}: Omit<ComponentProps<typeof SelectValueIconPrimitive>, 'className'> & {\n  className?: string\n}) {\n  return (\n    <SelectValueIconPrimitive\n      className={cn('flex shrink-0 items-center', className)}\n      {...props}\n    />\n  )\n}\n\nconst selectTriggerVariants = cva(\n  \"flex w-fit min-w-0 items-center justify-between gap-2 rounded-(--control-radius) border border-input bg-transparent text-start text-sm whitespace-nowrap shadow-xs transition-[color,background-color,border-color,box-shadow] duration-(--motion-press-duration) ease-(--motion-press-fade-easing) select-none hover:bg-muted/50 focus-visible:border-ring focus-visible:ring-3 focus-visible:ring-ring/50 focus-visible:outline-hidden data-disabled:pointer-events-none data-disabled:opacity-50 data-open:bg-muted/50 aria-invalid:border-destructive aria-invalid:focus-visible:border-destructive aria-invalid:focus-visible:ring-destructive/20 forced-colors:aria-invalid:outline-2 forced-colors:aria-invalid:outline-offset-1 forced-colors:aria-invalid:outline-dashed dark:bg-input/30 dark:hover:bg-input/50 dark:aria-invalid:border-destructive/70 dark:aria-invalid:focus-visible:ring-destructive/40 [&_svg]:pointer-events-none [&_svg]:shrink-0 [&_svg:not([class*='size-'])]:size-4 [&_svg:not([class*='text-'])]:text-muted-foreground\",\n  {\n    variants: {\n      size: {\n        default: 'h-(--control-height) px-(--control-padding)',\n        sm: 'h-(--control-height-sm) px-(--control-padding)',\n      },\n    },\n    defaultVariants: { size: 'default' },\n  },\n)\n\nfunction SelectTrigger({\n  className,\n  size = 'default',\n  children,\n  ref,\n  ...props\n}: Omit<ComponentProps<typeof SelectTriggerPrimitive>, 'className' | 'children'> &\n  VariantProps<typeof selectTriggerVariants> & {\n    className?: string\n    children?: ReactNode\n  }) {\n  const context = use(TriggerContext)\n  const refs = useMemo(\n    () => mergeRefs(context?.trigger, context?.setTrigger, ref),\n    [context, ref],\n  )\n  return (\n    <CutTooltip\n      render={\n        <SelectTriggerPrimitive\n          ref={refs}\n          data-size={size}\n          className={cn(selectTriggerVariants({ size }), className)}\n          {...props}\n        >\n          {children}\n          {/* As on every text control, an invalid value is marked by more than its colour. */}\n          <InputInvalidIcon className='-me-1 pe-0' />\n          <span aria-hidden className='flex shrink-0'>\n            <IconPlaceholder\n              lucide='ChevronDownIcon'\n              tabler='IconChevronDown'\n              hugeicons='ArrowDown01Icon'\n              phosphor='CaretDownIcon'\n              remixicon='RiArrowDownSLine'\n              aria-hidden\n            />\n          </span>\n        </SelectTriggerPrimitive>\n      }\n    />\n  )\n}\n\ntype Edges = { top: number; right: number; bottom: number; left: number }\n\n/**\n * Whether the list sits over its trigger, as ui-sdk places it with\n * `alignItemWithTrigger`, rather than above or below it.\n */\nfunction overTrigger(list: Edges, trigger: Edges) {\n  return list.top < trigger.bottom && list.bottom > trigger.top\n}\n\n/**\n * How far, in px along the x axis, to move a list placed over its trigger so\n * its rows' content starts where the trigger's does: `textStart` and\n * `valueStart` are where each starts on screen (its left edge, or its right\n * in right to left), and `current` how far the list is moved now, which\n * `textStart` and `list` include. It stays `padding` inside the screen's\n * edges, `viewport` wide.\n */\nfunction triggerShift({\n  textStart,\n  valueStart,\n  list,\n  current,\n  viewport,\n  padding = POPUP_COLLISION_PADDING,\n}: {\n  textStart: number\n  valueStart: number\n  list: Pick<Edges, 'left' | 'right'>\n  current: number\n  viewport: number\n  padding?: number\n}) {\n  const wanted = current + valueStart - textStart\n  const least = padding - (list.left - current)\n  const most = viewport - padding - (list.right - current)\n  return Math.round(Math.min(Math.max(wanted, least), most))\n}\n\n/** Where a row's or a trigger's content starts on screen: past its tick and gap, or past its border and padding. */\nfunction contentStart(row: HTMLElement, rtl: boolean) {\n  const style = getComputedStyle(row)\n  const box = row.getBoundingClientRect()\n  const tick = row.querySelector(':scope > [data-menu-indicator]')\n  if (tick) {\n    const at = tick.getBoundingClientRect()\n    const gap = parseFloat(style.columnGap) || 0\n    return rtl ? at.left - gap : at.right + gap\n  }\n  return rtl\n    ? box.right - parseFloat(style.borderRightWidth) - parseFloat(style.paddingRight)\n    : box.left + parseFloat(style.borderLeftWidth) + parseFloat(style.paddingLeft)\n}\n\n/**\n * The `inset()` sides, in pixels, that show the trigger inside the list's\n * box: inside the trigger's 1px border, so its outline still shows on the\n * first frame, and past the tick's tip, which sits 5px inside its start (the\n * right in right to left).\n */\nfunction triggerInset(box: Edges, trigger: Edges, rtl: boolean): Edges {\n  return {\n    top: trigger.top - box.top + 1,\n    right: box.right - trigger.right + (rtl ? 6 : 1),\n    bottom: box.bottom - trigger.bottom + 1,\n    left: trigger.left - box.left + (rtl ? 1 : 6),\n  }\n}\n\n/*\n * Over its trigger, the list is marked `data-over-trigger`, moved so the\n * picked row's text lines up with the value (`--select-shift`) and widened\n * by as much (`--select-reach`), and its box unfolds from the trigger's own\n * outline (`--select-trigger-box`). Measured whenever ui-sdk places the list\n * and whenever it grows. Kept outside the component: the React Compiler can\n * drop side effects written inside one.\n */\nfunction trackTrigger(content: HTMLElement, box: HTMLElement, trigger: HTMLElement) {\n  const write = () => {\n    const placed = content.getBoundingClientRect()\n    if (!placed.width) return\n    const at = trigger.getBoundingClientRect()\n    const over = content.dataset.presentation !== 'sheet' && overTrigger(placed, at)\n    content.toggleAttribute('data-over-trigger', over)\n    if (!over) return\n    const rtl = getComputedStyle(content).direction === 'rtl'\n    const row = content.querySelector<HTMLElement>(\n      '[data-slot=select-item][data-selected]',\n    )\n    const current = parseFloat(content.style.getPropertyValue('--select-shift')) || 0\n    const shift = row\n      ? triggerShift({\n          textStart: contentStart(row, rtl),\n          valueStart: contentStart(trigger, rtl),\n          list: placed,\n          current,\n          viewport: document.documentElement.clientWidth,\n        })\n      : current\n    if (shift !== current) {\n      content.style.setProperty('--select-shift', `${shift}px`)\n      content.style.setProperty('--select-reach', `${Math.abs(shift)}px`)\n    }\n    // Read once the move and the reach are written, which the box takes at once.\n    const shown = box.getBoundingClientRect()\n    const { top, right, bottom, left } = triggerInset(shown, at, rtl)\n    box.style.setProperty(\n      '--select-trigger-box',\n      `inset(${top}px ${right}px ${bottom}px ${left}px round max(0px, calc(var(--control-radius-capped) - 1px)))`,\n    )\n  }\n  // A close that interrupts the unfold fades where the box is, rather than\n  // letting it grow on while it fades; once it has closed, the next open\n  // unfolds again.\n  const hold = () => {\n    box.style.clipPath =\n      content.hasAttribute('data-ending-style') &&\n      content.hasAttribute('data-over-trigger')\n        ? getComputedStyle(box).clipPath\n        : ''\n  }\n  write()\n  const placing = new MutationObserver((records) => {\n    write()\n    if (records.some((record) => record.attributeName !== 'style')) hold()\n  })\n  placing.observe(content, {\n    attributes: true,\n    attributeFilter: ['style', 'data-side', 'data-starting-style', 'data-ending-style'],\n  })\n  const resize = new ResizeObserver(write)\n  resize.observe(content)\n  return () => {\n    placing.disconnect()\n    resize.disconnect()\n  }\n}\n\n/** A ref for the rows: the highlight that glides between them, the list's fading edges and its place over the trigger. */\nfunction followSelect(trigger: { current: HTMLElement | null } | undefined) {\n  return (rows: HTMLDivElement | null) => {\n    const list = rows?.parentElement\n    const content = rows?.closest<HTMLElement>('[data-slot=select-content]')\n    const layer = rows?.querySelector<HTMLElement>(':scope > [data-slot=menu-highlight]')\n    if (!rows || !list || !content || !layer) return\n    const stops = [\n      followRows(rows, layer, { popup: content, bounds: list }),\n      followEdges(list, '--scroll-fade'),\n    ]\n    const box = list.parentElement\n    if (box && trigger?.current) stops.push(trackTrigger(content, box, trigger.current))\n    return () => {\n      for (const stop of stops) stop()\n    }\n  }\n}\n\n/*\n * Over the trigger, the box starts as the trigger's own outline with its\n * rows already showing, so the picked row sits on the value from the first\n * frame. It closes by fading where it is, quickly, so the trigger and its\n * new value show through at once and are never covered by a box folding\n * back over them. Elsewhere, the Popover's Grow.\n */\nconst selectUnfoldClassName =\n  'group-data-over-trigger/popup:group-data-starting-style/popup:[clip-path:var(--select-trigger-box)] group-data-over-trigger/popup:group-data-ending-style/popup:[clip-path:var(--popup-grow-to)] group-data-over-trigger/popup:group-data-ending-style/popup:opacity-0 group-data-over-trigger/popup:group-data-ending-style/popup:duration-[calc(var(--motion-exit-duration)*0.5)] motion-reduce:group-data-over-trigger/popup:group-data-starting-style/popup:[clip-path:var(--popup-grow-to)]'\n\n// Over the trigger the rows show at once and fade with the box; elsewhere\n// they fade in and out as a Popover's content does.\nconst selectRowsClassName =\n  'group-data-over-trigger/popup:transition-none group-data-over-trigger/popup:group-data-starting-style/popup:opacity-100 group-data-over-trigger/popup:group-data-ending-style/popup:opacity-100'\n\n/**\n * The list. Over the trigger by default, with the picked row on the\n * trigger's value; `side`, `align` and the offsets apply when it opens under\n * the trigger instead, or with `alignItemWithTrigger={false}`. `className`\n * goes to the rows inside the box; every other prop goes to ui-sdk's\n * content. A long list scrolls.\n */\nfunction SelectContent({\n  className,\n  children,\n  alignItemWithTrigger = true,\n  side = 'bottom',\n  align = 'start',\n  sideOffset = 6,\n  ...props\n}: Omit<ComponentProps<typeof SelectContentPrimitive>, 'className' | 'children'> & {\n  className?: string\n  children?: ReactNode\n}) {\n  const context = use(TriggerContext)\n  const rowsRef: RefCallback<HTMLDivElement> = useMemo(\n    () => followSelect(context?.trigger),\n    [context],\n  )\n  return (\n    <SelectContentPrimitive\n      alignItemWithTrigger={alignItemWithTrigger}\n      side={side}\n      align={align}\n      sideOffset={sideOffset}\n      collisionPadding={POPUP_COLLISION_PADDING}\n      arrowPadding={0}\n      className={cn(\n        popupPlacedClassName,\n        panelSheetBackdropClassName,\n        // Over the trigger, it starts a tick's width before the trigger, so\n        // its rows' text lines up with the value, and reaches as far past its end.\n        'flex max-h-[min(var(--available-height),24rem)] max-w-[min(20rem,var(--available-width))] min-w-(--anchor-width) flex-col text-sm data-over-trigger:min-w-[calc(var(--anchor-width)+var(--select-reach,0px))] data-over-trigger:[translate:var(--select-shift,0px)_0]',\n        popupSheetClassName,\n      )}\n      {...props}\n    >\n      {({ presentation }) => (\n        <div\n          className={cn(\n            'flex min-h-0 flex-1 flex-col',\n            presentation === 'sheet'\n              ? popupSheetPanelClassName\n              : cn(\n                  'relative overflow-hidden rounded-(--list-radius)',\n                  popupSurfaceClassName,\n                  popupGrowBoxClassName,\n                  selectUnfoldClassName,\n                ),\n          )}\n        >\n          {presentation === 'sheet' ? <PanelHandle /> : null}\n          <SelectListPrimitive\n            className={cn(\n              'scrollbar-thin relative min-h-0 flex-1 overflow-x-hidden overflow-y-auto overscroll-contain rounded-[inherit]',\n              edgeFadeClassName,\n            )}\n          >\n            <div\n              ref={rowsRef}\n              className={cn(\n                // The 4px around the rows is the highlight's margin, not a\n                // surface's padding, so it stays the same at every density.\n                'group/menu relative flex flex-col p-1',\n                menuGutterClassName,\n                presentation === 'sheet' ? null : popupGrowContentClassName,\n                selectRowsClassName,\n                className,\n              )}\n            >\n              <MenuHighlight />\n              {children}\n            </div>\n          </SelectListPrimitive>\n        </div>\n      )}\n    </SelectContentPrimitive>\n  )\n}\n\nfunction SelectGroup(props: ComponentProps<typeof SelectGroupPrimitive>) {\n  return <SelectGroupPrimitive {...props} />\n}\n\nfunction SelectGroupLabel({\n  className,\n  ...props\n}: Omit<ComponentProps<typeof SelectGroupLabelPrimitive>, 'className'> & {\n  className?: string\n}) {\n  return (\n    <SelectGroupLabelPrimitive className={cn(menuLabelClassName, className)} {...props} />\n  )\n}\n\n/**\n * A row: the menus' own, with the tick first, as on macOS, so the text lines\n * up with the trigger's value. Its text is what the trigger shows, or\n * `label` where it holds more than text. Put an icon in a `SelectItemIcon`\n * before the text, and the trigger's `SelectValueIcon` shows it.\n */\nfunction SelectItem({\n  className,\n  children,\n  ...props\n}: Omit<ComponentProps<typeof SelectItemPrimitive>, 'className' | 'children'> & {\n  className?: string\n  children?: ReactNode\n}) {\n  return (\n    <SelectItemPrimitive\n      data-menu-row=''\n      className={cn(menuRowVariants(), 'group/menu-row', className)}\n      {...props}\n    >\n      <span data-menu-indicator='' className={menuIndicatorSlot}>\n        <SelectItemIndicatorPrimitive keepMounted className={listIndicatorClassName}>\n          <MenuTick />\n        </SelectItemIndicatorPrimitive>\n      </span>\n      {children}\n    </SelectItemPrimitive>\n  )\n}\n\n/** A row's icon, before its text: the trigger shows the picked row's with `SelectValueIcon`. */\nfunction SelectItemIcon({\n  className,\n  ...props\n}: Omit<ComponentProps<typeof SelectItemIconPrimitive>, 'className'> & {\n  className?: string\n}) {\n  return (\n    <SelectItemIconPrimitive\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\nfunction SelectSeparator({\n  className,\n  ...props\n}: Omit<ComponentProps<typeof SelectSeparatorPrimitive>, 'className'> & {\n  className?: string\n}) {\n  return (\n    <SelectSeparatorPrimitive\n      className={cn(menuSeparatorClassName, className)}\n      {...props}\n    />\n  )\n}\n\nexport {\n  Select,\n  SelectContent,\n  SelectGroup,\n  SelectGroupLabel,\n  SelectItem,\n  SelectItemIcon,\n  SelectSeparator,\n  SelectTrigger,\n  SelectValue,\n  SelectValueIcon,\n  selectTriggerVariants,\n  overTrigger,\n  triggerInset,\n  triggerShift,\n}\n",
      "type": "registry:component",
      "target": "@components/kino/select.tsx"
    }
  ],
  "type": "registry:component"
}