Web · Components
TreeView
Tree view: for nested data like a file explorer, category tree, org chart, permission list.
Live example
Preview
Code
Events
- Events appear here when you interact with the preview.
Checkboxes: a three-state permission tree, submitted in a form
Lazy loading: folders from the server, error and retry
100k nodes: virtualization, sticky parent nodes, search
101,100 nodes, all expanded.
Import
Tree view: for nested data like a file explorer, category tree, org chart, permission list. Works with a single items array.
- Expand/collapse: Chevron, clicking the row (
expandOnClick), double-click, keyboard. Expanded child rows slide in. WithstorageKeyexpansion survives page reloads. - Selection: Single or multiple (Ctrl/⌘, Shift range, Ctrl/⌘+A).
- Checkboxes: Tri-state, parent-child linked or independent; with
checkOutputall/leaf/top nodes; submitted in a form withname. - Lazy loading:
hasChildren+loadChildren; loading and error (Try again) rows. - Search: A built-in box or
query; insensitive to locale-specific letters/accents, matches highlighted and their paths expanded; filtering or highlight only. - Drag and drop: Before/after/inside drop indicator, multi-move, auto-scroll at the edge, expand on hover, cancel with Esc, keyboard moving (Alt+arrows). On touch from the handle.
- Rename: F2; Enter saves, Esc cancels; shows errors.
- Row content: Icon (a different icon when expanded), description, badge, action buttons appearing on hover, tooltip, link (
href). - Context menu:
contextMenu; Shift+F10, long press on touch or the ⋯ button. - Keyboard (WAI-ARIA tree): ↑/↓, →/← (expand/collapse, go to child/parent), Home/End, PageUp/PageDown,
*(expand siblings), type-ahead, Enter (onAction), Space (select/check), F2, Delete. In right-to-left languages →/← swap. - Performance: Smooth even with hundreds of thousands of nodes: rows are virtualized and the tree is flattened once; with
stickyParentsparent nodes stay stuck. - Accessibility:
role="tree",aria-level/setsize/posinset,aria-activedescendant, operations are announced via a live region.
Uses a fixed row height (for virtualization); descriptions fit on a single line. Give the tree a height: className="h-96".
Examples
nodeText
functionPlain text of a node used for search and type-ahead.
canHaveChildren
functionCan the node have children (loaded or to be loaded)?
indexTree
functionIndexes all known nodes (including loaded children) by id. Walks with a stack; no stack overflow on deep trees.
ancestorsOf
functionAncestor ids of a node from the root down (excluding itself, starting at the root).
descendantIds
functionThe node itself and all its (known) descendants.
flattenTree
functionConverts the tree into an array of visible rows: children of expanded nodes come right after their parent. Virtualization and keyboard navigation work on this array. Walks with a stack; fast even at 100k rows.
foldText
functionFolds text for search: Turkish lowercase, without accents (see matchRanges).
matchRanges
functionReturns where the query occurs in the text, with indices of the original text. Turkish case and accent differences are ignored ("sehir" → "Şehir").
filterTree
functionFinds the nodes matching a query and the path to them. Searches only known (loaded) nodes.
computeCheckStates
functionComputes each node's state from the checked set. In connected (cascade) mode a node with children takes its state from them: checked when all are checked, mixed when some are. A parent in the set counts all its descendants as checked.
checkedOutput
functionProduces the checked value from a state map. See CheckOutput.
toggleCheck
functionReturns the new checked set when a node's checkbox is pressed. In connected mode the node and all its descendants are checked or unchecked together (disabled ones and those with checkable: false don't change); the parents' entries in the set are removed and their state comes from their children.
updateNode
functionChanges a node. Returns a new tree; only nodes along the path are copied.
Examples
removeNodes
functionRemoves nodes (and their descendants) from the tree.
insertNodes
functionInserts nodes at a given position. With parentId null they are added to the root; at the end when index is omitted.
moveNodes
functionMoves nodes to a new position (the result of drag and drop). The target position is given relative to the tree before the move; the shift when moving down within the same list is corrected automatically. A node can't be moved under itself; such a request returns the tree unchanged.
Examples
materializeTree
functionWrites loaded children (loadChildren results) into the tree, so the tree can be stored or passed around as a single piece of data.
TreeNodeBase
interfaceNode fields used by tree math (platform nodes extend this).
| Name | Type | Description |
|---|---|---|
id | string | Unique id within the tree. |
label? | unknown | Display name; when it is text it is used for search and type-ahead. |
textValue? | string | Plain text for search when the name isn't text. |
children? | readonly TreeNodeBase[] | Child nodes. |
hasChildren? | boolean | Children will be loaded later. |
disabled? | boolean | Disabled node. |
selectable? | boolean | Selectable? |
checkable? | boolean | Checkable? |
TreeAction
interfaceSmall action button at the end of a TreeNode row.
| Name | Type | Default | Description |
|---|---|---|---|
icon | IconProp | Icon of the button: name or element. | |
label | string | Name of the button; it is the tooltip and screen reader text. | |
onSelect | (node: TreeNode<T>) => void | Called with the node when clicked. | |
tone? | "neutral" | "danger" | "neutral" | Color. |
TreeNode
interfaceA node inside TreeView. Only id and label are required.
Extends: TreeNodeBase
| Name | Type | Default | Description |
|---|---|---|---|
id | string | Unique key across the whole tree. Selection, expansion and moving use it. | |
label | ReactNode | Name shown in the row. Text is recommended for search and type-ahead. | |
textValue? | string | Plain text used in search, type-ahead and the drag preview if label isn't text. | |
icon? | IconProp | Icon before the name: name or element. | |
expandedIcon? | IconProp | Icon shown while the node is expanded (e.g. an open folder). | |
description? | ReactNode | Short faded description next to the name (single line). | |
badge? | ReactNode | Badge at the end of the row: a number or short text. 0 isn't shown, "" draws a dot. | |
badgeTone? | Tone | "neutral" | Badge color. |
children? | readonly TreeNode<T>[] | Child nodes. An empty array means "can have children but has none" (e.g. an empty folder). | |
hasChildren? | boolean | Child nodes aren't loaded yet but exist. When expanded, TreeView requests them with loadChildren. | |
disabled? | boolean | Can't be selected, checked or dragged; can still be expanded and collapsed. | |
selectable? | boolean | true | Whether this node can be selected. |
checkable? | boolean | true | Whether this node shows a checkbox (while the tree has checkboxes on). |
draggable? | boolean | true | Whether this node can be dragged (while the tree has draggable on). |
droppable? | boolean | Can items be dropped into it? Default: nodes that can have children (children or hasChildren given) accept, leaves don't. | |
editable? | boolean | true | Whether it can be renamed (if the tree has onRename). |
href? | string | Link: if given, clicking the name goes to this address (with the UIProvider link). | |
tooltip? | TooltipProp | Tooltip of the row. See TooltipProp. | |
actions? | readonly TreeAction<T>[] | Action buttons at the end of the row that appear on hover. | |
menu? | readonly MenuEntry[] | Context menu of this node only; replaces the tree's contextMenu. | |
className? | string | Classes added to the row. | |
data? | T | Your own data; it comes with the node in callbacks. |
Examples
FlatStatusRow
interfaceStatus row for children being loaded or failing to load.
| Name | Type | Description |
|---|---|---|
kind | "loading" | "error" | "empty" | Row kind: children loading, failed to load, or empty. |
id | string | Id of the row (generated from the parent). |
parentId | string | Id of the parent whose status is shown. |
level | number | Depth; one below the parent. |
GetChildren
typeFunction returning the children of a node (given or loaded).
TreeIndex
typeIndex of the tree by id.
FlatNodeRow
typeA visible node row.
FlatRow
typeA row of the flattened tree.
FlattenOptions
typeOptions of flattenTree.
TreeFilterResult
interfaceResult of filterTree.
| Name | Type | Description |
|---|---|---|
matched | Set<string> | Nodes matching the query. |
keep | Set<string> | Nodes that must be visible: the matches and their ancestors. |
parents | Set<string> | Nodes with a matching descendant; expanded automatically during search. |
CheckState
typeState of the checkbox.
CheckOutput
typeWhich nodes the TreeView checked value contains.
"all": all checked nodes (including parents)."leaves": only checked leaves (e.g. selected files)."top": the topmost fully checked nodes; their descendants aren't listed (the shortest list for saying "all of this folder").
TreeLocation
interfaceA position in the tree: the parent (null for the root) and the index in its child list.
| Name | Type | Description |
|---|---|---|
parentId | string | null | Id of the parent (null for the root). |
index | number | Index in the parent's child list (starts at 0). |
TreeViewSize
typeSize of a TreeView row.
TreeRowState
interfaceRow state given to the TreeView renderLabel function.
| Name | Type | Description |
|---|---|---|
level | number | Depth; roots are 1. |
expanded | boolean | Is the node expanded? |
selected | boolean | Is the node selected? |
focused | boolean | Is keyboard focus on this row? |
checked | CheckState | State of the checkbox. |
query | string | The search query ("" if empty). |
highlight | (text: string) => ReactNode | Marks query matches in the text with <mark>. |
Selection mode of TreeView.
"none": Rows aren't selected (only expand/collapse, checkboxes or actions)."single": A single row is selected."multiple": One by one with Ctrl/⌘, a range with Shift, all with Ctrl/⌘+A. On touch, tapping while a row is selected adds to/removes from the selection.
TreeDropPosition
typePosition of the drop relative to the row.
TreeMoveInfo
interfaceInfo of TreeViewProps.canDrop and TreeViewProps.onMove.
| Name | Type | Description |
|---|---|---|
ids | string[] | Ids of the moved nodes (in tree order). |
nodes | TreeNode<T>[] | The moved nodes. |
parentId | string | null | New parent (null for the root). |
parent | TreeNode<T> | null | New parent (null for the root). |
index | number | Index in the new parent's child list, relative to the tree before the move. |
targetId | string | null | The row dropped on and the drop position relative to it (filled for keyboard moves too). |
position | TreeDropPosition | Position of the drop relative to the target row: before, after or inside. |
TreeViewApi
interfaceTools obtained with apiRef to control TreeView from outside.
| Name | Type | Description |
|---|---|---|
expand | (ids: string | readonly string[]) => void | Expands nodes. |
collapse | (ids: string | readonly string[]) => void | Collapses nodes. |
toggle | (id: string) => void | Expands or collapses a node. |
expandAll | () => void | Expands all known nodes (unloaded children aren't requested). |
collapseAll | () => void | Collapses all nodes. |
reveal | (id: string) => void | Expands its parents, makes the row visible and moves keyboard focus to it. |
focus | (id?: string) => void | Focuses the tree (that row if given). |
select | (ids: readonly string[]) => void | Changes the selection. |
rename | (id: string) => void | Starts inline rename. If the node isn't in the tree yet (e.g. just added) it starts on the first render where it appears. |
reload | (id: string) => void | Drops the lazily loaded children and requests them again. |
getNode | (id: string) => TreeNode<T> | undefined | Node by id (loaded children included). |
getPath | (id: string) => string[] | Parent ids of the node starting from the root. |
getItems | () => TreeNode<T>[] | The full tree with loaded children written in. |
TreeViewProps
interfaceProps of the TreeView component.
Extends: Omit<ComponentProps<"div">, "children" | "onSelect" | "defaultValue" | "defaultChecked" | "contextMenu">
| Name | Type | Default | Description |
|---|---|---|---|
items | readonly TreeNode<T>[] | Nodes. See TreeNode. | |
label | string | Screen reader name of the tree (e.g. "Files"). | |
size? | TreeViewSize | "md" | Row height and font size. If omitted, chosen from the UIProvider density setting: compact → "sm", comfortable → "lg". |
indent? | number | 16 | Indent of each level (px). |
guides? | boolean | true | Vertical lines showing levels. The focused node's line darkens. |
expanded? | readonly string[] | Expanded nodes (controlled). | |
defaultExpanded? | readonly string[] | "all" | Initially expanded nodes; "all" expands all known nodes. | |
onExpandedChange? | (ids: string[]) => void | When the expanded nodes change. | |
storageKey? | string | If given, expanded nodes are stored in the browser under this key and restored on page reload (in uncontrolled use). | |
expandOnClick? | boolean | true | Whether clicking a parent's row toggles it. If false, only the chevron, double-click and keyboard expand it. |
selectionMode? | TreeSelectionMode | "single" | Selection mode. See TreeSelectionMode. |
selected? | readonly string[] | Selected nodes (controlled). | |
defaultSelected? | readonly string[] | Initially selected nodes. Their parents expand automatically. | |
onSelectedChange? | (ids: string[], nodes: TreeNode<T>[]) => void | When the selection changes, with ids and nodes. | |
selectionFollowsFocus? | boolean | false | In single selection, whether the selection follows focus while navigating with arrow keys (like a file explorer). |
onAction? | (node: TreeNode<T>) => void | When a row is "activated": double-click or Enter (e.g. to open a file). | |
checkboxes? | boolean | false | Checkboxes. |
checked? | readonly string[] | Checked nodes (controlled). See checkOutput. | |
defaultChecked? | readonly string[] | Initially checked nodes. | |
onCheckedChange? | (ids: string[]) => void | When the checks change. | |
checkMode? | "cascade" | "independent" | "cascade" | "cascade": a parent is linked to its children (tri-state: checked, mixed, empty). "independent": each node on its own. |
checkOutput? | CheckOutput | "all" | Which nodes the checked value contains. See CheckOutput. |
name? | string | Form field name: a hidden field with this name is added for each checked (if checkboxes are on) or selected node; FormData.getAll(name) gives the ids. | |
loadChildren? | (node: TreeNode<T>, context: { signal: AbortSignal; }) => Promise<readonly TreeNode<T>[] | undefined | void> | Fetches the children of a node with hasChildren: true and no children when it is expanded. If you return an array the tree stores it; if you return nothing and update your own items data, that is used. If it throws, "Tekrar dene" appears on the row. | |
query? | string | Search text (controlled). If not empty, matches are highlighted. | |
defaultQuery? | string | Initial search text. | |
onQueryChange? | (query: string) => void | When the search text changes (from the built-in search box too). | |
search? | boolean | string | false | Search box above the tree. true or placeholder text. In the box ↓ or Enter goes to the first match; Esc clears. |
filterMode? | "filter" | "highlight" | "filter" | Whether non-matching branches are hidden in search ("filter") or only matches are highlighted and their paths expanded ("highlight"). |
filter? | (node: TreeNode<T>, query: string) => boolean | Your own matching rule. If omitted, the name (and textValue) is searched insensitive to locale-specific letters and accents. | |
draggable? | boolean | false | Moving by drag and drop. Drag a row with the mouse; on touch drag from the row's handle. On the keyboard Alt+↑/↓ moves the order, Alt+→ inside, Alt+← outside. The result comes via onMove and/or onItemsChange. |
canDrop? | (info: TreeMoveInfo<T>) => boolean | Whether the drop is allowed. In addition to the default rules (can't drop into its own child). | |
onMove? | (info: TreeMoveInfo<T>) => void | When a move happens. | |
onItemsChange? | (items: TreeNode<T>[]) => void | The new tree after a move (loaded children included). If you keep items in state you can pass setItems directly. | |
onRename? | (node: TreeNode<T>, label: string) => void | string | Promise<void | string> | If given, nodes can be renamed: F2, apiRef.rename(id) or from a menu. If you return text (or a Promise resolves with text) it is shown as an error and the box stays open. | |
onDelete? | (nodes: TreeNode<T>[]) => void | When Delete is pressed, with the selected (otherwise the focused) nodes. | |
contextMenu? | readonly MenuEntry[] | ((node: TreeNode<T>, api: TreeViewApi<T>) => readonly MenuEntry[]) | Context menu (long press on touch, Shift+F10 on the keyboard). Pass a function if it varies by node. The node's own menu takes precedence. On touch screens a ⋯ button also appears on the row. | |
renderLabel? | (node: TreeNode<T>, state: TreeRowState) => ReactNode | Your own content instead of the name. | |
empty? | ReactNode | Shown when there are no rows. | |
effect? | EffectProp | Effect or effects of node rows (also plays on the selected and focused row) (e.g. "ripple", "border"). If omitted, UIProvider appearance.defaults.treeView.effect. See BuiltinEffect. | |
stickyParents? | boolean | false | While scrolling, parent nodes stay stuck at the top (like VS Code); clicking goes to that node. The background is taken from the color behind the tree; if you want a different one set the --ui-tree-surface CSS variable. |
virtualize? | boolean | "auto" | "auto" | Draws only visible rows. "auto": with more than 100 rows. Give the tree a height (className="h-96"). |
apiRef? | Ref<TreeViewApi<T>> | Tree API. See TreeViewApi. |