Skip to content
UI

Search docs

Search components and documentation

New
Menu

Tree View

A hierarchical list for browsing nested items — files, folders, categories — with expand/collapse, selection and full keyboard navigation.

  • src
    • components
      • button.tsx
      • card.tsx
      • tree-view.tsx
  • README.md
  • package.json

Installation

npx logic2b@latest add tree-view

Usage

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

<TreeView aria-label="Files" defaultExpanded={["src"]}>
  <TreeItem value="src" label="src">
    <TreeItem value="button" label="button.tsx" />
    <TreeItem value="card" label="card.tsx" />
  </TreeItem>
  <TreeItem value="readme" label="README.md" />
</TreeView>

Nesting TreeItem children turns an item into a branch — the chevron, aria-expanded state and indentation are derived automatically.

API

Prop Type Default
expanded / defaultExpanded (TreeView) string[] — controlled/uncontrolled expanded branch values []
onExpandedChange (TreeView) (expanded: string[]) => void
selected / defaultSelected (TreeView) string | null — controlled/uncontrolled selected value null
onSelectedChange (TreeView) (selected: string | null) => void
value (TreeItem) string — unique item id (required)
label (TreeItem) ReactNode — the row text (required)
icon (TreeItem) ReactNode — rendered before the label
disabled (TreeItem) boolean — skipped by pointer and keyboard false

Examples

Controlled

Drive expansion and selection externally — expand/collapse everything from the outside and react to the selected value.

  • workspace

Selected: none

Accessibility

  • Follows the WAI-ARIA Tree View pattern.
  • Arrow Up / Arrow Down move through visible items; Home / End jump to the first/last.
  • Arrow Right expands a collapsed branch (then steps into it); Arrow Left collapses an expanded branch or moves to the parent.
  • Enter or Space selects the focused item (and toggles branches).
  • Items expose aria-level, aria-expanded and aria-selected; disabled items are aria-disabled and skipped.