# Tree View

A hierarchy of rows that open and close, such as files and folders.

A hierarchy of rows that open and close: files and folders, nested settings, an outline. Rows are the sidebar nav's (32px at size md, 14px/500 ink-2, control radius, the one gliding hover highlight, the current row filled hover-2 in ink); a branch carries the collapsible's caret that turns down and opens with the collapsible's reveal; each level is indented by the caret's box and gap (26px at md) with a hairline guide under its parent's caret, drawn in ink-3 for the level that holds the selection. A row's fill, focus ring and hover highlight start where its level starts, so no guide is ever under them, while the whole row stays clickable. size puts the rows on the control scale (xs 24 for the dense side pane of a code editor's explorer, sm 28, md 32, lg 36) and changes dimensions only (the radius steps with the height: chip up to 28px, control from 32). Behaviour and ARIA from the Zag tree-view machine through Ark UI.

Kind: component · Category: Navigation · Status: beta · Targets: React, Vue, Svelte · Behaviour: Zag `tree-view` machine, through Ark UI

## Add

```bash
npx fndn add tree-view
```

It writes the folder `tree-view/` into the project's ui folder (`index.ts`, `tree-view.tsx`, `TreeView.vue`, `TreeView.svelte`, `tree-view.recipe.ts`), and what it is built from: [Collapsible](/components/collapsible.md). First time: [Installation](/installation.md).

## Usage

```tsx
import { TreeView } from "@/components/ui/tree-view";
```

```vue
import { TreeView } from "@/components/ui/tree-view";
```

```svelte
import { TreeView } from "$lib/components/ui/tree-view";
```

## Parts

In the DOM the root carries `data-slot="tree-view"` and each part `data-slot="tree-view-<part>"`; in the recipe the root is the `base` slot and each part a slot named after it in camelCase.

| Part | Element | data-slot | Flags | Notes |
| --- | --- | --- | --- | --- |
| `root` | `div` | `tree-view` |  | Ark's tree-view root: a column, no surface of its own. |
| `label` | `h3` | `tree-view-label` | optional | The tree's name above it (Ark's label; the tree is labelled by it): 28px, 12px/500 ink-2 (information, quieter than the rows by size), as the collapsible's section header; it starts where the top level's carets are drawn. Without a label, pass aria-label. |
| `tree` | `div` | `tree-view-tree` |  | role=tree; the offset parent of the highlight (relative, isolated), rows 1px apart. |
| `highlight` | `div` | `tree-view-highlight` |  | The gliding hover highlight (aria-hidden), as the sidebar nav's: it follows the pointer and keyboard focus between rows, shown only under a fine pointer; rows sit above it. It takes the shape of the row's fill: it starts where the row's level starts and narrows or widens as it glides between levels. |
| `branch` | `div` | `tree-view-branch` |  | A node with children (role=treeitem, aria-expanded, aria-level; data-state=open\|closed): its control, then its content. |
| `branch-control` | `div` | `tree-view-branch-control` |  | A branch's row (focusable, roving tabindex): the row's look; the caret first. A click opens or closes it and selects it. |
| `item` | `div` | `tree-view-item` |  | A leaf's row (role=treeitem, aria-selected, aria-level, roving tabindex). At md: 32px minimum, padding 6px 8px with the inline start indented 26px per level (a leaf adds one level, so its label lines up with a sibling branch's), gap 6, 14px/500 ink-2 on 20px lines. xs: 24px, padding 4px 6px, 12px text on 16px lines, a 16px caret / icon box, gap 4 (20px a level). sm: 28px, padding 4px 8px, 13px text. lg: 36px, padding 8px 8px. 40px minimum on a coarse pointer at every size. The fill and the focus ring are a ::before from where the row's level starts (26px a level at md; 0 at the top) to the row's end, 8px clear of the guide beside it (6px at xs); the row itself, the hit area, spans the whole width. Radius chip at xs and sm, control at md and lg (the highlight's too). Hover: the glide (pointer cursor; touch-action manipulation). Press: hover-2. Selected: hover-2 fill, ink text. Focus ring inside the fill. Disabled: half opacity, no pointer events. |
| `caret` | `div` | `tree-view-caret` |  | The collapsible's caret in the row's box (20px; 16px at xs; the glyph 14px at md and lg, 13 at sm, 12 at xs): right caret (ph caret-right) in ink-3 that turns down 90deg while the branch is open, mirrored right-to-left; aria-hidden; kbd-instant. |
| `icon` | `span` | `tree-view-icon` | optional | A node's icon (React: a node; Vue and Svelte: an icon component), in the row's box on the first line: 20px box, 18px glyph at md and lg (as the sidebar nav's item icon), 16px at sm; a 16px box, 14px glyph at xs. |
| `text` | `span` | `tree-view-text` |  | The node's name: wraps, never cut (a long name grows its row); the first line shares the caret's line. Author text keeps its own direction. |
| `content` | `div` | `tree-view-content` |  | role=group: the branch's children, in the collapsible's region (grid rows 0fr to 1fr). Closed: hidden, out of the layout once its exit has played. |
| `body` | `div` | `tree-view-body` |  | The one child of the region; it clips while the region moves and holds the guide and the child rows (1px apart). |
| `guide` | `div` | `tree-view-guide` |  | Ark's indent guide: a hairline (a 1px border, one device pixel at any scaling, color.line-strong) down the children, under the parent's caret, from the first child to half a row padding short of the last. It lies in the parent level's indent, where no fill, highlight or ring reaches, so a level is always one unbroken line; it takes no pointer. The level whose rows include a selected one draws its guide in ink-3 (the active guide). |

## Props

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `items` | `array` |  | The top-level nodes: { value, label, children?, disabled?, icon? } (icon is a node, React). A node with children (even none) is a branch. |
| `expandedValue` | `array` | `[]` | Bindable (React: `defaultExpandedValue` and `onExpandedValueChange`; Vue: `v-model:expandedValue`; Svelte: `bind:expandedValue`). The values of the open branches. React: expandedValue / defaultExpandedValue / onExpandedValueChange. |
| `selectedValue` | `array` | `[]` | Bindable (React: `defaultSelectedValue` and `onSelectedValueChange`; Vue: `v-model:selectedValue`; Svelte: `bind:selectedValue`). The values of the selected nodes. React: selectedValue / defaultSelectedValue / onSelectedValueChange. |
| `selectionMode` | `"single" \| "multiple"` | `"single"` | multiple: Ctrl / Cmd + click and Shift + arrows add to the selection. |
| `label` | `string` |  | The tree's name, shown above it. |
| `size` | `"xs" \| "sm" \| "md" \| "lg"` | `"md"` | Row height on the control scale: xs 24 (12px text; the dense rows of a side pane, a code editor's explorer), sm 28 (13px), md 32 (14px), lg 36 (14px). Dimensions only: padding, text, caret and icon box, indent; never the look. |

Other attributes and `class` go to the root; `class` is merged through the recipe.

## Events

| Event | Payload |
| --- | --- |
| `expandedValueChange` | (value: string[]) the open branches, after a click or a key |
| `selectedValueChange` | (value: string[]) the selected nodes, after a click or a key |

## States

`hover`, `press`, `focus-visible`, `open`, `selected`, `disabled`

## Keyboard and accessibility

Role: tree (treeitem, group).

Keyboard:

- Tab reaches the tree once (the focused or first row); Tab again leaves it
- ArrowDown / ArrowUp move to the next / previous visible row; Home / End to the first / last
- ArrowRight opens a closed branch, or moves to its first child; ArrowLeft closes an open branch, or moves to the parent
- Enter / Space select the focused row (and open or close a branch); * opens every sibling branch
- Typing a letter moves to the next row whose name starts with it
- Opening and closing from the keyboard is instant (no reveal, no caret turn); the highlight still glides

Notes:

- Every row carries aria-level; a branch carries aria-expanded; a selected row aria-selected=true.
- A closed branch's rows leave the layout and the accessibility tree once the exit has played.

## Motion

- branch: the collapsible's reveal (grid-template-rows 0fr -> 1fr over duration.reveal easing.out-strong, opacity over duration.base) and its held exit
- caret: rotate 0 -> 90deg (right-to-left 180 -> 90deg) over duration.move easing.out-strong
- highlight: the shared glide, under the pointer and the keyboard alike (transform and height over duration.move); between levels its inline offset (translate) and width move with it on the same timing; a first appearance lands on the row's level without travelling
- row fills (selected, press) and the active guide's colour over duration.fast; instant under the keyboard
- keyboard: region and caret are instant (kbd-instant)
- first paint: nothing plays (branches open at mount are shown open)

## Tokens

`color.line-strong`, `color.ink`, `color.ink-2`, `color.ink-3`, `color.hover`, `color.hover-2`, `color.accent`, `radius.control`, `radius.chip`, `text.xs`, `text.sm`, `text.md`, `duration.fast`, `duration.base`, `duration.move`, `duration.reveal`, `easing.out-strong`

## Dependencies

- [Collapsible](/components/collapsible.md)

## Examples

### Files of a repository

React · `repository-files.tsx`

```tsx
import { File, Folder } from "@phosphor-icons/react";
import { TreeView, type TreeViewNode } from "@/components/ui/tree-view";

const folder = <Folder />;
const file = <File />;

const items: TreeViewNode[] = [
  {
    value: "src", label: "src", icon: folder,
    children: [
      {
        value: "src/webhooks", label: "webhooks", icon: folder,
        children: [
          { value: "src/webhooks/deliver.ts", label: "deliver.ts", icon: file },
          { value: "src/webhooks/retry-policy.ts", label: "retry-policy.ts", icon: file },
        ],
      },
      {
        value: "src/invoices", label: "invoices", icon: folder,
        children: [{ value: "src/invoices/render-pdf.ts", label: "render-pdf.ts", icon: file }],
      },
    ],
  },
  { value: "package.json", label: "package.json", icon: file },
];

export default function RepositoryFiles() {
  return (
    <TreeView
      className="w-64"
      aria-label="billing-api files"
      defaultExpandedValue={["src"]}
      defaultSelectedValue={["src/webhooks"]}
      items={items}
    />
  );
}
```

Vue · `repository-files.vue`

```vue
<script setup lang="ts">
import { PhFile, PhFolder } from "@phosphor-icons/vue";
import { TreeView, type TreeViewNode } from "@/components/ui/tree-view";

const folder = PhFolder;
const file = PhFile;

const items: TreeViewNode[] = [
  {
    value: "src", label: "src", icon: folder,
    children: [
      {
        value: "src/webhooks", label: "webhooks", icon: folder,
        children: [
          { value: "src/webhooks/deliver.ts", label: "deliver.ts", icon: file },
          { value: "src/webhooks/retry-policy.ts", label: "retry-policy.ts", icon: file },
        ],
      },
      {
        value: "src/invoices", label: "invoices", icon: folder,
        children: [{ value: "src/invoices/render-pdf.ts", label: "render-pdf.ts", icon: file }],
      },
    ],
  },
  { value: "package.json", label: "package.json", icon: file },
];
</script>

<template>
  <TreeView
    class="w-64"
    aria-label="billing-api files"
    :defaultExpandedValue="['src']"
    :defaultSelectedValue="['src/webhooks']"
    :items="items"
  />
</template>
```

Svelte · `repository-files.svelte`

```svelte
<script lang="ts">
  import File from "phosphor-svelte/lib/FileIcon";
  import Folder from "phosphor-svelte/lib/FolderIcon";
  import { TreeView, type TreeViewNode } from "$lib/components/ui/tree-view";

  const folder = Folder;
  const file = File;

  const items: TreeViewNode[] = [
    {
      value: "src", label: "src", icon: folder,
      children: [
        {
          value: "src/webhooks", label: "webhooks", icon: folder,
          children: [
            { value: "src/webhooks/deliver.ts", label: "deliver.ts", icon: file },
            { value: "src/webhooks/retry-policy.ts", label: "retry-policy.ts", icon: file },
          ],
        },
        {
          value: "src/invoices", label: "invoices", icon: folder,
          children: [{ value: "src/invoices/render-pdf.ts", label: "render-pdf.ts", icon: file }],
        },
      ],
    },
    { value: "package.json", label: "package.json", icon: file },
  ];
</script>

<TreeView
  class="w-64"
  aria-label="billing-api files"
  defaultExpandedValue={["src"]}
  defaultSelectedValue={["src/webhooks"]}
  {items}
/>
```

### Dense explorer in an editor's side pane

React · `explorer-pane.tsx`

```tsx
import { File, FileText, Folder } from "@phosphor-icons/react";
import { TreeView, type TreeViewNode } from "@/components/ui/tree-view";

/* xs: the dense 24px rows of a code editor's side pane; sm 28 sits between, md 32 is the default */
const folder = <Folder />;
const file = <File />;
const doc = <FileText />;

const items: TreeViewNode[] = [
  {
    value: "src", label: "src", icon: folder,
    children: [
      {
        value: "src/webhooks", label: "webhooks", icon: folder,
        children: [
          { value: "src/webhooks/deliver.ts", label: "deliver.ts", icon: file },
          { value: "src/webhooks/retry-policy.ts", label: "retry-policy.ts", icon: file },
          { value: "src/webhooks/signature.ts", label: "signature.ts", icon: file },
        ],
      },
      { value: "src/invoices", label: "invoices", icon: folder, children: [{ value: "src/invoices/render-pdf.ts", label: "render-pdf.ts", icon: file }] },
      { value: "src/index.ts", label: "index.ts", icon: file },
    ],
  },
  { value: "tests", label: "tests", icon: folder, children: [{ value: "tests/deliver.test.ts", label: "deliver.test.ts", icon: file }] },
  { value: "package.json", label: "package.json", icon: file },
  { value: "README.md", label: "README.md", icon: doc },
];

export default function ExplorerPane() {
  return (
    <TreeView
      className="w-56"
      size="xs"
      label="billing-api"
      defaultExpandedValue={["src", "src/webhooks"]}
      defaultSelectedValue={["src/webhooks/retry-policy.ts"]}
      items={items}
    />
  );
}
```

Vue · `explorer-pane.vue`

```vue
<script setup lang="ts">
import { PhFile, PhFileText, PhFolder } from "@phosphor-icons/vue";
import { TreeView, type TreeViewNode } from "@/components/ui/tree-view";

/* xs: the dense 24px rows of a code editor's side pane; sm 28 sits between, md 32 is the default */
const folder = PhFolder;
const file = PhFile;
const doc = PhFileText;

const items: TreeViewNode[] = [
  {
    value: "src", label: "src", icon: folder,
    children: [
      {
        value: "src/webhooks", label: "webhooks", icon: folder,
        children: [
          { value: "src/webhooks/deliver.ts", label: "deliver.ts", icon: file },
          { value: "src/webhooks/retry-policy.ts", label: "retry-policy.ts", icon: file },
          { value: "src/webhooks/signature.ts", label: "signature.ts", icon: file },
        ],
      },
      { value: "src/invoices", label: "invoices", icon: folder, children: [{ value: "src/invoices/render-pdf.ts", label: "render-pdf.ts", icon: file }] },
      { value: "src/index.ts", label: "index.ts", icon: file },
    ],
  },
  { value: "tests", label: "tests", icon: folder, children: [{ value: "tests/deliver.test.ts", label: "deliver.test.ts", icon: file }] },
  { value: "package.json", label: "package.json", icon: file },
  { value: "README.md", label: "README.md", icon: doc },
];
</script>

<template>
  <TreeView
    class="w-56"
    size="xs"
    label="billing-api"
    :defaultExpandedValue="['src', 'src/webhooks']"
    :defaultSelectedValue="['src/webhooks/retry-policy.ts']"
    :items="items"
  />
</template>
```

Svelte · `explorer-pane.svelte`

```svelte
<script lang="ts">
  import File from "phosphor-svelte/lib/FileIcon";
  import FileText from "phosphor-svelte/lib/FileTextIcon";
  import Folder from "phosphor-svelte/lib/FolderIcon";
  import { TreeView, type TreeViewNode } from "$lib/components/ui/tree-view";

  /* xs: the dense 24px rows of a code editor's side pane; sm 28 sits between, md 32 is the default */
  const folder = Folder;
  const file = File;
  const doc = FileText;

  const items: TreeViewNode[] = [
    {
      value: "src", label: "src", icon: folder,
      children: [
        {
          value: "src/webhooks", label: "webhooks", icon: folder,
          children: [
            { value: "src/webhooks/deliver.ts", label: "deliver.ts", icon: file },
            { value: "src/webhooks/retry-policy.ts", label: "retry-policy.ts", icon: file },
            { value: "src/webhooks/signature.ts", label: "signature.ts", icon: file },
          ],
        },
        { value: "src/invoices", label: "invoices", icon: folder, children: [{ value: "src/invoices/render-pdf.ts", label: "render-pdf.ts", icon: file }] },
        { value: "src/index.ts", label: "index.ts", icon: file },
      ],
    },
    { value: "tests", label: "tests", icon: folder, children: [{ value: "tests/deliver.test.ts", label: "deliver.test.ts", icon: file }] },
    { value: "package.json", label: "package.json", icon: file },
    { value: "README.md", label: "README.md", icon: doc },
  ];
</script>

<TreeView
  class="w-56"
  size="xs"
  label="billing-api"
  defaultExpandedValue={["src", "src/webhooks"]}
  defaultSelectedValue={["src/webhooks/retry-policy.ts"]}
  {items}
/>
```

### Settings pages beside their content

React · `settings-pages.tsx`

```tsx
import { useState } from "react";
import { TreeView, type TreeViewNode } from "@/components/ui/tree-view";

const items: TreeViewNode[] = [
  { value: "general", label: "General" },
  {
    value: "members", label: "Members",
    children: [
      { value: "people", label: "People" },
      { value: "roles", label: "Roles" },
      { value: "invitations", label: "Invitations" },
    ],
  },
  { value: "billing", label: "Billing" },
  { value: "api-keys", label: "API keys" },
];

const pages: Record<string, string> = {
  general: "Workspace name, URL and default region.",
  people: "7 members. Maya Chen is the owner.",
  roles: "Owner, Admin, Developer and Viewer.",
  invitations: "2 invitations waiting, sent Nov 14, 2026.",
  billing: "Team plan, $1,240.00 a month.",
  "api-keys": "3 keys. nw_live_… was last used 2 min ago.",
};

/* the selection drives the pane: controlled, with a branch open from the start */
export default function SettingsPages() {
  const [selected, setSelected] = useState(["roles"]);
  const [expanded, setExpanded] = useState(["members"]);
  const current = selected[0] ?? "general";
  return (
    <div className="flex w-[480px] max-w-full items-start gap-6">
      <TreeView
        className="w-44 shrink-0"
        aria-label="Workspace settings"
        items={items}
        selectedValue={selected}
        onSelectedValueChange={setSelected}
        expandedValue={expanded}
        onExpandedValueChange={setExpanded}
      />
      <p className="m-0 pt-1.5 text-sm text-ink-2">{pages[current] ?? "Pick a page on the left."}</p>
    </div>
  );
}
```

Vue · `settings-pages.vue`

```vue
<script setup lang="ts">
import { computed, ref } from "vue";
import { TreeView, type TreeViewNode } from "@/components/ui/tree-view";

const items: TreeViewNode[] = [
  { value: "general", label: "General" },
  {
    value: "members", label: "Members",
    children: [
      { value: "people", label: "People" },
      { value: "roles", label: "Roles" },
      { value: "invitations", label: "Invitations" },
    ],
  },
  { value: "billing", label: "Billing" },
  { value: "api-keys", label: "API keys" },
];

const pages: Record<string, string> = {
  general: "Workspace name, URL and default region.",
  people: "7 members. Maya Chen is the owner.",
  roles: "Owner, Admin, Developer and Viewer.",
  invitations: "2 invitations waiting, sent Nov 14, 2026.",
  billing: "Team plan, $1,240.00 a month.",
  "api-keys": "3 keys. nw_live_… was last used 2 min ago.",
};

/* the selection drives the pane: controlled, with a branch open from the start */
const selected = ref(["roles"]);
const expanded = ref(["members"]);
const current = computed(() => selected.value[0] ?? "general");
</script>

<template>
  <div class="flex w-[480px] max-w-full items-start gap-6">
    <TreeView
      v-model:selectedValue="selected"
      v-model:expandedValue="expanded"
      class="w-44 shrink-0"
      aria-label="Workspace settings"
      :items="items"
    />
    <p class="m-0 pt-1.5 text-sm text-ink-2">{{ pages[current] ?? "Pick a page on the left." }}</p>
  </div>
</template>
```

Svelte · `settings-pages.svelte`

```svelte
<script lang="ts">
  import { TreeView, type TreeViewNode } from "$lib/components/ui/tree-view";

  const items: TreeViewNode[] = [
    { value: "general", label: "General" },
    {
      value: "members", label: "Members",
      children: [
        { value: "people", label: "People" },
        { value: "roles", label: "Roles" },
        { value: "invitations", label: "Invitations" },
      ],
    },
    { value: "billing", label: "Billing" },
    { value: "api-keys", label: "API keys" },
  ];

  const pages: Record<string, string> = {
    general: "Workspace name, URL and default region.",
    people: "7 members. Maya Chen is the owner.",
    roles: "Owner, Admin, Developer and Viewer.",
    invitations: "2 invitations waiting, sent Nov 14, 2026.",
    billing: "Team plan, $1,240.00 a month.",
    "api-keys": "3 keys. nw_live_… was last used 2 min ago.",
  };

  /* the selection drives the pane: controlled, with a branch open from the start */
  let selected = $state(["roles"]);
  let expanded = $state(["members"]);
  const current = $derived(selected[0] ?? "general");
</script>

<div class="flex w-[480px] max-w-full items-start gap-6">
  <TreeView
    bind:selectedValue={selected}
    bind:expandedValue={expanded}
    class="w-44 shrink-0"
    aria-label="Workspace settings"
    {items}
  />
  <p class="m-0 pt-1.5 text-sm text-ink-2">{pages[current] ?? "Pick a page on the left."}</p>
</div>
```

### Pick several environments, one frozen

React · `redeploy-targets.tsx`

```tsx
import { TreeView, type TreeViewNode } from "@/components/ui/tree-view";

/* Ctrl / Cmd + click or Shift + arrows add to the selection; a locked environment is disabled */
const items: TreeViewNode[] = [
  {
    value: "web-app", label: "web-app",
    children: [
      { value: "web-app/production", label: "Production" },
      { value: "web-app/staging", label: "Staging" },
    ],
  },
  {
    value: "billing-api", label: "billing-api",
    children: [
      { value: "billing-api/production", label: "Production (frozen until Nov 20)", disabled: true },
      { value: "billing-api/staging", label: "Staging" },
    ],
  },
];

export default function RedeployTargets() {
  return (
    <TreeView
      className="w-72"
      label="Redeploy to"
      selectionMode="multiple"
      defaultExpandedValue={["web-app", "billing-api"]}
      defaultSelectedValue={["web-app/staging", "billing-api/staging"]}
      items={items}
    />
  );
}
```

Vue · `redeploy-targets.vue`

```vue
<script setup lang="ts">
import { TreeView, type TreeViewNode } from "@/components/ui/tree-view";

/* Ctrl / Cmd + click or Shift + arrows add to the selection; a locked environment is disabled */
const items: TreeViewNode[] = [
  {
    value: "web-app", label: "web-app",
    children: [
      { value: "web-app/production", label: "Production" },
      { value: "web-app/staging", label: "Staging" },
    ],
  },
  {
    value: "billing-api", label: "billing-api",
    children: [
      { value: "billing-api/production", label: "Production (frozen until Nov 20)", disabled: true },
      { value: "billing-api/staging", label: "Staging" },
    ],
  },
];
</script>

<template>
  <TreeView
    class="w-72"
    label="Redeploy to"
    selectionMode="multiple"
    :defaultExpandedValue="['web-app', 'billing-api']"
    :defaultSelectedValue="['web-app/staging', 'billing-api/staging']"
    :items="items"
  />
</template>
```

Svelte · `redeploy-targets.svelte`

```svelte
<script lang="ts">
  import { TreeView, type TreeViewNode } from "$lib/components/ui/tree-view";

  /* Ctrl / Cmd + click or Shift + arrows add to the selection; a locked environment is disabled */
  const items: TreeViewNode[] = [
    {
      value: "web-app", label: "web-app",
      children: [
        { value: "web-app/production", label: "Production" },
        { value: "web-app/staging", label: "Staging" },
      ],
    },
    {
      value: "billing-api", label: "billing-api",
      children: [
        { value: "billing-api/production", label: "Production (frozen until Nov 20)", disabled: true },
        { value: "billing-api/staging", label: "Staging" },
      ],
    },
  ];
</script>

<TreeView
  class="w-72"
  label="Redeploy to"
  selectionMode="multiple"
  defaultExpandedValue={["web-app", "billing-api"]}
  defaultSelectedValue={["web-app/staging", "billing-api/staging"]}
  {items}
/>
```

## Recipe

`tree-view.recipe.ts`:

```ts
import { tv, type VariantProps } from "../lib/tv";
import { glideLayer } from "../lib/glide";
import { collapsible } from "../collapsible/collapsible.recipe";

/* tree-view — a hierarchy of rows that open and close (Zag tree-view through Ark UI).
 *
 * Rows are the sidebar nav's (sidebar-nav.recipe.ts item): 14px/500 ink-2, gap 6, control radius at md, the
 * one gliding hover highlight (glideLayer of lib/glide.ts, hover-gated, it follows pointer and focus), a press
 * that darkens to hover-2 and a current row filled hover-2 in ink, the fill and the focus ring on
 * a childless ::before. Two differences: the fill starts where the row's level starts, not at
 * the tree's edge (the row stays the hit area across the whole width; the highlight follows the
 * level, glideTree of tree-view.logic.ts), and a name wraps instead of being cut, so a row is
 * 32px at least (6px + 20px lines + 6px).
 *
 * A branch is a collapsible: Ark's branch is a collapsible root and its content the collapsible's
 * content, so the caret, the reveal, the held exit and the keyboard's instant change are the
 * collapsible recipe's (variant row) composed here, never copied.
 *
 * Indentation comes from the engine's --depth (1 at the top): 26px a level (the caret's 20px box
 * and the 6px gap), so a child's caret sits under its parent's icon or name; a leaf adds the
 * same 26px, so its name lines up with a sibling branch's. The guide is a hairline under the
 * parent's caret (8px padding + half the 20px box) in line-strong; the guide of the level holding
 * the selection is ink-3. It lies in the parent level's indent, 8px clear of its children's fills
 * (6px at xs), so no fill, highlight or ring ever covers or crosses it.
 * Parts are found by data-slot (tree-view, tree-view-item, tree-view-branch-control, …).
 *
 * size (the control scale) changes dimensions only, through
 * custom properties set on the root that every row, caret, icon, indent and guide reads (the
 * radius steps with the height, as every control's: chip up to 28px rows, control from 32px):
 *   xs  24px rows: 4 + 16px lines + 4, 12px text, a 16px caret / icon box, a 4px gap (20px a
 *       level), 6px inline padding: the dense rows of a side pane (a code editor's explorer)
 *   sm  28px rows: 4 + 20 + 4, 13px text, a 20px box (26px a level)
 *   md  32px rows (default): 6 + 20 + 6, 14px text, a 20px box (26px a level)
 *   lg  36px rows: 8 + 20 + 8, 14px text
 * A coarse pointer keeps its 40px minimum at every size. */

const row = collapsible({ variant: "row" });

const ROW = [
  "relative z-1 flex w-full min-w-0 items-start gap-[var(--tree-gap)] min-h-[calc(var(--tree-line)+2*var(--tree-py))] py-[var(--tree-py)] pe-[var(--tree-pad)]",
  "pointer-coarse:min-h-10 pointer-coarse:py-2.5",
  /* where the row's level starts: one level is the caret's box and the gap after it */
  "[--tree-level:calc((var(--depth,1)-1)*(var(--tree-box)+var(--tree-gap)))] ps-[calc(var(--tree-level)+var(--tree-pad))]",
  /* a row is pressed like the option rows: it invites the click, and on touch it acts at once
   * (base.css lists no treeitem among its controls). The text size is the root's (size). */
  "font-medium leading-[var(--tree-line)] text-ink-2 outline-none select-none cursor-pointer touch-manipulation [-webkit-touch-callout:none]",
  /* the fill and the focus ring are a childless ::before from where the row's level starts to the
   * end: the whole row stays the hit area, and no guide (each lies in a parent level's indent) is
   * ever under a fill or a ring */
  "before:content-[''] before:absolute before:inset-y-0 before:end-0 before:start-(--tree-level) before:-z-1 before:rounded-(--tree-radius)",
  "active:before:bg-hover-2 data-selected:before:bg-hover-2 data-selected:text-ink",
  "focus-visible:before:outline-2 focus-visible:before:outline-solid focus-visible:before:outline-accent focus-visible:before:-outline-offset-2",
  "data-disabled:opacity-50 data-disabled:pointer-events-none",
  "motion-colors-fast before:motion-colors-fast kbd:duration-0 kbd:before:duration-0",
];

export const treeView = tv({
  slots: {
    base: "flex w-full min-w-0 flex-col",
    /* the collapsible section header's type, not its press: a label is not a control. It starts
     * where the carets of the top level are drawn (the padding and the caret's inset in its box) */
    label: "flex items-center h-7 m-0 ps-[calc(var(--tree-pad)+(var(--tree-box)-var(--tree-caret))/2)] pe-[var(--tree-pad)] text-xs font-medium leading-none text-ink-2 [unicode-bidi:plaintext]",
    /* rows stack above the highlight; isolation keeps that inside, below portaled popups */
    tree: "relative isolate flex min-w-0 flex-col gap-px",
    /* glide() moves it down to the row; glideTree() (tree-view.logic.ts) gives it the row's level
     * (--tree-hl-depth), and it starts where the row's fill does: shifted and narrowed, on the
     * same timing as the move (translate is in motion-transform's list, width in motion-size's) */
    highlight: [
      glideLayer, "rounded-(--tree-radius) invisible [@media(hover:hover)_and_(pointer:fine)]:visible",
      "[--tree-hl-start:calc((var(--tree-hl-depth,1)-1)*(var(--tree-box)+var(--tree-gap)))] w-[calc(100%-var(--tree-hl-start))]",
      "ltr:translate-x-(--tree-hl-start) rtl:-translate-x-(--tree-hl-start) motion-size-move",
    ],
    branch: "flex min-w-0 flex-col",
    branchControl: ROW,
    /* a leaf adds one level: its name lines up with a sibling branch's */
    item: [ROW, "ps-[calc(var(--tree-level)+var(--tree-pad)+var(--tree-box)+var(--tree-gap))]"],
    caret: [
      row.caret(),
      "size-[var(--tree-box)] m-0 text-[length:var(--tree-caret)] [[data-slot=tree-view-branch-control][data-state=open]>&]:[transform:rotate(90deg)]",
    ],
    icon: "flex items-center justify-center shrink-0 size-[var(--tree-box)] text-[length:var(--tree-icon)]",
    text: "flex-1 min-w-0 wrap-anywhere [unicode-bidi:plaintext] [&:dir(ltr)]:text-left [&:dir(rtl)]:text-right",
    content: row.content(),
    /* the rows of a level, 1px apart like the top level; the guide's offset parent */
    body: [row.body(), "relative flex flex-col gap-px pt-px"],
    /* under the parent's caret: the padding, half the box, less half the line. A border, not a
     * 1px background: a border is drawn on whole device pixels, so every level has the same
     * weight at 125% or 150% scaling (a 1px box at a fractional position takes one device pixel
     * on one level and two on the next). It runs from the first child to half a row padding short
     * of the last, so a level ends visibly before the next row. It sits above the rows (none
     * paints where it lies) and takes no pointer. The level that holds a selected row draws its
     * guide in ink-3: the selection's folder shows at a glance. */
    guide: [
      "absolute top-0 bottom-[calc(var(--tree-py)/2)] z-2 w-0 border-s border-line-strong pointer-events-none start-[calc(var(--tree-pad)+var(--tree-box)/2-0.5px+(var(--depth,1)-1)*(var(--tree-box)+var(--tree-gap)))]",
      "[[data-slot=tree-view-body]:has(>[aria-selected=true])>&]:border-ink-3",
      "motion-colors-fast kbd:duration-0",
    ],
  },
  variants: {
    size: {
      xs: { base: "text-xs [--tree-line:16px] [--tree-py:4px] [--tree-box:16px] [--tree-gap:4px] [--tree-pad:6px] [--tree-caret:12px] [--tree-icon:14px] [--tree-radius:var(--fndn-radius-chip)]" },
      sm: { base: "text-sm [--tree-line:20px] [--tree-py:4px] [--tree-box:20px] [--tree-gap:6px] [--tree-pad:8px] [--tree-caret:13px] [--tree-icon:16px] [--tree-radius:var(--fndn-radius-chip)]" },
      md: { base: "text-md [--tree-line:20px] [--tree-py:6px] [--tree-box:20px] [--tree-gap:6px] [--tree-pad:8px] [--tree-caret:14px] [--tree-icon:18px] [--tree-radius:var(--fndn-radius-control)]" },
      lg: { base: "text-md [--tree-line:20px] [--tree-py:8px] [--tree-box:20px] [--tree-gap:6px] [--tree-pad:8px] [--tree-caret:14px] [--tree-icon:18px] [--tree-radius:var(--fndn-radius-control)]" },
    },
  },
  defaultVariants: { size: "md" },
});

export type TreeViewRecipeProps = VariantProps<typeof treeView>;
```
