{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "chart-tree-path",
  "title": "Chart tree path",
  "description": "What every chart of a tree that zooms shares: its root, whose groups are the tree’s tops, with what is hidden and the part in view, and the way back out, a Breadcrumb of the parts above the one in view, each a button that zooms out to it, with focus kept on the plot as a crumb folds away. The Treemap, Circle packing and Sunburst charts sit on it.",
  "registryDependencies": [
    "https://kinoui.com/r/announce.json",
    "https://kinoui.com/r/breadcrumb.json",
    "https://kinoui.com/r/chart-math.json",
    "https://kinoui.com/r/chart-root.json",
    "https://kinoui.com/r/chart-tiles.json",
    "https://kinoui.com/r/controllable.json"
  ],
  "files": [
    {
      "path": "registry/lib/chart-tree-path.tsx",
      "content": "'use client'\n\nimport {\n  Breadcrumb,\n  BreadcrumbItem,\n  BreadcrumbLink,\n  BreadcrumbList,\n  BreadcrumbPage,\n  BreadcrumbSeparator,\n} from '@/registry/components/kino/breadcrumb'\nimport { announce } from '@/registry/lib/announce'\nimport type { ChartConfig } from '@/registry/lib/chart-math'\nimport {\n  type ChartRootProps,\n  type ChartRootState,\n  useChartRoot,\n  useChartRootValue,\n} from '@/registry/lib/chart-root'\nimport { tileAncestors, type TileNode, tileNodeAt } from '@/registry/lib/chart-tiles'\nimport { useControllable } from '@/registry/lib/controllable'\nimport { Button as ButtonPrimitive } from '@ui-sdk/react/button'\nimport { type ComponentProps, createContext, Fragment, use } from 'react'\n\nimport { cn } from '@/lib/utils'\n\n/*\n * What every chart of a tree that zooms shares, such as the Treemap, the\n * Circle packing and the Sunburst: its root, whose groups are the tree's\n * tops, with what is hidden and the part in view, and the way back out,\n * the path of the parts above the one in view, each a button that zooms\n * out to it.\n */\n\n/** The props of a chart of a tree that zooms that its root reads: its groups, what is hidden and the part in view. */\ninterface TreeZoomRootProps extends ChartRootProps {\n  categoryFormat?: (value: unknown) => string\n  zoom?: string[]\n  defaultZoom?: string[]\n  onZoomChange?: (zoom: string[]) => void\n}\n\n/**\n * The root of a chart of a tree that zooms: every top is a group the legend\n * and the keys know, the ones the config leaves out after its own, named by\n * their names; a part whose top is hidden can't be in view, so the whole\n * is; and hiding the top in view zooms back out to the whole. Each zoom is\n * announced.\n */\nfunction useTreeZoomRoot(\n  props: TreeZoomRootProps,\n  tree: TileNode,\n  options: { entrance: boolean },\n): {\n  state: ChartRootState\n  groups: ChartConfig\n  hidden: readonly string[]\n  view: TileNode\n  nameOf: (node: TileNode) => string\n  /** A part's value as written, or the chart's word for none. */\n  valueOf: (node: TileNode) => string\n  zoomTo: (path: readonly string[]) => void\n} {\n  const { config, messages, categoryFormat } = props\n  const groups: ChartConfig = { ...config }\n  for (const top of tree.children) {\n    const key = top.path[0] ?? ''\n    groups[key] ??= {\n      label: categoryFormat ? categoryFormat(top.name) : String(top.name),\n    }\n  }\n  const [hidden, setHidden] = useControllable(\n    props.hiddenSeries,\n    props.defaultHiddenSeries ?? [],\n    props.onHiddenSeriesChange,\n  )\n  const [zoom, setZoom] = useControllable(\n    props.zoom,\n    props.defaultZoom ?? [],\n    props.onZoomChange,\n  )\n  const asked = tileNodeAt(tree, zoom)\n  const view = hidden.includes(asked.path[0] ?? '') ? tree : asked\n  const state = useChartRoot(\n    {\n      ...props,\n      config: groups,\n      hiddenSeries: hidden,\n      defaultHiddenSeries: undefined,\n      onHiddenSeriesChange: (next) => {\n        // The whole has no top, so hiding a group from it zooms nowhere.\n        if (view.path.length > 0 && next.includes(view.path[0] ?? '')) {\n          setZoom([])\n          announce(state.messages.zoomedTo(nameOf(tree)))\n        }\n        setHidden(next)\n      },\n      messages: {\n        series: 'Groups',\n        empty: 'No data yet',\n        allHidden: 'Every group is hidden',\n        showAll: (count) => `Show all ${count} groups`,\n        ...messages,\n      },\n    },\n    { entrance: options.entrance, heightFromData: false },\n  )\n  const nameOf = (node: TileNode): string => {\n    if (node.path.length === 0) return state.messages.whole\n    if (categoryFormat) return categoryFormat(node.name)\n    const named = node.path.length === 1 ? groups[node.path[0] ?? '']?.label : undefined\n    return typeof named === 'string' ? named : String(node.name)\n  }\n  const valueOf = (node: TileNode) =>\n    node.missing ? state.messages.noValue : state.valueText(node.value)\n  const zoomTo = (path: readonly string[]) => {\n    const next = tileNodeAt(tree, path)\n    if (next.key === view.key) return\n    setZoom([...next.path])\n    announce(state.messages.zoomedTo(nameOf(next)))\n  }\n  return { state, groups, hidden, view, nameOf, valueOf, zoomTo }\n}\n\ninterface TreeZoomValue {\n  tree: TileNode\n  view: TileNode\n  zoomTo: (path: readonly string[]) => void\n  nameOf: (node: TileNode) => string\n}\n\nconst TreeZoomContext = createContext<TreeZoomValue | null>(null)\n\nfunction useTreeZoom(part: string) {\n  const zoom = use(TreeZoomContext)\n  if (!zoom) throw new Error(`\\`${part}\\` goes inside a chart of a tree that zooms.`)\n  return zoom\n}\n\n/** How long focus is kept off the page after a crumb is pressed, in ms: past a menu that closes and hands focus back to \"…\". */\nconst FOCUS_WATCH = 600\n\n/**\n * Puts focus on `target` whenever it falls to the page in the next moments,\n * as it does when the crumb or the menu that had it goes.\n */\nfunction keepFocusOn(target: { current: HTMLElement | null }) {\n  const until = performance.now() + FOCUS_WATCH\n  const check = () => {\n    const now = document.activeElement\n    if (!now || now === document.body) target.current?.focus({ preventScroll: true })\n    if (performance.now() < until) requestAnimationFrame(check)\n  }\n  requestAnimationFrame(check)\n}\n\n/**\n * Kino's Breadcrumb of the parts above the one in view, the whole first,\n * each a button that zooms out to it, and the part in view last. It sits\n * above the plot, under a legend at the top. It shows the whole's name\n * before the first zoom too, so zooming never moves the plot, but with\n * nothing to press it is hidden from screen readers until then.\n */\nfunction TreeZoomPath({\n  slot,\n  className,\n  ...props\n}: ComponentProps<'nav'> & {\n  /** The chart's own name for it, such as `treemap-chart-path`. */\n  slot: string\n}) {\n  const chart = useChartRootValue()\n  const { tree, view, zoomTo, nameOf } = useTreeZoom(slot)\n  const above = tileAncestors(tree, view)\n  return (\n    <Breadcrumb\n      data-slot={slot}\n      aria-label={chart.messages.zoomPath(chart.label)}\n      aria-hidden={above.length === 0 ? true : undefined}\n      className={cn('pb-2 [grid-area:top-axis]', className)}\n      {...props}\n    >\n      <BreadcrumbList>\n        {above.map((node) => (\n          <Fragment key={node.key || 'whole'}>\n            <BreadcrumbItem>\n              <BreadcrumbLink\n                // oxlint-disable-next-line jsx-a11y/control-has-associated-label -- the crumb's name, its children, labels the button\n                render={<ButtonPrimitive />}\n                onClick={() => {\n                  zoomTo(node.path)\n                  // The crumb pressed becomes the part in view, and \"…\" may fold away with it:\n                  // focus goes to the plot rather than to the page.\n                  keepFocusOn(chart.plot)\n                }}\n              >\n                {nameOf(node)}\n              </BreadcrumbLink>\n            </BreadcrumbItem>\n            <BreadcrumbSeparator />\n          </Fragment>\n        ))}\n        <BreadcrumbItem>\n          <BreadcrumbPage>{nameOf(view)}</BreadcrumbPage>\n        </BreadcrumbItem>\n      </BreadcrumbList>\n    </Breadcrumb>\n  )\n}\n\nexport {\n  keepFocusOn,\n  TreeZoomContext,\n  TreeZoomPath,\n  type TreeZoomRootProps,\n  useTreeZoomRoot,\n  type TreeZoomValue,\n  useTreeZoom,\n}\n",
      "type": "registry:lib",
      "target": "@lib/chart-tree-path.tsx"
    }
  ],
  "type": "registry:lib"
}