> For the complete documentation index, see [llms.txt](https://docs.rierino.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.rierino.com/sdk-core-ui/components/tree-view.md). Agents can also search and read this documentation through its [MCP server](https://docs.rierino.com/~gitbook/mcp).

# TreeView

Shows nested items, such as categories or folders, as rows that open and close.

{% code title="tree-view/demo.tsx" %}

```tsx
import { useState } from "react";
import { TreeView } from "@rierino-open/core-ui";

const categories = [
  {
    id: "women",
    title: "Women",
    children: [
      {
        id: "women-clothing",
        title: "Clothing",
        children: [
          { id: "women-dresses", title: "Dresses" },
          { id: "women-shirts", title: "Shirts" },
        ],
      },
      { id: "women-shoes", title: "Shoes" },
    ],
  },
  {
    id: "men",
    title: "Men",
    children: [
      { id: "men-shirts", title: "Shirts" },
      { id: "men-trousers", title: "Trousers" },
    ],
  },
  { id: "accessories", title: "Accessories" },
];

export default function TreeViewDemo() {
  const [selected, setSelected] = useState("nothing yet");

  return (
    <div style={{ display: "grid", gap: 12, width: 280, fontSize: 12 }}>
      <div
        style={{
          background: "linear-gradient(180deg, #2921a3 0%, #361db0 33.04%, #a351cf 100%)",
          borderRadius: 12,
          padding: 8,
        }}
      >
        <TreeView items={categories} onItemClick={(item) => setSelected(item.title)} />
      </div>
      <span>Clicked: {selected}</span>
    </div>
  );
}
```

{% endcode %}

## Usage

{% tabs %}
{% tab title="React app" %}

```tsx
import { TreeView } from '@rierino-open/core-ui';

<TreeView
  items={[
    { id: 'women', title: 'Women', children: [{ id: 'dresses', title: 'Dresses' }] },
    { id: 'accessories', title: 'Accessories' },
  ]}
  onItemClick={(item) => open(item.id)}
/>
```

{% endtab %}

{% tab title="Hosted editor" %}

```jsx
import CoreUI from '@rierino-open/core-ui';
const { TreeView } = CoreUI;

<TreeView
  items={[
    { id: 'women', title: 'Women', children: [{ id: 'dresses', title: 'Dresses' }] },
    { id: 'accessories', title: 'Accessories' },
  ]}
  onItemClick={(item) => open(item.id)}
/>
```

{% endtab %}
{% endtabs %}

Each item has an `id` and a `title`, and nested items go in `children`. An item with children shows a chevron and can be opened. `onItemClick` receives the clicked item object.

{% hint style="info" %}
**Made for dark backgrounds**

TreeView is the tree in Rierino's list sidebars: its text is light (mischka) and nested rows are shaded with white. Place it on a dark background, such as the `g-list` gradient used in the examples.
{% endhint %}

## Examples

### Indenting nested rows

TreeView shades each level a little lighter, but doesn't indent it. React Aria sets a `--tree-item-level` variable on every row (1 at the top level, 2 for its children and so on), which you can use to indent rows from your own CSS.

TreeView doesn't take a `className`, so wrap it in an element with your own class and target its rows as `.react-aria-Tree > div`. This is how Rierino's list sidebars indent their trees.

{% code title="tree-view/indent.tsx" %}

```tsx
import { TreeView } from "@rierino-open/core-ui";

const pages = [
  {
    id: "catalog",
    title: "Catalog",
    children: [
      {
        id: "products",
        title: "Products",
        children: [
          { id: "variants", title: "Variants" },
          { id: "bundles", title: "Bundles" },
        ],
      },
      { id: "categories", title: "Categories" },
    ],
  },
  { id: "orders", title: "Orders" },
];

export default function TreeViewIndent() {
  return (
    <div className="Rie-PageTree" style={{ width: 280 }}>
      {/* React Aria sets --tree-item-level on every row: 1 for the top level, 2 for its children… */}
      <style>{`
        .Rie-PageTree {
          background: #2921a3;
          border-radius: 12px;
          padding: 8px;
        }
        .Rie-PageTree .react-aria-Tree > div {
          padding-inline-start: calc(10px + (var(--tree-item-level) - 1) * 16px);
        }
      `}</style>
      <TreeView items={pages} />
    </div>
  );
}
```

{% endcode %}

### Loading children on demand

When `onExpand` is set, every row shows a chevron, even without `children`. Pressing a chevron calls `onExpand` with the item, so you can fetch its children and add them to `items`. The row opens to show them.

{% code title="tree-view/lazy.tsx" %}

```tsx
import { useState } from "react";
import { TreeView } from "@rierino-open/core-ui";

type Node = { id: string; title: string; children?: Node[] };

// Stands in for an API call that returns a folder's children.
function fetchChildren(parent: Node): Promise<Node[]> {
  return new Promise((resolve) =>
    setTimeout(
      () =>
        resolve(
          [1, 2, 3].map((n) => ({ id: `${parent.id}/${n}`, title: `${parent.title} ${n}` }))
        ),
      400
    )
  );
}

function addChildren(nodes: Node[], id: string, children: Node[]): Node[] {
  return nodes.map((node) =>
    node.id === id
      ? { ...node, children }
      : { ...node, children: node.children && addChildren(node.children, id, children) }
  );
}

export default function TreeViewLazy() {
  const [tree, setTree] = useState<Node[]>([
    { id: "assets", title: "Assets" },
    { id: "templates", title: "Templates" },
  ]);

  return (
    <div style={{ width: 280, background: "#2921a3", borderRadius: 12, padding: 8 }}>
      <TreeView
        items={tree}
        onExpand={async (node: Node) => {
          if (node.children?.length) return;
          const children = await fetchChildren(node);
          setTree((current) => addChildren(current, node.id, children));
        }}
      />
    </div>
  );
}
```

{% endcode %}

`onExpand` is only called from the chevron, and on every press, so also when a row is closed. Skip the request when the item already has its children, as above.

### Highlighting the current item

TreeView has no selection state of its own. To mark the current item, set its `titleClassName` from your own state.

{% code title="tree-view/title-class.tsx" %}

```tsx
import { useState } from "react";
import { TreeView } from "@rierino-open/core-ui";

const statuses = [
  { id: "draft", title: "Drafts" },
  { id: "review", title: "In review" },
  { id: "published", title: "Published" },
  { id: "archived", title: "Archived" },
];

export default function TreeViewTitleClass() {
  const [current, setCurrent] = useState("review");

  return (
    <div style={{ width: 280, background: "#2921a3", borderRadius: 12, padding: 8 }}>
      <TreeView
        items={statuses.map((status) => ({
          ...status,
          // Highlight the current item: titleClassName is added to the item's title.
          titleClassName: status.id === current ? "text-white font-bold" : "",
        }))}
        onItemClick={(item) => setCurrent(item.id)}
      />
    </div>
  );
}
```

{% endcode %}

`text-white` and `font-bold` are included in Core UI's stylesheet, so this works in hosted editors too.

### Long titles

Titles that don't fit are cut off with an ellipsis, leaving room for the chevron.

{% code title="tree-view/long-titles.tsx" %}

```tsx
import { TreeView } from "@rierino-open/core-ui";

const items = [
  {
    id: "summer",
    title: "Summer collection 2026 — linen, cotton and lightweight knitwear",
    children: [{ id: "summer-shirts", title: "Short-sleeved linen shirts in seven colours" }],
  },
  { id: "basics", title: "Everyday basics that stay in the range all year" },
];

export default function TreeViewLongTitles() {
  return (
    <div style={{ width: 220, background: "#2921a3", borderRadius: 12, padding: 8 }}>
      <TreeView items={items} />
    </div>
  );
}
```

{% endcode %}

## Interaction

| Action          | Result                                                                        |
| --------------- | ----------------------------------------------------------------------------- |
| Click a row     | Calls `onItemClick`, and opens or closes the row if it has children           |
| Click a chevron | Opens or closes the row, and calls `onExpand`                                 |
| `↓` `↑`         | Moves between visible rows                                                    |
| `→` `←`         | Opens or closes the focused row; on a closed or leaf row, moves to its parent |
| `Enter`         | Opens or closes the focused row                                               |

`onItemClick` is called for clicks only. The keyboard opens and closes rows but doesn't call `onItemClick` or `onExpand`.

## Accessibility

* Renders a React Aria `treegrid`, so screen readers announce each row with its level and whether it is open. The tree is labelled "Files".
* Rows can be reached with `Tab` and navigated with the arrow keys.

## Styling

Rows use 11px text in mischka. Each level is shaded with 10% more white than the one above. The chevron turns down when a row is open.

| Target          | How                                                                         |
| --------------- | --------------------------------------------------------------------------- |
| The whole tree  | Wrap it in your own element and class; TreeView takes no `className`        |
| Rows            | `.react-aria-Tree > div`, with `var(--tree-item-level)` for the row's depth |
| An item's title | `titleClassName` on the item                                                |
| The chevron     | `[slot="chevron"]`                                                          |

| Attribute             | On  | Present when                |
| --------------------- | --- | --------------------------- |
| `data-expanded`       | Row | The row is open             |
| `data-has-child-rows` | Row | The item has children       |
| `data-hovered`        | Row | The pointer is over the row |
| `data-focus-visible`  | Row | The row has keyboard focus  |

## API reference

### TreeView

| Prop          | Type                 | Default | Description                                                                                                                                                       |
| ------------- | -------------------- | ------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `items`       | `Item[]`             |         | **Required.** The rows, with nested rows in `children`.                                                                                                           |
| `onItemClick` | `((x: any) => void)` |         | Called with the item when its row is clicked.                                                                                                                     |
| `onExpand`    | `((x: any) => void)` |         | Called with the item when its chevron is pressed, to open or close it. When set, every row shows a chevron, so children can be loaded when a row is first opened. |

### Item

| Prop             | Type               | Default | Description                                                                  |
| ---------------- | ------------------ | ------- | ---------------------------------------------------------------------------- |
| `title`          | `string`           |         | **Required.** The text shown for the row.                                    |
| `id`             | `string \| number` |         | **Required.** A unique id for the row.                                       |
| `children`       | `Item[]`           |         | Nested rows. An item with children can be expanded.                          |
| `titleClassName` | `string`           |         | Classes added to the row's title, for example to highlight the current item. |


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.rierino.com/sdk-core-ui/components/tree-view.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
