Tree View

An outline of rows that open and close with disclosure triangles, with arrow-key navigation and focused and unfocused selection.

@cupertinoui/tree-viewRegistry JSON

Installation

pnpm dlx shadcn@latest add @cupertinoui/tree-view

The first install also adds the macOS theme. See Installation to set up the registry.

Usage

import { TreeView, TreeViewItem } from "@/components/ui/tree-view"

<TreeView aria-label="Project" defaultExpanded={["app"]}>
  <TreeViewItem value="app" label="app" icon={<FolderIcon />}>
    <TreeViewItem value="page" label="page.tsx" icon={<FileCodeIcon />} badge="M" />
  </TreeViewItem>
</TreeView>
Up and Down move the selection, Right opens a row or moves into it, Left closes it or moves to its parent, and Home and End jump to the first and last rows. A double-click or a click on the triangle opens and closes a row.
The selection is accent blue while the tree has focus and grey when it doesn't, like an outline view on macOS.

Examples

Controlled

Pass value and expanded with their change handlers to drive the selection and the open rows yourself.

API Reference

Every part forwards the props of the underlying element. The props below are the ones CupertinoUI adds or changes.

TreeView

PropTypeDescription
value / defaultValuestring | nullThe selected row.
onValueChange(value: string) => voidCalled when a click or the keyboard selects a row.
expanded / defaultExpandedstring[]The open rows.
onExpandedChange(expanded: string[]) => voidCalled when a row opens or closes.
classNamestringExtra classes, merged with the component's own.

TreeViewItem

Nest TreeViewItems inside to give it a disclosure triangle.

PropTypeDescription
valuestringIdentifies the row; unique in the tree.
labelReactNodeThe name, cut short with an ellipsis.
iconReactNodeA symbol before the name, 16pt.
badgeReactNodeTrailing content, such as a count or a status letter.
classNamestringExtra classes, merged with the component's own.