<!-- index: /slotted/llms.txt -->
# tree

`data-ui="tree"` · Hierarchy of items that expand and select.

## Import

```tsx
import {
  Tree,
  TreeDefaultsProvider,
  TreeDescription,
  TreeEmpty,
  TreeGroup,
  TreeIcon,
  TreeLabel,
  TreeMeta,
  TreeNode,
} from '@slotted/react';
```

## Minimal example

Example

```tsx
import { Tree, TreeNode } from '@slotted/react/tree';

export default function TreeBasic() {
  return (
    <Tree aria-label="Product categories" defaultExpandedKeys={['beverages']} defaultValue="coffee">
      <TreeNode value="beverages" label="Beverages" meta="148">
        <TreeNode value="coffee" label="Coffee" meta="62" />
        <TreeNode value="tea" label="Tea" meta="54" />
        <TreeNode value="soft-drinks" label="Soft drinks" meta="32" />
      </TreeNode>
      <TreeNode value="pantry" label="Pantry" meta="231">
        <TreeNode value="oils" label="Oils and vinegars" meta="87" />
        <TreeNode value="spices" label="Spices" meta="144" />
      </TreeNode>
      <TreeNode value="household" label="Household" meta="76" />
    </Tree>
  );
}
```

## Anatomy

| Part | Element | Required |
| --- | --- | --- |
| `root` | div, or the element render returns | yes |
| `canvas` | ul with role="tree" | yes |
| `node` | li with role="treeitem" | yes |
| `row` | div | yes |
| `guide` | span | no |
| `expander` | span | yes |
| `indicator` | span | no |
| `icon` | span | no |
| `label` | span | yes |
| `description` | span | no |
| `meta` | span | no |
| `loader` | span with data-ui="spinner" | no |
| `actions` | div | no |
| `group` | ul with role="group" | no |
| `empty` | div | no |

## Props

| Name | Type | Default | Applies to | Description |
| --- | --- | --- | --- | --- |
| `value` | `string \| readonly string[] \| null` | — | Tree | The chosen key, or the set of chosen keys in multiple. When it is present the selection is controlled. |
| `defaultValue` | `string \| readonly string[] \| null` | — | Tree | The selection an uncontrolled tree starts with. |
| `expandedKeys` | `readonly string[]` | — | Tree | The keys of the open branches. When it is present the expansion is controlled. A query never changes it. |
| `defaultExpandedKeys` | `readonly string[]` | — | Tree | The branches an uncontrolled tree starts with open. |
| `activeKey` | `string \| null` | — | Tree | The node the keyboard is on, which is separate from the selection. When it is present the position is controlled. |
| `defaultActiveKey` | `string \| null` | — | Tree | The node the keyboard starts on in an uncontrolled tree. |
| `size` | `'sm' \| 'md' \| 'lg'` | md | Tree | The row height, the type size, the glyph size and the indent, which move together. |
| `variant` | `'plain' \| 'framed'` | plain | Tree | Whether the tree draws its own box. plain draws none and framed draws an edge, a radius and a shadow. |
| `structure` | `'auto' \| 'nested' \| 'flat'` | auto | Tree | Whether the nodes nest in groups or lie flat with their level stated. auto is flat while virtual and nested otherwise. Only a tree built from nodes can be flat. |
| `selectionMode` | `'none' \| 'single' \| 'multiple'` | single | Tree | How many nodes can be chosen at once. none writes no selection attribute on any node. |
| `selectionBehavior` | `'toggle' \| 'replace'` | toggle | Tree | What a plain press does. toggle changes one node and needs no modifier key; replace collapses the selection onto the node pressed, and Ctrl, Command and Shift extend it. |
| `selectionIndicator` | `'auto' \| 'highlight' \| 'checkbox' \| 'none'` | auto | Tree | How the selection is drawn and which attribute states it. highlight writes aria-selected, checkbox writes aria-checked, and auto draws a checkbox in multiple and a highlight in single. |
| `selectionCascade` | `'none' \| 'descendants'` | none | Tree | Whether choosing a branch also chooses its enabled descendants. descendants needs nodes. |
| `expandOn` | `'row' \| 'expander'` | row | Tree | What opens a branch. row gives the whole row and the chevron that job; expander leaves it to the chevron alone. |
| `focusMode` | `'auto' \| 'roving' \| 'activedescendant'` | auto | Tree | How the position of the keyboard is expressed. auto is activedescendant while virtual and a roving tab stop otherwise. |
| `guides` | `boolean` | true | Tree | Whether the lines that tie a row to its parent are drawn. |
| `loop` | `boolean` | false | Tree | Whether the arrow keys wrap past the first and the last node. |
| `typeahead` | `boolean` | true | Tree | Whether typing a name moves the keyboard to the node that starts with it. |
| `typeaheadTimeout` | `number` | 500 | Tree | The milliseconds of silence that start a new typeahead search. |
| `selectOnFocus` | `boolean` | false | Tree | Whether moving the keyboard also chooses the node it lands on. |
| `navigateDisabled` | `boolean` | false | Tree | Whether the arrow keys land on a disabled node. |
| `disallowEmptySelection` | `boolean` | false | Tree | Whether the last chosen node stays chosen when it is pressed again. |
| `disallowSelectAll` | `boolean` | false | Tree | Whether Control+A is kept from choosing every visible node. |
| `pageSize` | `number` | 10 | Tree | How many nodes Page Up and Page Down move by. |
| `virtual` | `boolean` | false | Tree | Whether only the rows in view are rendered. It needs nodes and itemSize, and it lays the rows flat. |
| `itemSize` | `number` | 32 | Tree | The height of one row in pixels, read while virtual. |
| `overscan` | `number` | 4 | Tree | How many rows are rendered on each side of the rows in view, while virtual. |
| `disabled` | `boolean` | false | Tree | Whether the whole tree is disabled. |
| `readOnly` | `boolean` | false | Tree | Whether the selection is fixed while the tree can still be opened and walked. |
| `required` | `boolean` | false | Tree | Whether the tree states that a choice is required. |
| `invalid` | `boolean` | false | Tree | Whether the tree is drawn and announced as being in error. |
| `loading` | `boolean` | false | Tree | Whether the nodes of the tree are still arriving. It writes aria-busy on the tree. |
| `name` | `string` | — | Tree | The form name. The tree renders one hidden input per chosen key under it. |
| `nodes` | `readonly T[]` | — | Tree | The hierarchy the tree renders. selectionCascade, expandAll, query and virtual read it. |
| `getKey` | `(node: T) => string` | — | Tree | Returns the key of a node. It is required with nodes. |
| `getLabel` | `(node: T) => string` | — | Tree | Returns the name of a node, which is what the row shows and what typeahead and query match. |
| `getChildren` | `(node: T) => readonly T[] \| undefined` | — | Tree | Returns the children of a node, for a nested hierarchy. |
| `getLevel` | `(node: T) => number` | — | Tree | Returns the depth of a node, for nodes that arrive as a flat list in reading order. |
| `hasChildren` | `(node: T) => boolean` | — | Tree | Returns whether a node is a branch before its children have arrived. |
| `getChildrenCount` | `(node: T) => number \| undefined` | — | Tree | Returns how many children a branch will have. Until all of them arrive, the ones present state the size of the whole set. |
| `isNodeDisabled` | `(node: T) => boolean` | — | Tree | Returns whether a node is disabled. |
| `isNodeLoading` | `(node: T) => boolean` | — | Tree | Returns whether the children of a node are arriving. |
| `query` | `string` | — | Tree | The text the tree filters nodes by. The field it is typed into is yours. |
| `renderCells` | `(node: T, key: string) => TreeNodeCells` | — | Tree | Returns the icon, label, description, meta and actions of one row of nodes. A cell left out is not drawn, and a label left out is the name getLabel returns. |
| `messages` | `PartialMessages<TreeMessages>` | — | Tree | The words the tree announces, over the ones of the language in force. |
| `emptyContent` | `ReactNode` | — | Tree | What the tree shows when it holds no node or the query matches none. |
| `render` | `(props: TreeRootProps) => ReactNode` | — | Tree | Renders the root as another element. |
| `handleRef` | `Ref<TreeHandle>` | — | Tree | The imperative handle: focus, scrollToKey, expand, collapse, expandAll, collapseAll, getActiveKey and getElement. |
| `label` | `ReactNode` | — | TreeNode | The name of the node, which is also its accessible name. |
| `description` | `ReactNode` | — | TreeNode | A second, muted text after the name. It is hidden when the tree is narrower than 240 pixels. |
| `icon` | `ReactNode` | — | TreeNode | The leading icon. One node with an icon reserves the same cell on every row. |
| `meta` | `ReactNode` | — | TreeNode | The trailing count, size or badge. |
| `actions` | `ReactNode` | — | TreeNode | The trailing controls. A press on one of them does not choose or open the row. |
| `textValue` | `string` | — | TreeNode | The text typeahead matches, for a label that is not plain text. |
| `match` | `'hit' \| 'ancestor'` | — | TreeNode | Whether a query matched this node or one below it. The tree writes it on the rows of nodes; a tree you write by hand states it. |
| `pinned` | `boolean` | — | TreeNode | Whether the node is placed outside the rows in view. The tree sets it while virtual. |
| `offset` | `number` | — | TreeNode | Where a pinned row sits, in pixels. The tree sets it while virtual. |
| `depth` | `number` | — | TreeNode | The depth of the node in the flat structure. Elsewhere the markup states it. |
| `TreeDefaultsProvider` | `{ size, variant, expandOn, … }` | — | a subtree | Sets the axes once for every tree under it. A prop on a tree still wins. |
| `render` | `(props) => ReactNode` | — | Tree, TreeNode | Renders the part as another element with the attributes and handlers it receives. |
| `label, description, icon, meta, actions` | `ReactNode` | — | TreeNode | The cells of a row written by hand. Each takes any content. |
| `handleRef` | `Ref<TreeHandle>` | — | Tree | Reaches focus, scrollToKey, expand, collapse, expandAll, collapseAll, getActiveKey and getElement. |
| `onClick` | `(event: MouseEvent) => void` | — | TreeNode | Runs before the row acts, and preventDefault keeps the row from acting. |
| `data-expanded, data-selected, data-loading` | `attribute` | — | the node | The state of a node in the DOM, which lets CSS change an icon by state. |

## Events

| Name | Payload | When |
| --- | --- | --- |
| `onValueChange` | `(value: string \| readonly string[] \| null, request: ChangeRequest)` | Before the selection changes. The value is one key, or the set in multiple. The request carries the previous value, the reason (pointer, keyboard, select, clear or programmatic), the originating event and cancel(). |
| `onExpandedKeysChange` | `(keys: readonly string[], request: ChangeRequest)` | Before a branch opens or closes. The reasons are pointer, keyboard and programmatic. A query holds branches open without this event. |
| `onActiveKeyChange` | `(key: string \| null, request: ChangeRequest)` | Before the keyboard moves to another node. The reasons are pointer, keyboard and programmatic. Closing a branch that holds the active node emits this event first. |
| `onNodeActivate` | `(key: string, reason: 'pointer' \| 'keyboard')` | When a node is pressed, or Enter is pressed on it. Use it to open what the node stands for; it does not depend on the selection. |

## Axes and defaults

| Axis | Values | Default |
| --- | --- | --- |
| `size` | `sm`, `md`, `lg` | `md` |
| `variant` | `plain`, `framed` | `plain` |
| `structure` | `auto`, `nested`, `flat` | `auto` |
| `selectionMode` | `none`, `single`, `multiple` | `single` |
| `selectionBehavior` | `toggle`, `replace` | `toggle` |
| `selectionIndicator` | `auto`, `highlight`, `checkbox`, `none` | `auto` |
| `selectionCascade` | `none`, `descendants` | `none` |
| `expandOn` | `row`, `expander` | `row` |
| `focusMode` | `auto`, `roving`, `activedescendant` | `auto` |
| `guides` | `true`, `false` | `true` |
| `loop` | `true`, `false` | `false` |
| `typeahead` | `true`, `false` | `true` |
| `selectOnFocus` | `true`, `false` | `false` |
| `navigateDisabled` | `true`, `false` | `false` |
| `disallowEmptySelection` | `true`, `false` | `false` |
| `disallowSelectAll` | `true`, `false` | `false` |
| `virtual` | `true`, `false` | `false` |
| `disabled` | `true`, `false` | `false` |

## Rules

- The tree takes its accessible name from aria-label or aria-labelledby, which reach the element with role="tree".
- Each node is named by its own label through aria-labelledby. A treeitem contains its children, so a name computed from its contents would be everything under it.
- aria-expanded is written on branches and never on leaves, which would otherwise be announced as parents.
- Selection is stated with aria-selected under the highlight and with aria-checked under the checkbox, and never with both.
- aria-level, aria-setsize and aria-posinset are written while the rows are flat. A nested tree leaves them to the browser, except on the children of a branch that is still missing some of them.
- The whole tree is one tab stop. Focus lands on the chosen node when there is one and on the first node otherwise, and a virtual tree keeps the active node in the document.
- A control in the trailing cell of a row is a tab stop of its own and receives its own keys.
- A branch whose children are arriving has aria-busy, and a polite live region announces the branch and how many items arrived.

## Tokens

`--slotted-border-strong`, `--slotted-border-subtle`, `--slotted-border-width-medium`, `--slotted-border-width-thin`, `--slotted-control-border-color`, `--slotted-control-font-size-lg`, `--slotted-control-font-size-md`, `--slotted-control-font-size-sm`, `--slotted-control-gap-lg`, `--slotted-control-gap-md`, `--slotted-control-gap-sm`, `--slotted-control-height-lg`, `--slotted-control-height-md`, `--slotted-control-height-sm`, `--slotted-control-icon-size-lg`, `--slotted-control-icon-size-md`, `--slotted-control-icon-size-sm`, `--slotted-control-invalid-edge-width`, `--slotted-control-padding-inline-lg`, `--slotted-control-padding-inline-md`, `--slotted-control-padding-inline-sm`, `--slotted-control-shadow`, `--slotted-control-transition-duration`, `--slotted-control-transition-easing`, `--slotted-disabled-background`, `--slotted-disabled-border`, `--slotted-disabled-foreground`, `--slotted-duration-medium`, `--slotted-duration-short`, `--slotted-easing-standard`, `--slotted-focus-ring-color`, `--slotted-focus-ring-color-invalid`, `--slotted-focus-ring-width`, `--slotted-font-family-sans`, `--slotted-font-size-xs`, `--slotted-font-weight-semibold`, `--slotted-highlight-tint-opacity`, `--slotted-line-height-sm`, `--slotted-radius-md`, `--slotted-radius-xs`, `--slotted-selection-size-lg`, `--slotted-selection-size-md`, `--slotted-selection-size-sm`, `--slotted-space-1`, `--slotted-space-2`, `--slotted-space-3`, `--slotted-space-4`, `--slotted-space-5`, `--slotted-surface`, `--slotted-target-min`, `--slotted-text`, `--slotted-text-muted`, `--slotted-tone-accent-border`, `--slotted-tone-accent-on-solid`, `--slotted-tone-accent-solid`, `--slotted-tone-accent-surface`, `--slotted-tone-accent-text`, `--slotted-tone-danger-border`, `--slotted-tone-secondary-subtle-active`, `--slotted-tone-secondary-subtle-hover`, `--slotted-tree-border-color`, `--slotted-tree-guide-color`, `--slotted-tree-highlight`, `--slotted-tree-highlight-active`, `--slotted-tree-indent`, `--slotted-tree-mark-inset`, `--slotted-tree-measure`, `--slotted-tree-padding-block`, `--slotted-tree-padding-inline`, `--slotted-tree-radius`, `--slotted-tree-rows`, `--slotted-tree-selected-edge`, `--slotted-tree-selected-surface`, `--slotted-tree-selected-text`, `--slotted-tree-shadow`, `--slotted-tree-surface`, `--slotted-tree-text`, `--slotted-tree-text-muted`

## Examples

See [tree.examples.md](./tree.examples.md).
