{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "tree-table",
  "title": "Tree Table",
  "description": "A table whose rows nest, like files in folders: open and close branches with a chevron, parents add up their children (folder sizes), sort any column with folders kept on top, and tick rows with checkboxes that select whole branches.",
  "dependencies": [
    "motion",
    "clsx",
    "tailwind-merge"
  ],
  "files": [
    {
      "path": "registry/new-york/tree-table/tree-table.tsx",
      "content": "\"use client\";\n\nimport * as React from \"react\";\nimport { motion, AnimatePresence, useReducedMotion } from \"motion/react\";\nimport { clsx, type ClassValue } from \"clsx\";\nimport { twMerge } from \"tailwind-merge\";\n\nfunction cn(...inputs: ClassValue[]) {\n  return twMerge(clsx(inputs));\n}\n\nexport interface TreeRow {\n  id: string;\n  name: string;\n  /** Values for the extra columns, by column key. */\n  values?: Record<string, string | number>;\n  children?: TreeRow[];\n}\n\nexport interface TreeColumn {\n  key: string;\n  label: string;\n  align?: \"left\" | \"right\";\n  /** CSS grid width, e.g. \"96px\" or \"1fr\". */\n  width?: string;\n  /** Parents show the sum of their children for this (numeric) column. */\n  sum?: boolean;\n  format?: (value: string | number | undefined, row: TreeRow) => React.ReactNode;\n}\n\nexport interface TreeTableProps {\n  data?: TreeRow[];\n  columns?: TreeColumn[];\n  /** Label of the first (tree) column. */\n  nameLabel?: string;\n  defaultExpanded?: string[];\n  selectable?: boolean;\n  /** Ids of selected leaf rows. */\n  defaultSelected?: string[];\n  onSelectionChange?: (ids: string[]) => void;\n  theme?: \"dark\" | \"light\";\n  className?: string;\n}\n\nconst PALETTES = {\n  dark: { text: \"#F5F4F1\", muted: \"rgba(245,244,241,0.52)\", faint: \"rgba(245,244,241,0.28)\", panel: \"#0E0E0E\", head: \"#121212\", border: \"rgba(255,255,255,0.09)\", line: \"rgba(255,255,255,0.06)\", hover: \"rgba(255,255,255,0.035)\", sel: \"rgba(255,255,255,0.06)\", box: \"rgba(255,255,255,0.22)\", fill: \"#F5F4F1\", onFill: \"#0A0A0A\", guide: \"rgba(255,255,255,0.08)\", focus: \"rgba(245,244,241,0.85)\" },\n  light: { text: \"#0A0A0A\", muted: \"rgba(10,10,10,0.52)\", faint: \"rgba(10,10,10,0.28)\", panel: \"#FFFFFF\", head: \"#FAFAF9\", border: \"rgba(10,10,10,0.1)\", line: \"rgba(10,10,10,0.06)\", hover: \"rgba(10,10,10,0.025)\", sel: \"rgba(10,10,10,0.045)\", box: \"rgba(10,10,10,0.25)\", fill: \"#0A0A0A\", onFill: \"#FFFFFF\", guide: \"rgba(10,10,10,0.08)\", focus: \"rgba(10,10,10,0.85)\" },\n};\n\nconst kb = (n: number | string | undefined) => (typeof n === \"number\" ? (n >= 1024 ? `${(n / 1024).toFixed(1)} MB` : `${Math.round(n)} KB`) : \"—\");\n\nconst DEFAULT_COLUMNS: TreeColumn[] = [\n  { key: \"size\", label: \"Size\", align: \"right\", width: \"96px\", sum: true, format: (v) => kb(v) },\n  { key: \"type\", label: \"Type\", width: \"110px\" },\n  { key: \"modified\", label: \"Modified\", width: \"110px\" },\n];\n\nconst DEFAULT_DATA: TreeRow[] = [\n  {\n    id: \"src\",\n    name: \"src\",\n    values: { type: \"Folder\", modified: \"Today\" },\n    children: [\n      {\n        id: \"components\",\n        name: \"components\",\n        values: { type: \"Folder\", modified: \"Today\" },\n        children: [\n          { id: \"button\", name: \"button.tsx\", values: { size: 6, type: \"TypeScript\", modified: \"Today\" } },\n          { id: \"card\", name: \"card.tsx\", values: { size: 4, type: \"TypeScript\", modified: \"Yesterday\" } },\n          { id: \"dialog\", name: \"dialog.tsx\", values: { size: 12, type: \"TypeScript\", modified: \"Sep 24\" } },\n        ],\n      },\n      { id: \"lib\", name: \"lib\", values: { type: \"Folder\", modified: \"Sep 22\" }, children: [{ id: \"utils\", name: \"utils.ts\", values: { size: 2, type: \"TypeScript\", modified: \"Sep 22\" } }] },\n      { id: \"app\", name: \"app.tsx\", values: { size: 9, type: \"TypeScript\", modified: \"Today\" } },\n    ],\n  },\n  { id: \"public\", name: \"public\", values: { type: \"Folder\", modified: \"Sep 19\" }, children: [{ id: \"hero\", name: \"hero.webp\", values: { size: 1480, type: \"Image\", modified: \"Sep 19\" } }, { id: \"favicon\", name: \"favicon.svg\", values: { size: 3, type: \"SVG\", modified: \"Sep 02\" } }] },\n  { id: \"package\", name: \"package.json\", values: { size: 2, type: \"JSON\", modified: \"Sep 26\" } },\n  { id: \"readme\", name: \"README.md\", values: { size: 5, type: \"Markdown\", modified: \"Sep 10\" } },\n];\n\nconst leaves = (r: TreeRow): string[] => (r.children?.length ? r.children.flatMap(leaves) : [r.id]);\n\n// A cell's value; summed columns add up a parent's children.\nfunction valueOf(row: TreeRow, col: TreeColumn): string | number | undefined {\n  if (col.sum && row.children?.length) return row.children.reduce((a, c) => a + (Number(valueOf(c, col)) || 0), 0);\n  return row.values?.[col.key];\n}\n\nfunction FolderIcon({ open }: { open: boolean }) {\n  return (\n    <svg width=\"16\" height=\"16\" viewBox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" strokeWidth=\"1.6\" strokeLinecap=\"round\" strokeLinejoin=\"round\" aria-hidden=\"true\">\n      {open ? <path d=\"M3.5 18.5V6a1 1 0 0 1 1-1h5l2 2.5h7a1 1 0 0 1 1 1V10M3.5 18.5l2.6-7.2a1 1 0 0 1 .9-.6h13.4a.8.8 0 0 1 .8 1.1l-2.4 6.2a.8.8 0 0 1-.8.5Z\" /> : <path d=\"M4 19.5h16a1 1 0 0 0 1-1v-10a1 1 0 0 0-1-1h-8L10 5H4a1 1 0 0 0-1 1v12.5a1 1 0 0 0 1 1Z\" />}\n    </svg>\n  );\n}\n\nfunction FileIcon() {\n  return (\n    <svg width=\"16\" height=\"16\" viewBox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" strokeWidth=\"1.6\" strokeLinecap=\"round\" strokeLinejoin=\"round\" aria-hidden=\"true\">\n      <path d=\"M14 3H7a2 2 0 0 0-2 2v14a2 2 0 0 0 2 2h10a2 2 0 0 0 2-2V8Z\" />\n      <path d=\"M14 3v4a1 1 0 0 0 1 1h4\" />\n    </svg>\n  );\n}\n\nexport function TreeTable({ data = DEFAULT_DATA, columns = DEFAULT_COLUMNS, nameLabel = \"Name\", defaultExpanded = [\"src\", \"components\"], selectable = true, defaultSelected = [], onSelectionChange, theme = \"dark\", className }: TreeTableProps) {\n  const p = PALETTES[theme];\n  const reduce = Boolean(useReducedMotion());\n  const [expanded, setExpanded] = React.useState<Set<string>>(new Set(defaultExpanded));\n  const [selected, setSelected] = React.useState<Set<string>>(new Set(defaultSelected));\n  const [sort, setSort] = React.useState<{ key: string; dir: 1 | -1 } | null>(null);\n\n  const allParents = React.useMemo(() => {\n    const out: string[] = [];\n    const walk = (rs: TreeRow[]) => rs.forEach((r) => r.children?.length && (out.push(r.id), walk(r.children)));\n    walk(data);\n    return out;\n  }, [data]);\n\n  const sortRows = (rows: TreeRow[]) => {\n    const folders = (r: TreeRow) => (r.children?.length ? 0 : 1);\n    return [...rows].sort((a, b) => {\n      const f = folders(a) - folders(b);\n      if (f !== 0) return f;\n      if (!sort) return 0;\n      const col = sort.key === \"__name\" ? null : columns.find((c) => c.key === sort.key);\n      const va = col ? valueOf(a, col) : a.name;\n      const vb = col ? valueOf(b, col) : b.name;\n      if (typeof va === \"number\" && typeof vb === \"number\") return (va - vb) * sort.dir;\n      return String(va ?? \"\").localeCompare(String(vb ?? \"\")) * sort.dir;\n    });\n  };\n\n  const setSel = (next: Set<string>) => {\n    setSelected(next);\n    onSelectionChange?.([...next]);\n  };\n  const selState = (r: TreeRow): \"on\" | \"off\" | \"mixed\" => {\n    const ls = leaves(r);\n    const n = ls.filter((id) => selected.has(id)).length;\n    return n === 0 ? \"off\" : n === ls.length ? \"on\" : \"mixed\";\n  };\n  const toggleSel = (r: TreeRow) => {\n    const ls = leaves(r);\n    const on = selState(r) === \"on\";\n    const next = new Set(selected);\n    ls.forEach((id) => (on ? next.delete(id) : next.add(id)));\n    setSel(next);\n  };\n  const toggleOpen = (id: string) =>\n    setExpanded((e) => {\n      const next = new Set(e);\n      if (next.has(id)) next.delete(id);\n      else next.add(id);\n      return next;\n    });\n\n  const grid = `minmax(220px, 1fr) ${columns.map((c) => c.width ?? \"1fr\").join(\" \")}`;\n  const allLeaves = data.flatMap(leaves);\n  const totalSel = allLeaves.filter((id) => selected.has(id)).length;\n\n  const box = (state: \"on\" | \"off\" | \"mixed\") => (\n    <span aria-hidden=\"true\" className=\"flex shrink-0 items-center justify-center\" style={{ width: 15, height: 15, borderRadius: 4, border: `1.5px solid ${state !== \"off\" ? p.fill : p.box}`, background: state !== \"off\" ? p.fill : \"transparent\", transition: \"background 0.15s ease\" }}>\n      {state === \"on\" && (\n        <svg width=\"9\" height=\"9\" viewBox=\"0 0 12 12\" fill=\"none\">\n          <path d=\"M2.4 6.3 4.9 8.7 9.6 3.4\" stroke={p.onFill} strokeWidth=\"2\" strokeLinecap=\"round\" strokeLinejoin=\"round\" />\n        </svg>\n      )}\n      {state === \"mixed\" && <span style={{ width: 7, height: 2, borderRadius: 1, background: p.onFill }} />}\n    </span>\n  );\n\n  const renderRows = (rows: TreeRow[], level: number): React.ReactNode =>\n    sortRows(rows).map((row) => {\n      const kids = row.children ?? [];\n      const isParent = kids.length > 0;\n      const open = isParent && expanded.has(row.id);\n      const state = selectable ? selState(row) : \"off\";\n      return (\n        <React.Fragment key={row.id}>\n          <motion.div\n            role=\"row\"\n            aria-level={level + 1}\n            aria-expanded={isParent ? open : undefined}\n            aria-selected={selectable ? state === \"on\" : undefined}\n            className=\"grid items-center bg-[var(--tt-bg)] hover:bg-[var(--tt-hover)]\"\n            style={{ gridTemplateColumns: grid, minHeight: 42, borderBottom: `1px solid ${p.line}`, [\"--tt-bg\" as string]: state !== \"off\" ? p.sel : \"transparent\", [\"--tt-hover\" as string]: state !== \"off\" ? p.sel : p.hover, transition: \"background-color 0.15s ease\" }}\n            initial={level > 0 && !reduce ? { opacity: 0, y: -6 } : false}\n            animate={{ opacity: 1, y: 0 }}\n            exit={{ opacity: 0, y: -6, transition: { duration: 0.12 } }}\n            transition={{ duration: 0.22, ease: [0.22, 1, 0.36, 1] }}\n          >\n            <div role=\"gridcell\" className=\"flex min-w-0 items-center\" style={{ gap: 8, paddingLeft: 12 + level * 22, paddingRight: 12, position: \"relative\" }}>\n              {/* Indent guides */}\n              {Array.from({ length: level }, (_, i) => (\n                <span key={i} aria-hidden=\"true\" className=\"absolute top-0 bottom-0\" style={{ left: 12 + i * 22 + 9, width: 1, background: p.guide }} />\n              ))}\n              {isParent ? (\n                <button\n                  type=\"button\"\n                  aria-label={`${open ? \"Collapse\" : \"Expand\"} ${row.name}`}\n                  onClick={() => toggleOpen(row.id)}\n                  className=\"flex shrink-0 cursor-pointer items-center justify-center rounded-md border-none bg-transparent outline-none focus-visible:ring-2\"\n                  style={{ width: 18, height: 18, color: p.muted, [\"--tw-ring-color\" as string]: p.focus }}\n                >\n                  <motion.svg width=\"12\" height=\"12\" viewBox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" strokeWidth=\"2.4\" strokeLinecap=\"round\" strokeLinejoin=\"round\" aria-hidden=\"true\" animate={{ rotate: open ? 90 : 0 }} transition={{ duration: reduce ? 0 : 0.18 }}>\n                    <path d=\"m9 6 6 6-6 6\" />\n                  </motion.svg>\n                </button>\n              ) : (\n                <span className=\"shrink-0\" style={{ width: 18 }} />\n              )}\n              {selectable && (\n                <button type=\"button\" role=\"checkbox\" aria-checked={state === \"mixed\" ? \"mixed\" : state === \"on\"} aria-label={`Select ${row.name}`} onClick={() => toggleSel(row)} className=\"flex shrink-0 cursor-pointer items-center rounded border-none bg-transparent p-0 outline-none focus-visible:ring-2\" style={{ [\"--tw-ring-color\" as string]: p.focus }}>\n                  {box(state)}\n                </button>\n              )}\n              <span className=\"shrink-0\" style={{ color: isParent ? p.text : p.muted }}>\n                {isParent ? <FolderIcon open={open} /> : <FileIcon />}\n              </span>\n              <span className={cn(\"truncate text-[13.5px]\", isParent ? \"font-semibold\" : \"font-medium\")} style={{ color: p.text, cursor: isParent ? \"pointer\" : undefined }} onClick={() => isParent && toggleOpen(row.id)}>\n                {row.name}\n              </span>\n            </div>\n            {columns.map((col) => {\n              const v = valueOf(row, col);\n              return (\n                <div key={col.key} role=\"gridcell\" className=\"truncate text-[13px] tabular-nums\" style={{ padding: \"0 12px\", textAlign: col.align ?? \"left\", color: p.muted }}>\n                  {col.format ? col.format(v, row) : (v ?? \"—\")}\n                </div>\n              );\n            })}\n          </motion.div>\n          <AnimatePresence initial={false}>{open && renderRows(kids, level + 1)}</AnimatePresence>\n        </React.Fragment>\n      );\n    });\n\n  const header = (label: string, key: string, align: \"left\" | \"right\" = \"left\", paddingLeft = 12) => {\n    const active = sort?.key === key;\n    return (\n      <div role=\"columnheader\" aria-sort={active ? (sort!.dir === 1 ? \"ascending\" : \"descending\") : \"none\"} style={{ padding: `0 12px 0 ${paddingLeft}px`, textAlign: align }}>\n        <button\n          type=\"button\"\n          onClick={() => setSort((s) => (s?.key === key ? (s.dir === 1 ? { key, dir: -1 } : null) : { key, dir: 1 }))}\n          className=\"inline-flex cursor-pointer items-center rounded border-none bg-transparent p-0 text-[12px] font-semibold tracking-wide uppercase outline-none focus-visible:ring-2\"\n          style={{ gap: 4, color: active ? p.text : p.muted, [\"--tw-ring-color\" as string]: p.focus, flexDirection: align === \"right\" ? \"row-reverse\" : \"row\" }}\n        >\n          {label}\n          <motion.svg width=\"10\" height=\"10\" viewBox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" strokeWidth=\"2.6\" strokeLinecap=\"round\" strokeLinejoin=\"round\" aria-hidden=\"true\" animate={{ rotate: active && sort!.dir === -1 ? 180 : 0, opacity: active ? 1 : 0.35 }} transition={{ duration: reduce ? 0 : 0.18 }}>\n            <path d=\"m6 15 6-6 6 6\" />\n          </motion.svg>\n        </button>\n      </div>\n    );\n  };\n\n  return (\n    <div className={cn(\"flex w-full flex-col overflow-hidden\", className)} style={{ borderRadius: 16, background: p.panel, border: `1px solid ${p.border}`, fontFamily: \"Inter, sans-serif\", color: p.text }}>\n      <div className=\"flex items-center justify-between\" style={{ gap: 12, padding: \"10px 12px\", borderBottom: `1px solid ${p.line}` }}>\n        <span className=\"text-[12.5px] tabular-nums\" style={{ color: p.muted }} aria-live=\"polite\">\n          {selectable && totalSel > 0 ? `${totalSel} of ${allLeaves.length} files selected` : `${allLeaves.length} files`}\n        </span>\n        <div className=\"flex\" style={{ gap: 6 }}>\n          {[\n            { label: \"Expand all\", on: () => setExpanded(new Set(allParents)) },\n            { label: \"Collapse all\", on: () => setExpanded(new Set()) },\n          ].map((b) => (\n            <button key={b.label} type=\"button\" onClick={b.on} className=\"h-7 cursor-pointer rounded-md bg-transparent text-[12px] font-medium outline-none focus-visible:ring-2\" style={{ padding: \"0 9px\", border: `1px solid ${p.border}`, color: p.text, [\"--tw-ring-color\" as string]: p.focus }}>\n              {b.label}\n            </button>\n          ))}\n        </div>\n      </div>\n      <div className=\"overflow-x-auto\">\n        <div role=\"treegrid\" aria-label=\"Files\" style={{ minWidth: 520 }}>\n          <div role=\"row\" className=\"grid items-center\" style={{ gridTemplateColumns: grid, height: 38, background: p.head, borderBottom: `1px solid ${p.line}` }}>\n            {header(nameLabel, \"__name\", \"left\", selectable ? 61 : 38)}\n            {columns.map((c) => (\n              <React.Fragment key={c.key}>{header(c.label, c.key, c.align)}</React.Fragment>\n            ))}\n          </div>\n          <AnimatePresence initial={false}>{renderRows(data, 0)}</AnimatePresence>\n        </div>\n      </div>\n    </div>\n  );\n}\n\nexport default TreeTable;\n",
      "type": "registry:component"
    }
  ],
  "type": "registry:component"
}