本文へ移動
日本語
「ツリーテーブル」は Pro に含まれます。解除するとプレビューが表示されます。

仕組み

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.

向いている場面

  • 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.

向かない場面

  • 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.

使い方

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"
    />
  )
}

プロパティ

プロパティ型既定値説明
columnsColumn[]—Column definitions: key (also the default field to read), title, width, align, render(row, index, info) where info is { level, hasChildren, expanded }.
rowsobject[]—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.
treeColumnstringkey of the first columnWhich 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.
indentnumber20Indent per level in pixels; the guide lines are spaced the same.
toggleOnRowClickbooleantrueClicking anywhere on a row with children toggles it. When false only the chevron works; mark your own clickable elements with data-no-toggle.
animatebooleantrueWhether rows move when expanding or collapsing. Skipped automatically above 200 visible rows.
emptyTextReactNode'No data'Text shown when there are no rows.
titleReactNode—Card title shown in the title row. A string title also names the table for screen readers when label is not given.
subtitleReactNode—Muted qualifier after the title, such as a period ("This week").
iconReactNode—Icon before the title, drawn in a tinted 32px rounded square. Decorative (aria-hidden).
actionsReactNode—Controls at the right end of the title row (buttons, a switch, a menu).
labelstring—Table name for screen readers (the <caption>). Defaults to title when title is a string.
...restHTMLAttributes<HTMLDivElement>—Other attributes go to the root <div>.

アクセシビリティ

  • 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.

モーション

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.

プロジェクトに追加

Pro メンバーはプロンプトをコピーし、コーディングエージェントが無料コンポーネントと同じ手順でソースを導入します。

TreeTable.jsxtree-table.csstokens.cssREADME.md

2 つのファイルを一緒に導入します。スタイルは tokens.css の変数を読み、予備の値も持つため、単体でも表示できます。

テーブル
「比較テーブル」は Pro に含まれます。解除するとプレビューが表示されます。
「データバー付きテーブル」は Pro に含まれます。解除するとプレビューが表示されます。