Tree
Tree shows nested items that you can open, close and select, such as a file browser or a layers panel.
<Tree items={files} multiselectable defaultExpandedIds={["src"]} aria-label="Project files" />Use a Tree for nested content that people open, close and select, such as a file browser, a layers panel or folders with sub-folders. By default a click selects one row, and multiselectable lets people select several.
For navigation, where pressing a row opens a page, use a NavTree instead. For a short flat list of choices, use a Menu or a Select.
You pass the content as data through items. Each TreeNode has an id and a label, and a node with children shows a disclosure arrow. Add leading for an icon, and set textValue whenever the label is not plain text, so that typing a letter can still find the row.
size sets the row height, the text and the indent, and one level of indent is the width of one icon at that size. Selected rows get a soft fill.
Screen readers announce the tree with the level, position, open state and selected state of each row. The tree is a single tab stop. Up and Down move between visible rows, Right opens a row or moves to its first child, and Left closes a row or moves to its parent. Home and End go to the first and last rows, Enter or Space selects, and typing a letter jumps to the next matching row.
With multiselectable, Shift with an arrow key or a click selects a range, and Command or Control with a click adds or removes a single row.
Props
Generated from the types. It lists what this component DECLARES: a prop that arrives from a shared type or from the platform is real and is not repeated here.
| Prop | Type | What it does |
|---|---|---|
defaultExpandedIds? | readonly string[] | Uncontrolled starting expansion. |
defaultSelectedIds? | readonly string[] | Uncontrolled starting selection. |
expandedIds? | readonly string[] | Controlled expansion, paired with onExpandedChange. |
items | readonly TreeNode[] | The hierarchy, as data. |
multiselectable? | boolean | Lets the user select more than one row. |
onExpandedChange? | (ids: string[]) => void | Fires when a node opens or closes, with the whole expanded set. |
onSelectionChange? | (ids: string[]) => void | Fires when the selection changes, with the whole selected set. |
selectedIds? | readonly string[] | Controlled selection, paired with onSelectionChange. |
size? | "1" | "2" | "3" | "4" | Sets the size step of the rows. |
It also takes every prop of a <div>.
- Drag to reorder — The rules for where an item may be dropped are different in every app, so reordering is left to your code.
- Selecting a folder's children with it — Whether selecting a folder also selects its contents is a product decision. The tree selects exactly the rows that the person picks.
- Loading children on demand — The tree does not yet load branches over the network or show a loading state, so pass the full data you want to show.
- Renaming in place — Your app decides what counts as a valid name and when a rename is saved, so build the rename field in your own code.
- JSX children — The tree renders its rows as one flat list with ARIA levels, so it needs the hierarchy as data. Pass it through
items. - An
indentprop — One level of indent is always one icon wide at the current size, so the indent grows and shrinks withsize.