# Tree

Nested items people open and close, like folders, categories or permissions.

```tsx
import { Tree } from '@halo-ds/react';
```

## When to use it
Use a Tree on its own for browsing, like a file list, or for picking several with checkboxes, like permissions for a role. Use Tree select to pick from nested options in a form, like a team inside a department.

## Do
- Open the branch that holds the current or chosen item.
- Use icons to tell folders from files.
- Use checkboxes when people pick several; let branches pick everything inside.
- Turn on search in Tree select when the tree is long.

## Don't
- Nest deeper than four levels; split it into pages instead.
- Use a Tree for a flat list.
- Hide actions people need inside closed branches.
- Mix checkboxes and single picking in one tree.

## Props
### Tree props
| Prop | Type | What it does |
| --- | --- | --- |
| `nodes` (required) | `TreeNode[]` |  |
| `expanded` | `string[] \| undefined` | Ids of the open branches. |
| `defaultExpanded` | `string[] \| undefined` |  |
| `onExpandedChange` | `((expanded: string[]) => void) \| undefined` |  |
| `selected` | `string \| null \| undefined` | The chosen row, when the tree has no checkboxes. |
| `defaultSelected` | `string \| null \| undefined` |  |
| `onSelectedChange` | `((id: string) => void) \| undefined` |  |
| `checkable` | `boolean \| undefined` | Shows checkboxes. Checking a branch checks everything inside it. Figma: Checkbox. |
| `checked` | `string[] \| undefined` | Ids of the checked leaves (rows with no children). |
| `defaultChecked` | `string[] \| undefined` |  |
| `onCheckedChange` | `((checked: string[]) => void) \| undefined` |  |
| `size` | `"sm" \| "md" \| undefined` | 32 or 40px rows. Figma: Size. |
| `aria-label` | `string \| undefined` | Screen reader name, like "Folders". |
| `classNames` | `Partial<Record<TreePart, string>> \| undefined` | Extra class names for the parts inside the component, by part name, like `{ header: 'my-header' }`. |
| `styles` | `Partial<Record<TreePart, CSSProperties>> \| undefined` | Inline styles for the parts inside the component, by part name, like `{ footer: { gap: 4 } }`. |
It also takes the usual HTML attributes for its element.

## Examples
### Overview
```tsx
import { Tree } from '@halo-ds/react';
import { File, FilePdf, Folder } from '@phosphor-icons/react';

const files = [
  {
    id: 'brand',
    label: 'Brand',
    icon: <Folder />,
    children: [
      { id: 'logos', label: 'Logos', icon: <Folder />, children: [{ id: 'logo', label: 'Logo.svg', icon: <File /> }] },
      { id: 'guide', label: 'Guidelines.pdf', icon: <FilePdf /> },
    ],
  },
  { id: 'notes', label: 'Notes.txt', icon: <File /> },
];

export default function Example() {
  return (
    <div style={{ width: 300 }}>
      <Tree nodes={files} defaultExpanded={['brand']} defaultSelected="guide" aria-label="Files" />
    </div>
  );
}
```

### Checkable
Checkboxes for picking several, like permissions for a role. Checking a parent checks everything inside.
```tsx
import { useState } from 'react';
import { Tree } from '@halo-ds/react';

const permissions = [
  { id: 'projects', label: 'Projects', children: [{ id: 'projects-view', label: 'View projects' }, { id: 'projects-edit', label: 'Edit projects' }] },
  { id: 'billing', label: 'Billing', children: [{ id: 'billing-view', label: 'View invoices' }, { id: 'billing-pay', label: 'Pay invoices' }] },
];

export default function Demo() {
  const [checked, setChecked] = useState<string[]>(['projects-view']);
  return (
    <div style={{ width: 300 }}>
      <Tree nodes={permissions} checkable checked={checked} onCheckedChange={setChecked} defaultExpanded={['projects', 'billing']} aria-label="Permissions" />
    </div>
  );
}
```

### Small
Small rows, for dense side panels.
```tsx
import { Tree } from '@halo-ds/react';

const files = [
  { id: 'brand', label: 'Brand', children: [{ id: 'logo', label: 'Logo.svg' }, { id: 'colours', label: 'Colours.pdf' }] },
  { id: 'notes', label: 'Notes.txt' },
];

export default function Demo() {
  return (
    <div style={{ width: 260 }}>
      <Tree size="sm" nodes={files} defaultExpanded={['brand']} aria-label="Files" />
    </div>
  );
}
```

More: https://halodesignsystem.dev/components/tree
