# Tree

A hierarchy (objects → fields, a flow's elements, a knowledge base's sections) with the keyboard behaviour of a tree: ↑↓ move, → opens or steps in, ← closes or steps out, Home/End jump, Enter selects. One tab stop for the whole tree.

- Registry name: `tree` (`@candor/tree`)
- Tier: Pro. Installing it needs a Pro, Premium or Team licence key: the person runs `npx candor-ui key <key>` themselves; never ask for the key in chat or write it to a file.
- Docs: https://candor.themethodic.com/docs/tree

## Install

```bash
npx candor-ui add tree
```

Run `npx candor-ui@latest init` once first (https://candor.themethodic.com/get-started.md). Without the CLI: `npx shadcn@latest add @candor/tree`.
It brings in: data, locale, utils.
npm packages: lucide-react@^1.67.0.

## Example

```tsx
"use client"

import * as React from "react"
import { Columns3, Flag, Table2 } from "lucide-react"
import { Tree } from "@/components/tree"
import type { TreeNode } from "@/lib/data"

const schema: TreeNode[] = [
  {
    id: "tables",
    label: "Tables",
    meta: "24",
    icon: Table2,
    children: [
      {
        id: "projects",
        label: "projects",
        meta: "18 columns",
        icon: Table2,
        children: [
          { id: "projects-region", label: "region", meta: "enum", icon: Columns3 },
          { id: "projects-owner", label: "owner_id", meta: "uuid", icon: Columns3 },
          { id: "projects-archived", label: "archived_at", meta: "timestamp", icon: Columns3 },
        ],
      },
      { id: "deployments", label: "deployments", meta: "12 columns", icon: Table2, children: [{ id: "deployments-sha", label: "commit_sha", meta: "text", icon: Columns3 }] },
      { id: "users", label: "users", meta: "21 columns", icon: Table2 },
    ],
  },
  { id: "flags", label: "Feature flags", meta: "9", icon: Flag, children: [{ id: "flag-billing", label: "new-billing-page", meta: "25% rollout", icon: Flag }] },
]

export default function Example() {
  const [selected, setSelected] = React.useState<string | undefined>()
  return (
    <div className="max-w-xs rounded-xl border bg-card p-1.5">
      <Tree nodes={schema} defaultOpen={["tables", "projects"]} selectedId={selected} onSelect={(n) => setSelected(n.id)} label="acme/api schema" />
    </div>
  )
}
```

## <Tree>

Props:

- `nodes: TreeNode[]`: The top-level nodes, each with its children.
- `selectedId?: string | undefined`: The selected node's id, highlighted and focused first.
- `onSelect?: ((node: TreeNode) => void) | undefined`: Called with the node a person clicks or presses Enter on.
- `defaultOpen?: string[] | undefined` (default `[]`): Ids of nodes open on first render.
- `label?: string | undefined`: The tree's accessible name.
- `className?: string | undefined`

## Strings (`tree`)

The fixed words, English by default. Override any of them under `tree` in `<LocaleProvider strings={{ tree: { … } }}>`; the table is exported as `treeStrings`.

- `tree`: Tree

## Rules for using Candor UI

- Components take plain typed data. Never pass model output as HTML or render it as a link; messages are typed blocks.
- A control that can't be used says why: wrap it in `<Locked reason="…">`, don't just disable it.
- Don't show a value you don't have: leave it out (it renders as —) rather than guess. An estimate is labelled as one.
- Nothing is pre-selected in a decision a person makes, and the agent is named from your app's data, never from model output.
- Your app's words go in `LocaleProvider` (fixed strings) and `TermsProvider` (nouns); entity kinds and agent roles in `EntityKindsProvider` and `AgentRolesProvider`.
