# Tree

Nested items that open and close, such as a file tree, an outline or nested categories. Arrow keys move through the rows and open branches, typing jumps to a name, and items can be selected one at a time, several at a time or with check boxes.

**Dependencies:** npm: 1 direct, 2 total; registry: 3 direct, 3 total.

## Usage

### When to use

- Show nested items that open and close, such as a file tree, an outline or nested categories.
- Pick several nested options at once with check boxes, such as permissions.

### When not to use

- Link a few app sections. Use Sidebar.
- Show flat rows with columns. Use Table.
- Hide and show long sections of content. Use Accordion.

## Variants

### selectionMode

none: items only open, close and run onAction. single: one selected item. multiple: several, with Shift ranges and Ctrl or Cmd to add.

Values: `none`, `single`, `multiple`. Default: `single`.

### size

Row height: sm 28px for dense side panels, md 32px.

Values: `sm`, `md`. Default: `md`.

## Examples

### File tree

Folders open and close, and Enter opens a file.

```tsx
<Tree aria-label="Project files" defaultExpanded={["src"]} className="w-64">
  <TreeItem value="src" label="src" icon={<IconFolder size={16} />}>
    <TreeItem value="src/app.tsx" label="app.tsx" icon={<IconFile size={16} />} />
    <TreeItem value="src/lib" label="lib" icon={<IconFolder size={16} />}>
      <TreeItem value="src/lib/utils.ts" label="utils.ts" icon={<IconFile size={16} />} />
    </TreeItem>
  </TreeItem>
  <TreeItem value="package.json" label="package.json" icon={<IconFile size={16} />} />
</Tree>
```

### Check boxes

Checking a branch checks everything under it; a branch with some items checked shows mixed.

```tsx
<Tree aria-label="Permissions" checkboxes defaultExpanded={["billing"]} defaultValue={["billing.view"]} className="w-64">
  <TreeItem value="billing" label="Billing">
    <TreeItem value="billing.view" label="View invoices" />
    <TreeItem value="billing.pay" label="Pay invoices" />
  </TreeItem>
  <TreeItem value="members" label="Members">
    <TreeItem value="members.invite" label="Invite" />
    <TreeItem value="members.remove" label="Remove" />
  </TreeItem>
</Tree>
```

### Multiple selection, small

Shift with a click or an arrow key selects a range; Ctrl or Cmd adds to it.

```tsx
<Tree aria-label="Layers" selectionMode="multiple" size="sm" defaultExpanded={["header"]} className="w-56">
  <TreeItem value="header" label="Header" trailing="3">
    <TreeItem value="logo" label="Logo" />
    <TreeItem value="nav" label="Navigation" />
    <TreeItem value="search" label="Search" disabled />
  </TreeItem>
  <TreeItem value="hero" label="Hero" />
  <TreeItem value="footer" label="Footer" />
</Tree>
```

## Do and don't

### Do

- Give each item a short, unique name, so typeahead lands where people expect.
- Open the branch that holds the current item on first load with defaultExpanded.
- Use onAction for the main thing an item does, such as opening a file.

### Don't

- Do not use a tree for page navigation with a few sections. Use Sidebar.
- Do not nest more than five or six levels; people lose their place.
- Do not put buttons inside a row; use trailing for read-only content and a ContextMenu for actions.

## Props

| Name | Type | Values | Default | Description |
| --- | --- | --- | --- | --- |
| `selectionMode` | `enum` | `none`, `single`, `multiple` | `single` | none: items only open, close and run onAction. single: one selected item. multiple: several, with Shift ranges and Ctrl or Cmd to add. |
| `value` | `node` |  |  | Controlled selection: an array of item values. In single mode it holds at most one. |
| `defaultValue` | `node` |  |  | Initial selection when uncontrolled: an array of item values. |
| `onValueChange` | `(value: string[]) => void` |  |  | Receives the selected values in tree order. |
| `expanded` | `node` |  |  | Controlled open branches: an array of item values. |
| `defaultExpanded` | `node` |  |  | Branches open at first when uncontrolled. |
| `onExpandedChange` | `(expanded: string[]) => void` |  |  | Receives the values of the open branches. |
| `checkboxes` | `boolean` |  | `false` | A check box on every item, for multiple selection. Checking a branch checks everything under it; a branch with some items checked shows mixed. |
| `onAction` | `function` |  |  | Enter or a double click on an item, such as opening a file. |
| `size` | `enum` | `sm`, `md` | `md` | Row height: sm 28px for dense side panels, md 32px. |
| `disabled` | `boolean` |  | `false` | Set on TreeItem. The item can't be focused, selected or opened, and uses disabled colors. |

### Slots

| Name | Required | Description |
| --- | --- | --- |
| `items` | yes | TreeItems, each with a value and a label; nest TreeItems inside one to make it a branch. |

## Anatomy

| Part | Description |
| --- | --- |
| `root` | The tree: a column of rows with spacing-2xs between them. |
| `treeItem` | A row: the toggle, an optional check box and icon, the label and optional trailing content. Each level steps in by spacing-lg. |
| `toggle` | A 16px chevron on a branch that turns down when it opens; empty space on a leaf, so labels line up. |
| `checkbox` | With checkboxes: a 16px box drawn like Checkbox, with a check when on and a minus when mixed. |
| `icon` | An optional 16px icon before the label. |
| `label` | The item's name on one line, cut with an ellipsis when it does not fit. |
| `trailing` | Optional content at the end of the row, such as a count, in 12px secondary text. |

## Accessibility

Role: `tree`

| Keys | Action |
| --- | --- |
| `Tab` | Enters the tree at the focused or selected item, then leaves it. |
| `ArrowDown, ArrowUp` | Moves to the next or previous visible item, skipping disabled items. With Shift in multiple mode, extends the selection. |
| `ArrowRight` | Opens a closed branch, or moves to the first child of an open one. Reversed in right-to-left pages. |
| `ArrowLeft` | Closes an open branch, or moves to the parent. Reversed in right-to-left pages. |
| `Home, End` | Moves to the first or last visible item. |
| `Space` | Selects the item; in multiple mode or with check boxes, toggles it. |
| `Enter` | Selects the item and runs onAction, such as opening a file. |
| `*` | Opens every branch at the same level as the focused item. |
| `Ctrl+A, Cmd+A` | Selects every visible item in multiple mode. |
| `Letters` | Moves to the next item whose name starts with the typed letters. |

- A tree requires aria-label or aria-labelledby.
- Rows are flat siblings with aria-level, aria-setsize and aria-posinset, so the focus ring and the selected fill cover one row.
- A branch has aria-expanded. Selection uses aria-selected, and the tree sets aria-multiselectable in multiple mode.
- With checkboxes, rows use aria-checked instead: true, false or mixed for a branch with some items checked.
- Only the focused item has tabIndex 0. Clicking the chevron opens or closes a branch without selecting it.

## Install

```bash
npx werewolf-ui add https://www.werewolf-ui.dev/r/tree.json
```

Run `npx werewolf-ui init` once in your app before adding components. Init sets up tokens, their CSS import and agent rules. Add installs this component and its dependencies while preserving your theme. See [Installation](https://www.werewolf-ui.dev/docs/installation.md) for requirements and the alternative registry setup.
