# Tree

Expandable/collapsible tree view



<ComponentPreview base="ink" name="tree-demo" />

Installation [#installation]

<CodeTabs>
  <TabsList>
    <TabsTrigger value="cli">
      Command
    </TabsTrigger>

    <TabsTrigger value="manual">
      Manual
    </TabsTrigger>
  </TabsList>

  <TabsContent value="cli">
    ```bash
    npx shadcn@latest add @termcn/tree
    ```
  </TabsContent>

  <TabsContent value="manual">
    <Steps>
      <Step>
        Copy and paste the following code into your project.
      </Step>

      <ComponentSource base="ink" name="tree" title="components/ui/tree.tsx" />

      <Step>
        Update the import paths to match your project setup.
      </Step>
    </Steps>
  </TabsContent>
</CodeTabs>

Usage [#usage]

```tsx
import { Tree } from "@/components/ui/tree";
```

```tsx
<Tree
  defaultExpanded={["src"]}
  nodes={[
    {
      key: "src",
      label: "src",
      children: [
        { key: "index", label: "index.ts" },
        { key: "utils", label: "utils.ts" },
      ],
    },
    { key: "pkg", label: "package.json" },
  ]}
/>
```

API Reference [#api-reference]

Tree [#tree]

| Prop              | Type                       | Default    |
| ----------------- | -------------------------- | ---------- |
| `nodes`           | `TreeNode[]`               | `required` |
| `onSelect`        | `(node: TreeNode) => void` | -          |
| `defaultExpanded` | `string[]`                 | `[]`       |
| `expandedIcon`    | `string`                   | `"▼"`      |
| `collapsedIcon`   | `string`                   | `"▶"`      |
| `leafIcon`        | `string`                   | `"•"`      |

TreeNode [#treenode]

| Prop       | Type         | Default    |
| ---------- | ------------ | ---------- |
| `key`      | `string`     | `required` |
| `label`    | `string`     | `required` |
| `children` | `TreeNode[]` | -          |
| `icon`     | `string`     | -          |
