{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "tabs",
  "title": "Tabs",
  "description": "Tabs whose highlight travels to the picked tab: a pill that changes each label’s colour as its edge crosses, or an underline. A backdrop follows the pointer, and a long list scrolls.",
  "dependencies": [
    "@ui-sdk/react",
    "@ui-sdk/core",
    "class-variance-authority"
  ],
  "registryDependencies": [
    "https://kinoui.com/r/highlight.json",
    "https://kinoui.com/r/pill.json",
    "https://kinoui.com/r/refs.json",
    "https://kinoui.com/r/resize.json",
    "https://kinoui.com/r/scroll.json",
    "https://kinoui.com/r/swap.json",
    "https://kinoui.com/r/theme.json"
  ],
  "files": [
    {
      "path": "registry/components/kino/tabs.tsx",
      "content": "'use client'\n\nimport {\n  followPointer,\n  highlightClassName,\n  itemMatching,\n  rowTintClassName,\n} 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 { inertCopy, markResizing, transitionsQuiet } from '@/registry/lib/resize'\nimport { revealInRow } from '@/registry/lib/scroll'\nimport { SwapIcon } from '@/registry/lib/swap'\nimport {\n  TabsIndicator,\n  TabsList as TabsListPrimitive,\n  TabsPanel as TabsPanelPrimitive,\n  Tabs as TabsPrimitive,\n  TabsTab as TabsTabPrimitive,\n  TabsTabIcon as TabsTabIconPrimitive,\n} from '@ui-sdk/react/tabs'\nimport { cva, type VariantProps } from 'class-variance-authority'\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/** The picked tab's value, from ui-sdk's state, so the pill's copy of the list knows it. */\nconst TabsValueContext = createContext<string | null>(null)\n\n/** Whether the tab around it is picked, for its icon. */\nconst TabsSelectedContext = createContext(false)\n\nfunction Tabs<Value extends string>({\n  className,\n  children,\n  ...props\n}: Omit<ComponentProps<typeof TabsPrimitive<Value>>, 'className' | 'children'> & {\n  className?: string\n  children?: ReactNode\n}) {\n  return (\n    <TabsPrimitive<Value>\n      className={cn(\n        'group/tabs flex min-w-0 gap-3 data-[orientation=horizontal]:flex-col',\n        className,\n      )}\n      {...props}\n    >\n      {(state) => <TabsValueContext value={state.value}>{children}</TabsValueContext>}\n    </TabsPrimitive>\n  )\n}\n\nconst tabsListVariants = cva('group/tabs-track max-w-full min-w-0 shrink-0', {\n  variants: {\n    variant: {\n      default: cn(pillTrackClassName, 'self-start'),\n      line: 'self-stretch',\n      rows: '',\n    },\n  },\n  defaultVariants: {\n    variant: 'default',\n  },\n})\n\n/**\n * Scrolls a horizontal list that outgrows its column, fades its edges, and\n * glides the picked or focused tab into view. ui-sdk's own scroll moves only\n * a list that scrolls itself, at once, and never for focus.\n */\nfunction TabsScroller({ className, ...props }: ComponentProps<'div'>) {\n  const ref = useRef<HTMLDivElement>(null)\n\n  useEffect(() => {\n    const scroller = ref.current\n    if (!scroller) return\n\n    return revealInRow(scroller, {\n      item: '[role=\"tab\"]',\n      picked: 'data-selected',\n      fade: '--tabs-fade',\n    })\n  }, [])\n\n  return (\n    <div\n      ref={ref}\n      data-slot='tabs-scroller'\n      className={cn(\n        'min-w-0 scrollbar-none group-data-[orientation=horizontal]/tabs:overflow-x-auto group-data-[orientation=horizontal]/tabs:[mask-image:linear-gradient(to_right,transparent,black_var(--tabs-fade-left,0px),black_calc(100%-var(--tabs-fade-right,0px)),transparent)]',\n        className,\n      )}\n      {...props}\n    />\n  )\n}\n\n// The list and the pill's copy of it share this, so every label sits exactly on its twin.\nconst tabsListLayout = cn(\n  'flex w-max min-w-full',\n  pillRowClassName,\n  'group-data-[variant=line]/tabs-track:p-0 group-data-[orientation=vertical]/tabs:w-full group-data-[orientation=vertical]/tabs:flex-col group-data-[variant=line]/tabs-track:gap-1 group-data-[variant=line]/tabs-track:pb-1.5 group-data-[orientation=vertical]/tabs:group-data-[variant=line]/tabs-track:pe-1.5 group-data-[orientation=vertical]/tabs:group-data-[variant=line]/tabs-track:pb-0',\n)\n\n// The highlight is as large as the list and only its window moves, so nothing changes size.\n// ui-sdk measures the picked tab once the page runs: until then the window is\n// empty and the picked tab draws itself, and its first placement doesn't move.\n// Its end and bottom come from the tab's own size: ui-sdk's `--active-tab-right`\n// and `-bottom` count from the list's scroll size, which rounds and takes in overflow.\n// Forced colours drop backgrounds, so there the highlight takes the system's own.\nconst tabsIndicatorBase =\n  'pointer-events-none absolute z-10 transition-[clip-path] duration-(--motion-morph-duration) ease-(--motion-morph-easing) data-[activation-direction=none]:transition-none group-data-resizing/tabs-list:transition-none motion-reduce:transition-none forced-colors:bg-[Highlight] forced-colors:forced-color-adjust-none'\n\n/** True inside the pill's copy of the list, where a tab is only its label. */\nconst TabsCopyContext = createContext(false)\n\n/** True inside a list of rows, where each tab draws its own content. */\nconst TabsRowsContext = createContext(false)\n\nfunction TabsList({\n  ref,\n  className,\n  variant = 'default',\n  children,\n  ...props\n}: Omit<\n  ComponentProps<typeof TabsListPrimitive> & VariantProps<typeof tabsListVariants>,\n  'className' | 'children'\n> & {\n  className?: string\n  children?: ReactNode\n}) {\n  const backdropRef = useRef<HTMLSpanElement>(null)\n  const listRefs = useMemo(() => mergeRefs(markMounted, ref), [ref])\n\n  // Behind the tab under the pointer, or the one with visible focus.\n  useEffect(() => {\n    const backdrop = backdropRef.current\n    const list = backdrop?.parentElement\n    if (!backdrop || !list) return\n    const stopFollowing = followPointer(list, backdrop, {\n      itemOf: itemMatching(list, '[role=\"tab\"]'),\n      focus: true,\n    })\n    // A list that stretches moves its tabs when it resizes: the indicator goes with them at once.\n    // Rows grow as they're picked, so their list aims its tint itself.\n    const stopMarking = variant === 'rows' ? aimAtPickedRow(list) : markResizing(list)\n    return () => {\n      stopFollowing()\n      stopMarking?.()\n    }\n  }, [variant])\n\n  if (variant === 'rows') {\n    return (\n      <TabsRowsContext value={true}>\n        <TabsListPrimitive\n          ref={listRefs}\n          data-variant={variant}\n          className={cn(\n            'group/tabs-list relative isolate flex flex-col gap-1',\n            className,\n          )}\n          {...props}\n        >\n          {children}\n          <span\n            ref={backdropRef}\n            data-slot='tabs-backdrop'\n            aria-hidden\n            className={cn(highlightClassName, rowTintClassName, '-z-20')}\n          />\n          {/*\n           * The picked row's tint: as large as the list, and only its window moves. It aims\n           * at where the picked row ends up and moves on `layout`, as the rows grow, so the\n           * two land together. A row's height can't overshoot, so nor does the tint: it eases\n           * on the role's fade curve. ui-sdk places it until the list has aimed it.\n           */}\n          <TabsIndicator className='bg-muted dark:bg-muted/60 pointer-events-none absolute inset-0 -z-10 transition-[clip-path] duration-(--motion-layout-duration) ease-(--motion-layout-fade-easing) [clip-path:inset(var(--tabs-row-top,var(--active-tab-top,0px))_0_var(--tabs-row-bottom,calc(100%_-_var(--active-tab-top,0px)_-_var(--active-tab-height,0px)))_0_round_var(--surface-radius))] data-[activation-direction=none]:transition-none group-data-resizing/tabs-list:transition-none motion-reduce:transition-none forced-colors:hidden' />\n        </TabsListPrimitive>\n      </TabsRowsContext>\n    )\n  }\n\n  return (\n    <div\n      data-slot='tabs-track'\n      data-variant={variant}\n      className={cn(tabsListVariants({ variant }), className)}\n    >\n      {/* Room for the underline variant's focus ring, which the scroller would cut off. */}\n      <TabsScroller className='group-data-[variant=line]/tabs-track:-m-1 group-data-[variant=line]/tabs-track:p-1'>\n        <TabsListPrimitive\n          ref={listRefs}\n          className={cn(\n            'group/tabs-list relative isolate',\n            tabsListLayout,\n            variant === 'line' &&\n              'after:bg-border forced-colors:after:bg-[CanvasText] after:absolute after:inset-x-0 after:bottom-0 after:h-px data-[orientation=vertical]:after:inset-x-auto data-[orientation=vertical]:after:inset-y-0 data-[orientation=vertical]:after:end-0 data-[orientation=vertical]:after:h-auto data-[orientation=vertical]:after:w-px',\n          )}\n          {...props}\n        >\n          {children}\n          <span\n            ref={backdropRef}\n            data-slot='tabs-backdrop'\n            aria-hidden\n            className={cn(highlightClassName, pillBackdropClassName)}\n          />\n          {variant === 'line' ? (\n            <TabsIndicator\n              className={cn(\n                tabsIndicatorBase,\n                'bg-foreground inset-x-0 bottom-0 h-0.5 [clip-path:inset(0_calc(100%_-_var(--active-tab-left,0px)_-_var(--active-tab-width,0px))_0_var(--active-tab-left,0px))] data-[orientation=vertical]:inset-x-auto data-[orientation=vertical]:inset-y-0 data-[orientation=vertical]:end-0 data-[orientation=vertical]:h-auto data-[orientation=vertical]:w-0.5 data-[orientation=vertical]:[clip-path:inset(var(--active-tab-top,0px)_0_calc(100%_-_var(--active-tab-top,0px)_-_var(--active-tab-height,0px))_0)]',\n              )}\n            />\n          ) : (\n            <TabsIndicator\n              aria-hidden\n              // Whatever the list holds is copied here, so nothing in the copy may take focus.\n              inert\n              className={cn(\n                tabsIndicatorBase,\n                pillClassName,\n                'inset-0 [clip-path:inset(var(--active-tab-top,0px)_calc(100%_-_var(--active-tab-left,0px)_-_var(--active-tab-width,0px))_calc(100%_-_var(--active-tab-top,0px)_-_var(--active-tab-height,100%))_var(--active-tab-left,0px)_round_max(0px,calc(var(--list-radius)-2px)))]',\n              )}\n            >\n              <span className={cn(tabsListLayout, 'h-full')}>\n                <TabsCopyContext value={true}>{children}</TabsCopyContext>\n              </span>\n            </TabsIndicator>\n          )}\n        </TabsListPrimitive>\n      </TabsScroller>\n    </div>\n  )\n}\n\nconst tabsTabLayout = cn(\n  pillItemLayout,\n  'shrink-0 group-data-[orientation=vertical]/tabs:justify-start',\n)\n\n// Until the page runs and ui-sdk places the indicator, the picked tab draws it itself.\nconst tabsTabUnplaced =\n  'relative [[data-slot=tabs-list]:not([data-mounted])>&]:data-[selected]:bg-primary [[data-slot=tabs-list]:not([data-mounted])>&]:data-[selected]:text-primary-foreground group-data-[variant=line]/tabs-track:[[data-slot=tabs-list]:not([data-mounted])>&]:data-[selected]:bg-transparent group-data-[variant=line]/tabs-track:[[data-slot=tabs-list]:not([data-mounted])>&]:data-[selected]:text-foreground group-data-[variant=line]/tabs-track:[[data-slot=tabs-list]:not([data-mounted])>&]:data-[selected]:after:bg-foreground after:absolute after:inset-x-0 after:-bottom-1.5 after:h-0.5 group-data-[orientation=vertical]/tabs:after:inset-x-auto group-data-[orientation=vertical]/tabs:after:inset-y-0 group-data-[orientation=vertical]/tabs:after:-end-1.5 group-data-[orientation=vertical]/tabs:after:h-auto group-data-[orientation=vertical]/tabs:after:w-0.5'\n\nfunction TabsTab({\n  className,\n  style,\n  children,\n  ...props\n}: Omit<ComponentProps<typeof TabsTabPrimitive>, 'className' | 'children'> & {\n  className?: string\n  children?: ReactNode\n}) {\n  const copy = use(TabsCopyContext)\n  const rows = use(TabsRowsContext)\n  const picked = use(TabsValueContext) === props.value\n  const content = (state: { selected: boolean }) => (\n    <TabsSelectedContext value={state.selected}>{children}</TabsSelectedContext>\n  )\n  if (rows) {\n    return (\n      <TabsTabPrimitive\n        className={cn(\n          '[[data-slot=tabs-list]:not([data-mounted])>&]:data-[selected]:bg-muted [[data-slot=tabs-list]:not([data-mounted])>&]:data-[selected]:rounded-(--surface-radius)',\n          className,\n        )}\n        style={style}\n        {...props}\n      >\n        {content}\n      </TabsTabPrimitive>\n    )\n  }\n  if (copy) {\n    return (\n      <span\n        className={cn(tabsTabLayout, className)}\n        style={typeof style === 'function' ? undefined : style}\n      >\n        {content({ selected: picked })}\n      </span>\n    )\n  }\n  return (\n    <TabsTabPrimitive\n      // A disabled tab fades its label, not itself, so its focus ring keeps its strength.\n      className={cn(\n        tabsTabLayout,\n        pillItemClassName,\n        tabsTabUnplaced,\n        'group-data-[variant=line]/tabs-track:data-[selected]:text-foreground',\n        className,\n      )}\n      style={style}\n      {...props}\n    >\n      {content}\n    </TabsTabPrimitive>\n  )\n}\n\n/**\n * A tab's icon: `children`, and `activeIcon` while the tab is picked. It\n * rolls to the other icon as a Toggle's icon does. A tab whose icon stays\n * the same takes it as a plain child: rolling to itself shows nothing change.\n */\nfunction TabsTabIcon({\n  activeIcon,\n  children,\n  ...props\n}: Omit<ComponentProps<typeof TabsTabIconPrimitive>, 'children' | 'className'> & {\n  activeIcon: ReactNode\n  children: ReactNode\n}) {\n  // A row holds more than its icon's line, so the roll stays in that line.\n  const icon = (\n    <SwapIcon\n      on={use(TabsSelectedContext)}\n      onIcon={activeIcon}\n      clip={use(TabsRowsContext)}\n    >\n      {children}\n    </SwapIcon>\n  )\n  // The pill's copy is no tab, so its icon is only drawn.\n  if (use(TabsCopyContext)) return icon\n  return (\n    // ui-sdk's part hides the icon from screen readers; the roll draws it.\n    <TabsTabIconPrimitive className='contents' {...props}>\n      {icon}\n    </TabsTabIconPrimitive>\n  )\n}\n\nfunction TabsPanel({\n  className,\n  ...props\n}: Omit<ComponentProps<typeof TabsPanelPrimitive>, 'className'> & {\n  className?: string\n}) {\n  return (\n    <TabsPanelPrimitive\n      // The old panel stays mounted while the new one enters; hiding it at once stops the two overlapping.\n      className={cn(\n        'focus-visible:ring-ring/50 min-w-0 rounded-(--control-radius-capped) text-sm focus-visible:ring-3 focus-visible:outline-hidden transition-[opacity,translate] duration-(--motion-enter-duration) ease-(--motion-enter-easing) group-data-[orientation=vertical]/tabs:flex-1 data-ending-style:hidden data-starting-style:opacity-0 motion-safe:data-starting-style:translate-y-(--tabs-panel-rise,6px)',\n        className,\n      )}\n      {...props}\n    />\n  )\n}\n\n/**\n * Aims a list of rows' tint at where the picked row ends up, as `--tabs-row-top`\n * and `--tabs-row-bottom`. Rows grow and shrink as they're picked, so where\n * the picked one is now isn't where it lands: a copy of the list, styled at\n * once with nothing moving, shows where. A change of size while nothing moves\n * places it at once, as `markResizing` does for the other lists. Kept outside\n * the component: the React Compiler drops the forced reads written inside one.\n */\nfunction aimAtPickedRow(list: HTMLElement) {\n  let width = list.getBoundingClientRect().width\n  let frame = 0\n  let picking = 0\n\n  const aim = (at: 'glide' | 'once') => {\n    const insets = picking ? landingInsets(list) : pickedInsets(list)\n    if (!insets) return\n    const top = `${insets.top}px`\n    const bottom = `${insets.bottom}px`\n    if (\n      list.style.getPropertyValue('--tabs-row-top') === top &&\n      list.style.getPropertyValue('--tabs-row-bottom') === bottom\n    )\n      return\n    if (at === 'once') {\n      list.dataset.resizing = ''\n      cancelAnimationFrame(frame)\n      frame = requestAnimationFrame(() => {\n        frame = requestAnimationFrame(() => delete list.dataset.resizing)\n      })\n    }\n    list.style.setProperty('--tabs-row-top', top)\n    list.style.setProperty('--tabs-row-bottom', bottom)\n  }\n\n  // A pick, or rows that come and go or change places.\n  const picks = new MutationObserver(() => {\n    const run = ++picking\n    aim('glide')\n    // While the rows grow, the list's height changes every frame: nothing to aim again until they settle.\n    void transitionsQuiet(list, { subtree: true }).then(() => {\n      if (run !== picking) return\n      picking = 0\n      // Something else may have changed size meanwhile, such as a font arriving.\n      aim('once')\n    })\n  })\n  picks.observe(list, {\n    childList: true,\n    subtree: true,\n    attributeFilter: ['data-selected'],\n  })\n\n  const sizes = new ResizeObserver(() => {\n    const next = list.getBoundingClientRect().width\n    const widthChanged = Math.abs(next - width) >= 0.5\n    width = next\n    if (widthChanged || picking === 0) aim('once')\n  })\n  sizes.observe(list)\n\n  return () => {\n    picks.disconnect()\n    sizes.disconnect()\n    cancelAnimationFrame(frame)\n  }\n}\n\n/** The picked row's place now, while nothing moves. */\nfunction pickedInsets(list: HTMLElement) {\n  const row = list.querySelector('[role=\"tab\"][data-selected]')\n  return row ? rowInsets(list.getBoundingClientRect(), row.getBoundingClientRect()) : null\n}\n\n/** The picked row's place once the rows have settled, measured on a copy of the list styled with nothing moving. */\nfunction landingInsets(list: HTMLElement) {\n  const copy = inertCopy(list, {\n    insetBlockStart: '0',\n    insetInlineStart: '0',\n    width: `${list.getBoundingClientRect().width}px`,\n    visibility: 'hidden',\n  })\n  list.after(copy)\n  const insets = pickedInsets(copy)\n  copy.remove()\n  return insets\n}\n\n/** How far a row sits from its list's top and bottom edges, in px. */\nfunction rowInsets(\n  list: { top: number; bottom: number },\n  row: { top: number; bottom: number },\n) {\n  return { top: row.top - list.top, bottom: list.bottom - row.bottom }\n}\n\nexport { Tabs, TabsList, TabsPanel, TabsTab, TabsTabIcon, rowInsets }\n",
      "type": "registry:component",
      "target": "@components/kino/tabs.tsx"
    }
  ],
  "type": "registry:component"
}