TreeView
TreeView renders a nested, expandable tree. It flattens a recursive node structure, tracks the expanded and selected state, and wires the WAI-ARIA tree pattern with full keyboard navigation.
What it is
A single component over a TreeNode array. It flattens the visible nodes — those whose ancestors
are expanded — and hands each one to your renderNode function with its depth and its expanded,
selected, and focused flags, plus toggle and select callbacks. Expansion and selection state
are managed for you; the markup of each row is yours.
Install
API
TreeView
stablefunction TreeView<T>(props: TreeViewProps<T>): JSX.Element
The root nodes. Each node may carry its own `children`.
Renders one node. `info` carries `node`, `depth`, `isExpanded`, `isSelected`, `isFocused`, `toggle`, and `select`.
The selected node id, controlled or uncontrolled.
Node ids expanded on first render.
Names the tree.
Inline style for the tree container.
TreeNode
Keyboard
| Key | Action |
|---|---|
| Arrow Down / Up | Move focus to the next / previous visible node |
| Arrow Right | Expand a collapsed branch |
| Arrow Left | Collapse an expanded branch |
| Enter / Space | Select the focused node |
Accessibility
TreeView implements the WAI-ARIA tree pattern. The container is role="tree"; each row is
role="treeitem" with aria-level for its depth, aria-expanded on branches, aria-selected,
and aria-disabled where set. A screen reader announces the node's depth, whether it is open,
and whether it is selected.
Indentation is the visual cue for depth — but aria-level is the one assistive technology reads.
Both come from the depth TreeView gives renderNode; use it for the visual indent and trust
the component to set the ARIA.
Examples
A file tree: