# Tree

A hierarchy to browse: folders and files, categories, with nodes that open and close.

- Family: collection
- Import: `import { Tree } from "@senvenli/react/collection";`
- Page: https://design.senvenli.com/docs/components/tree
- JSON: https://design.senvenli.com/docs/components/tree.json

## Example: Files and folders

Arrows move, Right opens and Left closes a folder.

```tsx
import { Tree } from "@senvenli/react/collection";

export default function Example() {
  return (
    <Tree
      aria-label="Files"
      defaultExpandedKeys={["src"]}
      items={[
        {
          id: "src",
          label: "src",
          icon: "folder",
          children: [
            { id: "components", label: "components", icon: "folder", children: [{ id: "button", label: "button.tsx", icon: "file-code" }] },
            { id: "index", label: "index.ts", icon: "file-code" },
          ],
        },
        { id: "readme", label: "README.md", icon: "file-text" },
      ]}
    />
  );
}
```

## Guidelines

### When to use it

- Data with parents and children people browse: files, categories, an org chart.

### Use something else when

- [grid-list](https://design.senvenli.com/docs/components/grid-list.md): The items have no hierarchy.
- [sidebar](https://design.senvenli.com/docs/components/sidebar.md): It is the app's navigation.
- [accordion](https://design.senvenli.com/docs/components/accordion.md): A few sections of content, not data.

### Do and avoid

- Keep labels short; three or four levels at most.

## Accessibility

Built in:

- Arrows move, Right opens and Left closes a node, Home and End jump; the chevron and Enter expand.

You provide:

- An aria-label naming the tree.

## API reference

### Tree

| Prop | Type | Default | Required | Description |
| --- | --- | --- | --- | --- |
| aria-label | `string` |  | yes | Names the tree for screen readers ("Files", "Categories"). |
| items | `readonly TreeNode[]` |  | yes |  |
| selectionMode | `"none" \| "single" \| "multiple"` | `"none"` | no | `none` (default) for navigation with `onAction`, `single` or `multiple` to pick nodes. |
| selectedKeys | `Iterable<Key>` |  | no |  |
| defaultSelectedKeys | `Iterable<Key>` |  | no |  |
| onSelectionChange | `(keys: Selection) => void` |  | no |  |
| expandedKeys | `Iterable<Key>` |  | no |  |
| defaultExpandedKeys | `Iterable<Key>` |  | no |  |
| onExpandedChange | `(keys: Set<Key>) => void` |  | no |  |
| onAction | `(key: Key) => void` |  | no | What pressing a node does (open the file, go to the category). |

### Tokens it reads

`--ds-background-neutral-hovered`, `--ds-background-neutral-subtle-hovered`, `--ds-background-selected`, `--ds-border-focused`, `--ds-icon-subtle`, `--ds-radius-small`, `--ds-space-025`, `--ds-space-075`, `--ds-space-100`, `--ds-text`, `--ds-text-disabled`

## Related

- [grid-list](https://design.senvenli.com/docs/components/grid-list.md)
- [sidebar](https://design.senvenli.com/docs/components/sidebar.md)
- [accordion](https://design.senvenli.com/docs/components/accordion.md)
