Skip to content
synccn

Tree View

New

Generic tree with tri-state checkboxes that cascade, or single selection, and full keyboard support.

Projects
View projects
Edit projects
Delete projects
Team
Invite members
Change roles
Remove members

3 permissions granted

Installation

$ pnpm dlx shadcn@latest add @synccn/tree-view

First time in this project? Connect synccn with one command.

Usage

import { TreeView } from "@/components/ui/tree-view";<TreeView data={nodes} value={checked} onValueChange={setChecked} />

API reference

Generated from the component's TypeScript types, so it always matches the code.

TreeView

Generic tree with expand and collapse, tri-state checkboxes that cascade to children, or single selection, plus full keyboard support. Use it for permissions, categories, folders.

  • datarequired
    TreeNode[]

    —

  • selectionMode
    "checkbox" | "single"

    Default "checkbox"

    "checkbox" checks leaves and shows parents as checked or partly checked; "single" selects one node.

  • value
    string[]

    Controlled checked leaf ids (checkbox) or the selected id in a one-item array (single).

  • defaultValue
    string[]

    Default []

    Selection at first, when uncontrolled.

  • onValueChange
    (value: string[]) => void

    Called with the new selection.

  • defaultExpanded
    string[]

    Default []

    Ids of nodes open at first.

  • aria-label
    string

    Accessible name for the tree.

  • className
    string

    Extra classes for the root element.

Dependencies

Read from the component's imports when the site builds. The CLI installs all of these for you.

Helpers

  • lib/hooksuseControllable (works controlled or uncontrolled)
  • lib/utilscn (merges class names)

npm packages

Keyboard

  • ↓or↑Move between nodes
  • →Open, or move to the first child
  • ←Close, or move to the parent
  • SpaceCheck or select
  • EnterOpen a branch, or select
  • HomeorEndFirst or last node

Accessibility

  • Uses tree and treeitem roles; checkboxes report aria-checked including "mixed" for partly checked branches.

From the source

Read from this component's code when the site builds, so it can't drift.

  • ARIA roles: tree, treeitem
  • ARIA attributes: aria-checked, aria-disabled, aria-expanded, aria-label, aria-level, aria-multiselectable, aria-posinset, aria-selected, aria-setsize
  • Keys handled: ArrowUp, ArrowDown, ArrowLeft, ArrowRight, Home, End, Enter, Space
  • Shows a visible focus ring for keyboard users