Tree

Data Display
Navigation

Hierarchical tree structure with expandable nodes, selection, and checkboxes.

Preview

File system tree with expandable folders

src
components
Button.tsx
Card.tsx
Input.tsx
styles.css
utils.ts
package.json

Selectable Tree

Click nodes to select them

Documents
Personal
Resume.pdf

Checkable Tree

Check nodes with cascade behavior

src
styles.css
utils.ts
package.json

Props

Component API reference

Tree

PropTypeDefaultDescription
data*TreeNode[]Array of root tree nodes
defaultExpandedstring[][]IDs of initially expanded nodes
selectablebooleanfalseEnable node selection
checkablebooleanfalseEnable checkboxes
onSelect(node: TreeNode) => voidCallback when node is selected
onCheck(ids: Set<string>) => voidCallback with checked node IDs
classNamestringAdditional CSS classes

TreeNode Type

PropTypeDefaultDescription
id*stringUnique node identifier
label*stringNode display label
type'file' | 'document' | 'image' | 'code'File type for icon display
childrenTreeNode[]Child nodes (makes this a folder)

Usage

Import and implementation examples

import { Tree, type TreeNode } from '@/components/ui/tree'

const data: TreeNode[] = [
  {
    id: '1',
    label: 'src',
    children: [
      { id: '1-1', label: 'Button.tsx', type: 'code' },
      { id: '1-2', label: 'Card.tsx', type: 'code' }
    ]
  },
  { id: '2', label: 'package.json', type: 'document' }
]

// Basic tree
<Tree data={data} />

// With default expanded nodes
<Tree
  data={data}
  defaultExpanded={['1']}
/>

// Selectable tree
<Tree
  data={data}
  selectable
  onSelect={(node) => console.log('Selected:', node)}
/>

// Checkable tree with cascade
<Tree
  data={data}
  checkable
  onCheck={(ids) => console.log('Checked:', ids)}
/>

Features

Built-in functionality

  • Unlimited nesting: Hierarchical tree structure with any depth
  • Expand/collapse: Toggle folders with smooth animation
  • Selectable nodes: Single selection with visual feedback
  • Checkable nodes: Checkboxes with cascade behavior
  • File type icons: Auto icons for folder, file, document, image, code
  • Connection lines: Visual lines between parent and children
  • Dark mode: Full dark mode support

Accessibility

Accessibility considerations

ARIA Attributes

Uses role="tree" and role="treeitem"aria-expanded for expandable nodesaria-selected for selectable nodesaria-checked for checkable nodes

Keyboard Navigation

KeyAction
Arrow Up/DownNavigate between visible nodes
Arrow LeftCollapse node or move to parent
Arrow RightExpand node or move to first child
Enter / SpaceSelect or toggle checkbox

Notes

  • Full keyboard navigation support
  • Focus management within tree structure
  • Screen reader announces node state

Related Components