{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "segmented-control",
  "title": "Segmented control",
  "description": "One of a few options, picked from a row: a radio group a form submits, drawn as the Tabs are, with the pill sliding to the pick and a backdrop under the pointer.",
  "dependencies": [
    "@ui-sdk/react",
    "@ui-sdk/core"
  ],
  "registryDependencies": [
    "https://kinoui.com/r/theme.json",
    "https://kinoui.com/r/highlight.json",
    "https://kinoui.com/r/pill.json",
    "https://kinoui.com/r/refs.json",
    "https://kinoui.com/r/scroll.json",
    "https://kinoui.com/r/swap.json"
  ],
  "files": [
    {
      "path": "registry/components/kino/segmented-control.tsx",
      "content": "'use client'\n\nimport { followPointer, highlightClassName, itemMatching } from '@/registry/lib/highlight'\nimport {\n  pillBackdropClassName,\n  pillClassName,\n  pillItemClassName,\n  pillItemLayout,\n  pillRowClassName,\n  pillTrackClassName,\n} from '@/registry/lib/pill'\nimport { markMounted, mergeRefs } from '@/registry/lib/refs'\nimport { revealInRow } from '@/registry/lib/scroll'\nimport { SwapIcon } from '@/registry/lib/swap'\nimport {\n  SegmentedControlIndicator,\n  SegmentedControlItem as SegmentedControlItemPrimitive,\n  SegmentedControl as SegmentedControlPrimitive,\n} from '@ui-sdk/react/segmented-control'\nimport {\n  type ComponentProps,\n  createContext,\n  type ReactNode,\n  use,\n  useEffect,\n  useMemo,\n  useRef,\n} from 'react'\n\nimport { cn } from '@/lib/utils'\n\n/*\n * One of a few options, picked from a row: ui-sdk's Segmented control, which\n * a form submits, drawn as the Tabs are, with the pill sliding to the pick.\n * A row wider than its column scrolls, as the Tabs' list does. `vertical`\n * stacks the options in a column, as the vertical Tabs do, labels read from\n * the start.\n */\n\nconst SEGMENT = '[data-slot=segmented-control-item]'\n\n// The row and the pill's copy of it share this, so every label sits exactly on its twin.\nconst segmentedControlRow = cn(\n  // The control's own `data-orientation`, and the pill's copy inside it.\n  'flex w-max min-w-full data-[orientation=vertical]:w-auto data-[orientation=vertical]:flex-col group-data-[orientation=vertical]/segmented-control:w-auto group-data-[orientation=vertical]/segmented-control:flex-col',\n  pillRowClassName,\n)\n\n/** True inside the pill's copy of the row, where a segment is only its label. */\nconst SegmentedControlCopyContext = createContext(false)\n\n/** The checked value, from ui-sdk's state, so the pill's copy of the row knows it. */\nconst SegmentedControlValueContext = createContext<string | null>(null)\n\n/** Whether the segment around it is picked, for its icon. */\nconst SegmentedControlCheckedContext = createContext(false)\n\nfunction SegmentedControl<Value extends string>({\n  ref,\n  className,\n  children,\n  orientation = 'horizontal',\n  ...props\n}: Omit<\n  ComponentProps<typeof SegmentedControlPrimitive<Value>>,\n  'className' | 'children' | 'orientation'\n> & {\n  className?: string\n  children?: ReactNode\n  /** A row, or a column read down its start edge. */\n  orientation?: 'horizontal' | 'vertical'\n}) {\n  const scrollerRef = useRef<HTMLDivElement>(null)\n  const groupRefs = useMemo(() => mergeRefs(markMounted, ref), [ref])\n  const backdropRef = useRef<HTMLSpanElement>(null)\n\n  useEffect(() => {\n    const scroller = scrollerRef.current\n    const group = backdropRef.current?.parentElement\n    if (!scroller || !group || !backdropRef.current) return\n    // Behind the segment under the pointer, or the one with visible focus.\n    const stopHover = followPointer(group, backdropRef.current, {\n      itemOf: itemMatching(group, SEGMENT),\n      focus: true,\n    })\n    const stopRow = revealInRow(scroller, {\n      item: SEGMENT,\n      picked: 'data-checked',\n      fade: '--segmented-control-fade',\n    })\n    return () => {\n      stopHover()\n      stopRow()\n    }\n  }, [])\n\n  return (\n    <div\n      data-slot='segmented-control-track'\n      className={cn(\n        pillTrackClassName,\n        // An invalid Field around it rings it, and adds a dashed outline in forced colours.\n        'w-fit max-w-full group-data-invalid/field:ring-1 group-data-invalid/field:ring-destructive forced-colors:group-data-invalid/field:outline-2 forced-colors:group-data-invalid/field:outline-dashed',\n        className,\n      )}\n    >\n      <div\n        ref={scrollerRef}\n        data-slot='segmented-control-scroller'\n        className={cn(\n          'min-w-0',\n          // A row wider than its column scrolls; a column grows down the page.\n          orientation === 'horizontal' &&\n            'scrollbar-none overflow-x-auto [mask-image:linear-gradient(to_right,transparent,black_var(--segmented-control-fade-left,0px),black_calc(100%-var(--segmented-control-fade-right,0px)),transparent)]',\n        )}\n      >\n        <SegmentedControlPrimitive<Value>\n          ref={groupRefs}\n          orientation={orientation}\n          className={cn(segmentedControlRow, 'group/segmented-control relative isolate')}\n          {...props}\n        >\n          {(state) => (\n            <SegmentedControlValueContext value={state.value}>\n              {children}\n              <span\n                ref={backdropRef}\n                aria-hidden\n                data-slot='segmented-control-backdrop'\n                className={cn(highlightClassName, pillBackdropClassName)}\n              />\n              {/*\n               * The pill: as large as the row, and only its window moves, on `morph`. ui-sdk\n               * measures the checked segment once the page runs: until then the window is\n               * empty and the checked segment draws itself, and the first placement doesn't move.\n               * Its end and bottom come from the segment's own size, as the Tabs' pill's do.\n               */}\n              <SegmentedControlIndicator\n                // Whatever the row holds is copied here, so nothing in the copy may take focus.\n                inert\n                className={cn(\n                  pillClassName,\n                  'pointer-events-none absolute inset-0 z-10 [clip-path:inset(var(--segmented-control-indicator-top,0px)_calc(100%_-_var(--segmented-control-indicator-left,0px)_-_var(--segmented-control-indicator-width,0px))_calc(100%_-_var(--segmented-control-indicator-top,0px)_-_var(--segmented-control-indicator-height,100%))_var(--segmented-control-indicator-left,0px)_round_max(0px,calc(var(--list-radius)-2px)))] transition-[clip-path] duration-(--motion-morph-duration) ease-(--motion-morph-easing) data-[activation-direction=none]:transition-none motion-reduce:transition-none group-data-disabled/segmented-control:opacity-50',\n                )}\n              >\n                <span className={cn(segmentedControlRow, 'h-full')}>\n                  <SegmentedControlCopyContext value={true}>\n                    {children}\n                  </SegmentedControlCopyContext>\n                </span>\n              </SegmentedControlIndicator>\n            </SegmentedControlValueContext>\n          )}\n        </SegmentedControlPrimitive>\n      </div>\n    </div>\n  )\n}\n\nconst segmentLayout = cn(\n  pillItemLayout,\n  'shrink-0',\n  // A column reads down its start edge, and a label longer than it wraps.\n  'group-data-[orientation=vertical]/segmented-control:h-auto group-data-[orientation=vertical]/segmented-control:min-h-[calc(var(--control-height)_-_6px)] group-data-[orientation=vertical]/segmented-control:justify-start group-data-[orientation=vertical]/segmented-control:py-1.5 group-data-[orientation=vertical]/segmented-control:text-start group-data-[orientation=vertical]/segmented-control:whitespace-normal',\n)\n\nfunction SegmentedControlItem({\n  className,\n  children,\n  ...props\n}: Omit<\n  ComponentProps<typeof SegmentedControlItemPrimitive>,\n  'className' | 'children' | 'render'\n> & {\n  className?: string\n  children?: ReactNode\n}) {\n  const checked = use(SegmentedControlValueContext) === props.value\n  const content = (\n    <SegmentedControlCheckedContext value={checked}>\n      {children}\n    </SegmentedControlCheckedContext>\n  )\n  if (use(SegmentedControlCopyContext)) {\n    return <span className={cn(segmentLayout, className)}>{content}</span>\n  }\n  return (\n    <SegmentedControlItemPrimitive\n      className={cn(\n        segmentLayout,\n        pillItemClassName,\n        // Until the page runs and the pill is placed, the pick draws itself.\n        '[[data-slot=segmented-control]:not([data-mounted])>&]:data-checked:bg-primary [[data-slot=segmented-control]:not([data-mounted])>&]:data-checked:text-primary-foreground',\n        className,\n      )}\n      {...props}\n    >\n      {content}\n    </SegmentedControlItemPrimitive>\n  )\n}\n\n/**\n * A segment's icon: `children`, and `checkedIcon` while the segment is\n * picked. It rolls to the other icon as a Tab's icon does. A segment whose\n * icon stays the same takes it as a plain child: rolling to itself shows\n * nothing change.\n */\nfunction SegmentedControlIcon({\n  checkedIcon,\n  children,\n}: {\n  checkedIcon: ReactNode\n  children: ReactNode\n}) {\n  return (\n    <SwapIcon\n      on={use(SegmentedControlCheckedContext)}\n      onIcon={checkedIcon}\n      slot='segmented-control-icon'\n    >\n      {children}\n    </SwapIcon>\n  )\n}\n\nexport { SegmentedControl, SegmentedControlIcon, SegmentedControlItem }\n",
      "type": "registry:component",
      "target": "@components/kino/segmented-control.tsx"
    }
  ],
  "type": "registry:component"
}