本文へ移動
日本語

仕組み

Rows are 36px pills (44px on touch) with a chevron slot, optional check box, optional icon and text; each level indents by indent. There are no borders or separators: hover is a filled surface-2 pill, the selected row a tinted accent pill, and search matches get a tinted highlight. Every node, collapsed or not, is rendered in the DOM: expanding animates grid-template-rows from 0fr to 1fr without measuring while the children slide down into place, and collapsed levels are visibility: hidden. Expansion, selection and checking are independent and each can be controlled. By default checks link parent and children, and a disabled node is the boundary of that linking. Row corners follow --ds-treeview-radius.

向いている場面

  • Assigning permissions to a role: a group of three or four levels, where checking a parent selects a whole group and the indeterminate state shows partial access.
  • Choosing from hierarchical data such as product categories, regions or departments.
  • A directory on the left of a page: click a node and show its content on the right.
  • Many nodes: put a search field above and pass its keyword to filter.

向かない場面

  • Each node needs several columns of numbers beside it: use a tree table.
  • Only one level: use a checkbox group or a plain list.
  • Hierarchical choice in the space of one input: use a cascading select.
  • Tens of thousands of nodes: all nodes are rendered in the DOM, so add lazy loading or virtual scrolling.
  • The main navigation of an app: use a sidebar nav, which has link and current-page semantics rather than a tree.

使い方

import { useState } from 'react'
import { TreeView } from './TreeView.jsx'

const menus = [
  {
    key: 'files',
    label: 'Files',
    children: [
      { key: 'files-upload', label: 'Upload and delete' },
      { key: 'files-rename', label: 'Rename' },
    ],
  },
  {
    key: 'billing',
    label: 'Billing',
    children: [
      { key: 'billing-report', label: 'Monthly report' },
      { key: 'billing-plan', label: 'Change plan (owner only)', disabled: true },
    ],
  },
]

// Role permissions: check boxes with parent-child linking and a search filter above
export function RolePermissions() {
  const [checked, setChecked] = useState(['files-upload'])
  const [keyword, setKeyword] = useState('')
  return (
    <>
      <input type="search" value={keyword} onChange={(e) => setKeyword(e.target.value)} aria-label="Search permissions" />
      <TreeView
        aria-label="Permissions"
        nodes={menus}
        defaultExpandAll
        checkable
        selectable={false}
        checkedKeys={checked}
        onCheck={(keys) => setChecked(keys)}
        filter={keyword}
      />
    </>
  )
}

// As a directory: clicking a row selects it (single choice)
export function Directory({ onPick }) {
  return <TreeView aria-label="Directory" nodes={menus} defaultExpandedKeys={['files']} onSelect={onPick} />
}

プロパティ

プロパティ型既定値説明
nodes{ key, label, icon?, disabled?, children? }[]—Top-level nodes. key is a string unique in the whole tree; label can be any ReactNode; icon is a decorative 18px icon before the text.
expandedKeysstring[]—Controlled: the expanded nodes. Omit to let the component keep them.
defaultExpandedKeysstring[][]Uncontrolled: which nodes start expanded.
defaultExpandAllbooleanfalseUncontrolled: start fully expanded (takes priority over defaultExpandedKeys).
onExpandedChange(keys: string[]) => void—Called on expand or collapse. Temporary changes while searching do not trigger it.
selectablebooleantrueWhether a click selects a row (single choice). When off, a click checks the row in a checkable tree, otherwise expands or collapses it.
selectedKeystring | null—Controlled: the selected node; null means none.
defaultSelectedKeystring | nullnullUncontrolled: the initially selected node.
onSelect(key, node) => void—Called when another node is selected. Clicking the selected row again does not fire it or deselect.
checkablebooleanfalseShows a check box before each row.
checkedKeysstring[]—Controlled: the checked nodes. With linking, passing only a parent means all its enabled descendants are checked.
defaultCheckedKeysstring[][]Uncontrolled: the initially checked nodes.
onCheck(checkedKeys, { node, checked, halfCheckedKeys }) => void—Called when checks change with all checked nodes (including computed parents) in tree order; info has the clicked node, its new state and the indeterminate parents.
checkStrictlybooleanfalseNo parent-child linking: every node is checked on its own, with no indeterminate state.
filterstring—Search keyword from an input outside: shows matching nodes and their ancestors, expanded, with matches highlighted. Empty means no search.
filterNode(keyword, node) => boolean—Custom match rule. By default the label text is searched, ignoring case.
indentnumber20Indent per level, in px.
emptyTextReactNode—Text shown when there is nothing to display. Defaults to the labels noMatch (while searching) or noData.
labelsobject—Replaces built-in text (noMatch, noData, found). Shallow-merged over the English defaults.
...restHTMLAttributes<HTMLDivElement>—className, style and others go to the root <div>; aria-label, aria-labelledby and aria-describedby go to the role="tree" element to name the tree.

アクセシビリティ

  • role="tree" > role="treeitem" > role="group", with aria-level, aria-posinset, aria-setsize, aria-expanded, aria-selected, aria-checked (mixed when partial) and aria-disabled where they apply.
  • The whole tree is one Tab stop (the row last visited, or the selected one): Up and Down move by row, Right expands or enters the first child, Left collapses or goes to the parent, Home and End jump, Enter selects, Space checks, typing jumps to a matching node.
  • Collapsed subtrees are visibility: hidden, so they cannot be tabbed to or read; if focus is inside when collapsing, it returns to the collapsed row.
  • The focus ring is drawn inside the row so scroll containers do not clip it, and keyboard moves scroll only that row into view.
  • Search results are announced through aria-live, such as “3 matching nodes found” or “No matching nodes”.
  • State is not colour alone: tick, dash, chevron direction and indentation; high-contrast mode uses the system highlight colours.

モーション

Expand 240ms ease-out with the children sliding 6px down into place; collapse 160ms ease-in. The chevron rotates 90° in 240ms. The check box shrinks while pressed and springs back with a small overshoot; the tick draws in 160ms and the indeterminate dash grows from the middle in 160ms.

プロジェクトに追加

プロンプトを Codex または Claude Code に貼り付けます。エージェントが公開ソースを読み、書き込む内容を示したうえで、既存のコンポーネントの隣に追加します。

TreeView.jsxtree-view.csstokens.cssREADME.md

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

リスト
「アーチ型カルーセル」は Pro に含まれます。解除するとプレビューが表示されます。
「円柱カルーセル」は Pro に含まれます。解除するとプレビューが表示されます。