

<InteractiveDemo
  name="tree"
  controls="[
  {
    type: 'enum',
    name: 'selectionMode',
    options: ['none', 'single', 'multiple'],
    defaultValue: 'multiple',
  },
  {
    type: 'enum',
    name: 'selectionBehavior',
    options: ['toggle', 'replace'],
    defaultValue: 'toggle',
  },
]"
/>

## Installation [#installation]

<CodeBlockTabs defaultValue="npm">
  <CodeBlockTabsList>
    <CodeBlockTabsTrigger value="npm">
      npm
    </CodeBlockTabsTrigger>

    <CodeBlockTabsTrigger value="pnpm">
      pnpm
    </CodeBlockTabsTrigger>

    <CodeBlockTabsTrigger value="yarn">
      yarn
    </CodeBlockTabsTrigger>

    <CodeBlockTabsTrigger value="bun">
      bun
    </CodeBlockTabsTrigger>
  </CodeBlockTabsList>

  <CodeBlockTab value="npm">
    ```bash
    npx shadcn@latest add @dotui/tree
    ```
  </CodeBlockTab>

  <CodeBlockTab value="pnpm">
    ```bash
    pnpm dlx shadcn@latest add @dotui/tree
    ```
  </CodeBlockTab>

  <CodeBlockTab value="yarn">
    ```bash
    yarn dlx shadcn@latest add @dotui/tree
    ```
  </CodeBlockTab>

  <CodeBlockTab value="bun">
    ```bash
    bun x shadcn@latest add @dotui/tree
    ```
  </CodeBlockTab>
</CodeBlockTabs>

## Usage [#usage]

Use a tree to display nested, hierarchical data such as a file system or
category navigation. Each row is a `TreeItem` whose `TreeItemContent` holds the
label; nested `TreeItem`s become its children.

```tsx
import { Tree, TreeItem, TreeItemContent } from '@/components/ui/tree'
```

```tsx
<Tree
  aria-label="Files"
  selectionMode="single"
  defaultExpandedKeys={['documents']}
>
  <TreeItem id="documents" textValue="Documents">
    <TreeItemContent>Documents</TreeItemContent>
    <TreeItem id="report" textValue="Weekly report">
      <TreeItemContent>Weekly report</TreeItemContent>
    </TreeItem>
  </TreeItem>
</Tree>
```

When rendering data dynamically, pass `items` to the `Tree` and a `Collection`
to each `TreeItem` for its children.

```tsx
import { Collection } from 'react-aria-components'
```

```tsx
<Tree aria-label="Files" items={items}>
  {function renderItem(item) {
    return (
      <TreeItem textValue={item.name}>
        <TreeItemContent>{item.name}</TreeItemContent>
        {item.children && (
          <Collection items={item.children}>{renderItem}</Collection>
        )}
      </TreeItem>
    )
  }}
</Tree>
```

## Examples [#examples]

<Examples className="md:grid-cols-2">
  <Example name="tree/demos/basic" title="Basic" />

  <Example name="tree/demos/selection" title="Multiple selection" />

  <Example name="tree/demos/with-icons" title="With icons" />

  <Example name="tree/demos/dynamic" title="Dynamic items" />

  <Example name="tree/demos/default-expanded" title="Default expanded" />

  <Example name="tree/demos/disabled" title="Disabled items" />

  <Example name="tree/demos/drag-and-drop" title="Drag and drop" />

  <Example name="tree/demos/empty" title="Empty state" />
</Examples>

## API Reference [#api-reference]

### Tree [#tree]

<Reference name="tree" />

### TreeItem [#treeitem]

<Reference name="tree-item" />

### TreeItemContent [#treeitemcontent]

<Reference name="tree-item-content" />
