Cómo funciona
A card with no outer border, a muted 12px header over the only hairline and no lines between rows. The tree column starts with a 20px slot: rows with children show a chevron that turns 90° when open, leaf rows keep an empty slot so text lines up, and each level indents by indent. Every ancestor level draws a faint 1px guide through the middle of its chevron slot over the full row height, so the guides join into rails. On expand, new children fade in and drop 8px, staggered 40ms; rows already below slide with FLIP (offsetTop plus Web Animations, continuing from mid-flight). Collapse removes children at once. Rows are described by rows plus getChildren, and state can be internal or controlled for lazy loading; render receives { level, hasChildren, expanded } so a cell can show an open or closed folder.
Úsalo para
- Data with real parent-child levels where you also compare numbers: folder → subfolder storage, department → team → member results, channel → sub-channel revenue.
- Parent rows that are sums of their children: see the total first, then open it to see what contributes.
- Two to four levels with a few to a few dozen nodes each, drilled down within one table.
Busca otra opción cuando
- Extra detail under one row that is not the same kind of row: use an expandable-row table.
- Very deep trees (five levels or more) or structure without numbers: use a tree view or drill-down pages; deep indents squeeze the columns.
- Flat data you only want to group: use sorting or filters rather than inventing a parent row.
Uso
import { TreeTable } from './TreeTable.jsx'
const columns = [
{ key: 'name', title: 'Folder' }, // The first column gets the indent, guide lines and chevron
{ key: 'size', title: 'Size', width: 92, align: 'right', render: (r) => `${r.size.toFixed(1)} GB` },
]
const rows = [
{
id: 'projects',
name: 'Projects',
size: 386.2,
children: [
{ id: 'brand', name: 'Brand refresh', size: 241.5 },
{ id: 'website', name: 'Website', size: 144.7 },
],
},
{ id: 'archive', name: 'Archive', size: 97.3 },
]
export function SharedDrive() {
return (
<TreeTable
title="Shared drive"
subtitle="483.5 GB used"
columns={columns}
rows={rows}
rowKey={(r) => r.id}
getChildren={(r) => r.children}
defaultExpanded="all"
/>
)
}Propiedades
| Propiedad | Tipo | Por defecto | Qué hace |
|---|---|---|---|
columns | Column[] | — | Column definitions: key (also the default field to read), title, width, align, render(row, index, info) where info is { level, hasChildren, expanded }. |
rows | object[] | — | The top-level rows. |
rowKey | (row) => string | number | — | Unique key of each row, unique across the whole tree. |
getChildren | (row) => object[] | undefined | null | — | Returns the children of a row; undefined or an empty array for none. |
treeColumn | string | key of the first column | Which column gets the indent and chevron. |
expanded | (string | number)[] | — | Controlled: keys of the expanded rows. If omitted, the component keeps its own. |
defaultExpanded | (string | number)[] | 'all' | [] | Initially expanded rows when uncontrolled; 'all' expands everything. |
onExpandedChange | (keys) => void | — | Called when rows expand or collapse. |
indent | number | 20 | Indent per level in pixels; the guide lines are spaced the same. |
toggleOnRowClick | boolean | true | Clicking anywhere on a row with children toggles it. When false only the chevron works; mark your own clickable elements with data-no-toggle. |
animate | boolean | true | Whether rows move when expanding or collapsing. Skipped automatically above 200 visible rows. |
emptyText | ReactNode | 'No data' | Text shown when there are no rows. |
title | ReactNode | — | Card title shown in the title row. A string title also names the table for screen readers when label is not given. |
subtitle | ReactNode | — | Muted qualifier after the title, such as a period ("This week"). |
icon | ReactNode | — | Icon before the title, drawn in a tinted 32px rounded square. Decorative (aria-hidden). |
actions | ReactNode | — | Controls at the right end of the title row (buttons, a switch, a menu). |
label | string | — | Table name for screen readers (the <caption>). Defaults to title when title is a string. |
...rest | HTMLAttributes<HTMLDivElement> | — | Other attributes go to the root <div>. |
Accesibilidad
- The table is role="treegrid"; each row has aria-level, aria-posinset and aria-setsize, and rows with children have aria-expanded.
- The whole table is one Tab stop (landing on the row focused last): Up and Down move by row, Right expands or enters the first child, Left collapses or returns to the parent, Home and End go to the ends, Enter or Space toggles.
- When a row collapses while focus is inside its descendants, focus returns to that row instead of falling to the page start.
- The focus ring spans the row, drawn inside the cells; the chevron is a mouse target only and hidden from screen readers, because the state lives on the row.
- Level is not only colour: indent, guide lines, chevron direction and name weight say it together; on touch the header is 44px and the chevron target fills the row height.
- With reduced motion children appear at once and other rows jump to place.
Movimiento
Child entrance: opacity 0 to 1 and translateY -8px to 0 over 240ms ease-out, staggered 40ms per row (guide lines travel with their rows). Rows making room: translateY over 240ms standard (Web Animations, can continue mid-flight). The chevron turns 90° with a light spring over 360ms.
Añádelo a tu proyecto
Los miembros Pro copian un prompt y su agente de programación instala el código, igual que con los gratuitos.
TreeTable.jsxtree-table.csstokens.cssREADME.md
Los dos archivos se instalan juntos. La hoja de estilos lee las variables de tokens.css y lleva valores de reserva, así que también funciona sola.





