{
  "name": "tree",
  "type": "registry:ui",
  "framework": "react",
  "title": "Tree",
  "description": "A collapsible, keyboard-driven list of nested rows, such as categories and their channels.",
  "files": [
    {
      "path": "ui/tree.tsx",
      "content": "\"use client\"\n\nimport * as React from \"react\"\n\nimport { cn } from \"@/lib/utils\"\n\n/** One row. A node with `children` is a branch that opens and closes. */\nexport interface TreeNode {\n  /** Unique across the whole tree. */\n  id: string\n  label: string\n  /** A short sign before the label, such as `#`. */\n  prefix?: string\n  /** A count shown at the end of the row, such as unread messages. */\n  badge?: number\n  children?: readonly TreeNode[]\n}\n\nexport interface TreeProps extends Omit<\n  React.ComponentProps<\"ul\">,\n  \"children\" | \"defaultValue\"\n> {\n  items: readonly TreeNode[]\n  /** The selected leaf's id. Leave undefined to let the tree own it. */\n  value?: string\n  defaultValue?: string\n  onValueChange?: (value: string) => void\n  /** Ids of the branches that start open. */\n  defaultExpanded?: readonly string[]\n  /** Accessible name of the tree. */\n  label?: string\n}\n\ninterface Row {\n  readonly node: TreeNode\n  readonly level: number\n  readonly parent: string | undefined\n  readonly position: number\n  readonly size: number\n}\n\n/* The rows a reader can see, in order: a closed branch hides what is under it. */\nfunction visibleRows(\n  nodes: readonly TreeNode[],\n  expanded: readonly string[],\n  level = 1,\n  parent?: string\n): Row[] {\n  return nodes.flatMap((node, index) => [\n    { node, level, parent, position: index + 1, size: nodes.length },\n    ...(node.children !== undefined && expanded.includes(node.id)\n      ? visibleRows(node.children, expanded, level + 1, node.id)\n      : []),\n  ])\n}\n\n/*\n * A nested list drawn flat: every row is a sibling, and `aria-level` says how\n * deep it sits. That is a tree to a screen reader all the same, and it makes\n * \"the next row down\" the next element, which is what the arrow keys need.\n * The tree is one tab stop; a branch toggles and a leaf is selected.\n */\nconst Tree = React.forwardRef<HTMLUListElement, TreeProps>(\n  (\n    {\n      className,\n      items,\n      value,\n      defaultValue,\n      onValueChange,\n      defaultExpanded = [],\n      label,\n      ...props\n    },\n    ref\n  ) => {\n    const baseId = React.useId()\n    const [internal, setInternal] = React.useState(defaultValue)\n    const [expanded, setExpanded] =\n      React.useState<readonly string[]>(defaultExpanded)\n    const [focused, setFocused] = React.useState<string | undefined>(undefined)\n    const selected = value ?? internal\n\n    const rows = visibleRows(items, expanded)\n    const ids = rows.map((row) => row.node.id)\n    /* The one tab stop: where focus last was, else the selection, else the top. */\n    const tabStop =\n      [focused, selected].find((id) => id !== undefined && ids.includes(id)) ??\n      ids[0]\n\n    const focus = (id: string | undefined) => {\n      if (id !== undefined) document.getElementById(`${baseId}-${id}`)?.focus()\n    }\n\n    const toggle = (id: string, open: boolean) => {\n      setExpanded(\n        open ? [...expanded, id] : expanded.filter((each) => each !== id)\n      )\n    }\n\n    const activate = (node: TreeNode) => {\n      if (node.children !== undefined) {\n        toggle(node.id, !expanded.includes(node.id))\n        return\n      }\n      if (value === undefined) setInternal(node.id)\n      onValueChange?.(node.id)\n    }\n\n    const onKeyDown = (event: React.KeyboardEvent<HTMLLIElement>, row: Row) => {\n      const index = ids.indexOf(row.node.id)\n      const forward =\n        getComputedStyle(event.currentTarget).direction === \"rtl\" ? -1 : 1\n      const horizontal =\n        event.key === \"ArrowRight\"\n          ? forward\n          : event.key === \"ArrowLeft\"\n            ? -forward\n            : 0\n      const branch = row.node.children !== undefined\n      const open = expanded.includes(row.node.id)\n\n      if (event.key === \"ArrowDown\") focus(ids[index + 1])\n      else if (event.key === \"ArrowUp\") focus(ids[index - 1])\n      else if (event.key === \"Home\") focus(ids[0])\n      else if (event.key === \"End\") focus(ids.at(-1))\n      else if (event.key === \"Enter\" || event.key === \" \") activate(row.node)\n      else if (horizontal === 1) {\n        if (branch && !open) toggle(row.node.id, true)\n        else if (branch) focus(ids[index + 1])\n      } else if (horizontal === -1) {\n        if (branch && open) toggle(row.node.id, false)\n        else focus(row.parent)\n      } else return\n      event.preventDefault()\n    }\n\n    return (\n      <ul\n        ref={ref}\n        role=\"tree\"\n        aria-label={label}\n        className={cn(\n          \"m-0 flex w-full list-none flex-col gap-0.5 p-0\",\n          className\n        )}\n        {...props}\n      >\n        {rows.map((row) => {\n          const { node } = row\n          const branch = node.children !== undefined\n          const isSelected = !branch && node.id === selected\n          return (\n            <li\n              key={node.id}\n              id={`${baseId}-${node.id}`}\n              role=\"treeitem\"\n              aria-level={row.level}\n              aria-posinset={row.position}\n              aria-setsize={row.size}\n              aria-expanded={branch ? expanded.includes(node.id) : undefined}\n              aria-selected={branch ? undefined : isSelected}\n              tabIndex={node.id === tabStop ? 0 : -1}\n              onClick={() => activate(node)}\n              onFocus={() => setFocused(node.id)}\n              onKeyDown={(event) => onKeyDown(event, row)}\n              style={{ paddingInlineStart: 8 + (row.level - 1) * 16 }}\n              className={cn(\n                \"group flex min-h-8 cursor-pointer select-none items-center gap-1.5 rounded-[var(--newt-radius-sm)] py-1 pe-2 focus-visible:outline focus-visible:outline-2 focus-visible:-outline-offset-2 focus-visible:outline-newt-text-link\",\n                branch\n                  ? \"text-xs font-bold uppercase tracking-[0.02em]\"\n                  : \"text-[15px] font-medium leading-5\",\n                isSelected\n                  ? \"bg-newt-bg-active text-newt-text-primary\"\n                  : \"text-newt-text-muted hover:bg-newt-bg-hover hover:text-newt-text-secondary\"\n              )}\n            >\n              {branch ? (\n                <svg\n                  viewBox=\"0 0 16 16\"\n                  aria-hidden=\"true\"\n                  className=\"h-3 w-3 shrink-0 group-aria-expanded:rotate-90 rtl:-scale-x-100 rtl:group-aria-expanded:scale-x-100\"\n                >\n                  <path\n                    fill=\"currentColor\"\n                    d=\"M5.3 3.3a1 1 0 0 1 1.4 0l4 4a1 1 0 0 1 0 1.4l-4 4a1 1 0 0 1-1.4-1.4L8.6 8 5.3 4.7a1 1 0 0 1 0-1.4Z\"\n                  />\n                </svg>\n              ) : (\n                <span aria-hidden=\"true\" className=\"w-5 shrink-0 text-center\">\n                  {node.prefix}\n                </span>\n              )}\n              <span className=\"min-w-0 flex-1 truncate\">{node.label}</span>\n              {node.badge === undefined ? null : (\n                <span className=\"min-w-4 rounded-full bg-newt-danger px-[5px] text-center text-[11px] font-bold leading-4 text-newt-on-accent\">\n                  {node.badge}\n                </span>\n              )}\n            </li>\n          )\n        })}\n      </ul>\n    )\n  }\n)\nTree.displayName = \"Tree\"\n\nexport { Tree }\n",
      "type": "registry:ui"
    }
  ]
}