{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "tree",
  "title": "Tree",
  "description": "A hierarchy, such as files or pages, by the WAI-ARIA tree pattern: arrows, Home, End and typing a name move focus, and one item or many can be picked. Its rows are the Collapsible’s, a branch opening on its height under a rail, and one highlight glides from row to row as in the Table.",
  "dependencies": [
    "@ui-sdk/react",
    "@ui-sdk/core"
  ],
  "registryDependencies": [
    "https://kinoui.com/r/collapsible.json",
    "https://kinoui.com/r/highlight.json",
    "https://kinoui.com/r/list-rows.json",
    "https://kinoui.com/r/refs.json",
    "https://kinoui.com/r/resize.json",
    "https://kinoui.com/r/theme.json"
  ],
  "files": [
    {
      "path": "registry/components/kino/tree.tsx",
      "content": "'use client'\n\nimport {\n  CollapsibleIndicator,\n  collapsibleNestedClassName,\n  collapsibleRowClassName,\n} from '@/registry/components/kino/collapsible'\nimport {\n  followPointer,\n  type HighlightTarget,\n  itemMatching,\n  pickedRowClassName,\n  rowHighlightClassName,\n  tintedRowTextClassName,\n} from '@/registry/lib/highlight'\nimport {\n  menuIndicatorClassName,\n  menuIndicatorSlot,\n  MenuTick,\n} from '@/registry/lib/list-rows'\nimport { mergeRefs } from '@/registry/lib/refs'\nimport { collapseClassName, followPanelContent } from '@/registry/lib/resize'\nimport {\n  TreeGroup as TreeGroupPrimitive,\n  TreeItemIndicator as TreeItemIndicatorPrimitive,\n  TreeItemLabel as TreeItemLabelPrimitive,\n  TreeItem as TreeItemPrimitive,\n  type TreeMode,\n  Tree as TreePrimitive,\n  type TreeProps,\n} from '@ui-sdk/react/tree'\nimport {\n  Children,\n  type ComponentProps,\n  createContext,\n  isValidElement,\n  type ReactNode,\n  use,\n  useEffect,\n  useMemo,\n  useRef,\n} from 'react'\n\nimport { cn } from '@/lib/utils'\n\n/*\n * A hierarchy, such as files or pages: ui-sdk's Tree, which owns its keys,\n * focus, selection and type-ahead. Its rows are the Collapsible's: a branch's\n * chevron turns down and its items open on the Collapsible's height, under a\n * rail from the chevron. One highlight glides from row to row under the\n * pointer and with focus, as the Table's does, and picked rows take the\n * Table's tint.\n */\n\nconst rowSelector = '[data-slot=tree-label]'\n\n/**\n * The row the highlight goes to for what the pointer or the focus is on:\n * focus sits on the item, whose row is its own label; a nested item's gap\n * between rows is a gap.\n */\nfunction rowOf(area: HTMLElement) {\n  const row = itemMatching(area, rowSelector)\n  return (node: EventTarget | null): HighlightTarget => {\n    const element = node instanceof Element ? node : null\n    const label = element?.matches('[role=treeitem]')\n      ? element.querySelector(`:scope > ${rowSelector}`)\n      : element\n    return row(label ?? null)\n  }\n}\n\n/** Whether a press on a branch's row opens it, so its chevron needs no press of its own. */\nconst RowOpensContext = createContext(true)\n\nfunction Tree<Mode extends TreeMode = 'single'>({\n  className,\n  children,\n  expandOnPress = true,\n  ref,\n  ...props\n}: Omit<TreeProps<Mode>, 'className' | 'children'> & {\n  className?: string\n  children?: ReactNode\n}) {\n  const treeRef = useRef<HTMLDivElement>(null)\n  const layerRef = useRef<HTMLSpanElement>(null)\n  const rootRef = useMemo(() => mergeRefs(treeRef, ref), [ref])\n\n  useEffect(() => {\n    const tree = treeRef.current\n    const layer = layerRef.current\n    if (!tree || !layer) return\n    // One highlight for every row, so it glides from row to row.\n    return followPointer(tree, layer, { itemOf: rowOf(tree), focus: true })\n  }, [])\n\n  return (\n    <TreePrimitive<Mode>\n      ref={rootRef}\n      className={cn(\n        '@container/tree relative isolate flex min-w-0 flex-col gap-px',\n        className,\n      )}\n      expandOnPress={expandOnPress}\n      {...props}\n    >\n      <RowOpensContext value={expandOnPress}>{children}</RowOpensContext>\n      <span\n        ref={layerRef}\n        aria-hidden\n        data-tree-highlight=''\n        className={rowHighlightClassName}\n      />\n    </TreePrimitive>\n  )\n}\n\n/**\n * One item. With a `TreeGroup` inside, even an empty one, it is a branch that\n * opens to show the items in it; without, a leaf. Its row is its\n * `TreeItemLabel`, which names it.\n */\nfunction TreeItem({\n  className,\n  children,\n  branch,\n  ...props\n}: Omit<ComponentProps<typeof TreeItemPrimitive>, 'className' | 'children'> & {\n  className?: string\n  children?: ReactNode\n}) {\n  return (\n    <TreeItemPrimitive\n      // ui-sdk finds a branch by its own group among the children, so it is told of ours.\n      branch={branch ?? (holdsGroup(children) || undefined)}\n      // Focus sits on the item, and its own row draws the ring: each item sets\n      // the ring's width afresh, so a focused branch rings only its own row.\n      className={cn(\n        'min-w-0 [--tree-item-outline:0px] [--tree-item-ring:0px] focus-visible:outline-0 focus-visible:[--tree-item-outline:2px] focus-visible:[--tree-item-ring:3px]',\n        className,\n      )}\n      {...props}\n    >\n      {children}\n    </TreeItemPrimitive>\n  )\n}\n\n/** Whether a `TreeGroup` is among `children`, at any depth, as ui-sdk looks for its own. */\nfunction holdsGroup(children: ReactNode): boolean {\n  return Children.toArray(children).some(\n    (child) =>\n      isValidElement<{ children?: ReactNode }>(child) &&\n      (child.type === TreeGroup || holdsGroup(child.props.children)),\n  )\n}\n\n/**\n * The item's row: the chevron of a branch, or its room in a leaf, so labels\n * at one level line up, then what you give it (an icon, the label, anything\n * at its end, as the Waterfall's bar), then the tick of a picked row.\n */\nfunction TreeItemLabel({\n  className,\n  children,\n  ...props\n}: Omit<ComponentProps<typeof TreeItemLabelPrimitive>, 'className' | 'children'> & {\n  className?: string\n  children?: ReactNode\n}) {\n  const rowOpens = use(RowOpensContext)\n  return (\n    <TreeItemLabelPrimitive\n      className={cn(\n        collapsibleRowClassName,\n        'group/collapsible-trigger text-foreground cursor-default select-none',\n        'transition-[color,background-color,box-shadow] duration-(--motion-press-duration) ease-(--motion-press-fade-easing)',\n        'data-disabled:text-foreground/40 [&>svg]:text-muted-foreground',\n        // The ring of the item around it. Inset, since an open branch clips what pokes past its first and last rows.\n        'ring-ring/50 ring-(length:--tree-item-ring,0px) ring-inset',\n        // Forced colours drop the ring: the row takes the system's outline instead.\n        'forced-colors:outline-(length:--tree-item-outline,0px) forced-colors:-outline-offset-2 forced-colors:outline-[CanvasText] forced-colors:outline-solid',\n        // A picked row takes the Table's tint, and the system's own colours under forced colours.\n        pickedRowClassName,\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      {(state) => (\n        <>\n          {state.branch && rowOpens ? (\n            <CollapsibleIndicator open={state.expanded} />\n          ) : state.branch ? (\n            // A press on the row only picks it, so the chevron opens and closes the\n            // branch: ui-sdk's indicator, its target grown to 24px.\n            <TreeItemIndicatorPrimitive className='relative flex shrink-0 py-0.5 before:absolute before:-inset-x-1 before:-inset-y-0.5'>\n              <CollapsibleIndicator open={state.expanded} />\n            </TreeItemIndicatorPrimitive>\n          ) : (\n            <span aria-hidden className='size-4 shrink-0' />\n          )}\n          {children}\n          {/* Picked shows by more than its tint: the Select's tick, in a slot kept\n              for it, so the label never narrows. It stays mounted, and takes the\n              Select's leaving look while unpicked. */}\n          <span aria-hidden className={cn(menuIndicatorSlot, 'ms-auto')}>\n            <span\n              data-ending-style={state.selected ? undefined : ''}\n              className={menuIndicatorClassName}\n            >\n              <MenuTick />\n            </span>\n          </span>\n        </>\n      )}\n    </TreeItemLabelPrimitive>\n  )\n}\n\n/**\n * The items in a branch, which open and close on the Collapsible's height,\n * under a rail from the branch's chevron. Their labels line up with the\n * branch's.\n */\nfunction TreeGroup({\n  className,\n  children,\n  ...props\n}: Omit<ComponentProps<typeof TreeGroupPrimitive>, 'className' | 'children'> & {\n  className?: string\n  children?: ReactNode\n}) {\n  const ref = useRef<HTMLDivElement>(null)\n\n  // Items that come or go while it is open move it on the same transition.\n  useEffect(() => {\n    const content = ref.current\n    const group = content?.parentElement\n    if (!content || !group) return\n    return followPanelContent(group, content, '--tree-group-height')\n  }, [])\n\n  return (\n    <TreeGroupPrimitive\n      // Clipped down the page only, so a focus ring inside keeps its sides.\n      className={cn(\n        collapseClassName,\n        'overflow-y-clip [--collapse-height:var(--tree-group-height)]',\n      )}\n      {...props}\n    >\n      <div\n        ref={ref}\n        role='none'\n        className={cn(\n          collapsibleNestedClassName,\n          '@max-xs/tree:ps-4 before:start-[calc(var(--control-padding)+--spacing(2))]',\n          className,\n        )}\n      >\n        {children}\n      </div>\n    </TreeGroupPrimitive>\n  )\n}\n\nexport { Tree, TreeGroup, TreeItem, TreeItemLabel }\n",
      "type": "registry:component",
      "target": "@components/kino/tree.tsx"
    }
  ],
  "type": "registry:component"
}