TreeView

A composable, accessible tree view component with expand/collapse, single & multi-select, keyboard navigation, and indent guide lines.

Preview

  • src
    • app
      • page.tsx
      • layout.tsx
      • globals.css
    • components
      • ui
        • button.tsx
        • card.tsx
  • package.json
  • tsconfig.json
  • README.md
  • Installation

    Usage

    tsximport {
      TreeView,
      TreeItem,
      TreeItemLabel,
      TreeItemActions,
    } from "@/components/ui/tree-view"
    import { Folder, FileCode } from "lucide-react"
    
    export function MyTree() {
      return (
        <TreeView defaultExpanded={["src"]} selectionMode="single">
          <TreeItem value="src">
            <TreeItemLabel>
              <Folder className="size-4 text-sky-500" />
              src
            </TreeItemLabel>
    
            <TreeItem value="index.ts">
              <TreeItemLabel>
                <FileCode className="size-4 text-blue-500" />
                index.ts
              </TreeItemLabel>
            </TreeItem>
          </TreeItem>
        </TreeView>
      )
    }

    Controlled Mode

    Pass expanded and selected props for full control over state.

    tsxconst [expanded, setExpanded] = useState(["src"])
    const [selected, setSelected] = useState<string[]>([])
    
    <TreeView
      expanded={expanded}
      onExpandedChange={setExpanded}
      selected={selected}
      onSelectedChange={setSelected}
      selectionMode="multiple"
    >
      {/* ... */}
    </TreeView>

    Item Actions

    Use TreeItemActions to add hover-visible action buttons.

    tsx<TreeItem value="file.ts">
      <TreeItemLabel>
        <FileCode className="size-4" />
        file.ts
      </TreeItemLabel>
      <TreeItemActions>
        <Button variant="ghost" size="icon" className="size-6">
          <Pencil className="size-3" />
        </Button>
        <Button variant="ghost" size="icon" className="size-6">
          <Trash2 className="size-3" />
        </Button>
      </TreeItemActions>
    </TreeItem>

    API Reference

    TreeView

    PropTypeDefaultDescription
    expandedstring[]Controlled expanded item values.
    defaultExpandedstring[][]Items expanded on initial render (uncontrolled).
    onExpandedChange(expanded: string[]) => voidCalled when expanded state changes.
    selectedstring[]Controlled selected item values.
    defaultSelectedstring[][]Items selected on initial render (uncontrolled).
    onSelectedChange(selected: string[]) => voidCalled when selection changes.
    selectionMode"single" | "multiple" | "none""single"How items can be selected.
    indicatorbooleantrueShow indent guide lines.
    size"sm" | "md" | "lg""md"Text size variant.

    TreeItem

    PropTypeDefaultDescription
    valuestringUnique identifier for this item (required).

    TreeItemLabel

    PropTypeDefaultDescription
    childrenReactNodeLabel content (icons, text, etc.).

    TreeItemActions

    PropTypeDefaultDescription
    childrenReactNodeAction buttons shown on hover/focus.

    Keyboard Navigation

    KeyAction
    Arrow DownMove focus to next visible item
    Arrow UpMove focus to previous visible item
    Arrow RightExpand collapsed node
    Arrow LeftCollapse expanded node
    EnterSelect item and toggle expand
    SpaceSelect item
    HomeMove focus to first item
    EndMove focus to last visible item