Skip to content

shadcn.io is not affiliated with official shadcn/ui

Shadcn Tree View for React Radix UI

Hierarchical data in a tree structure.

Framework
UI library

Radix UI does not include this primitive, so this page uses the Ark UI component. The visuals match the Ark UI version.

package.json
README.md

Installation

bunx --bun shadcn@latest add https://kit.dev/r/radix/tree-view.json

Anatomy

TreeViewRoot
├── TreeViewBranch
│   ├── TreeViewBranchContent
│   │   └── TreeViewBranchIndentGuide
│   ├── TreeViewBranchIndicator
│   ├── TreeViewBranchItem
│   │   └── TreeViewBranchControl
│   └── TreeViewBranchTrigger
├── TreeViewContent
│   ├── TreeViewItemIndicator
│   └── TreeViewItem
├── TreeViewLabel
├── TreeViewCheckbox
│   └── TreeViewNodeRenameInput
└── TreeViewTree

Usage

import {
  createTreeCollection,
  TreeView,
  TreeViewBranch,
  TreeViewBranchContent,
  TreeViewBranchItem,
  TreeViewContent,
  TreeViewItem,
  TreeViewNode,
  TreeViewTree,
} from "@/components/ui/tree-view";
const TreeViewDemo = () => (
  <div className="w-full max-w-48">
    <TreeView collection={collection}>
      <TreeViewTree>
        {collection.rootNode.children?.map((node, index) => (
          <TreeNode indexPath={[index]} key={node.id} node={node} />
        ))}
      </TreeViewTree>
    </TreeView>
  </div>
);

const collection = createTreeCollection({
  rootNode: {
    id: "ROOT",
    name: "",
    children: [
      {
        id: "app",
        name: "app",
        children: [
          { id: "app/page.tsx", name: "page.tsx" },
          { id: "app/layout.tsx", name: "layout.tsx" },
        ],
      },
      {
        id: "components",
        name: "components",
        children: [
          { id: "components/button.tsx", name: "button.tsx" },
          { id: "components/input.tsx", name: "input.tsx" },
        ],
      },
      { id: "package.json", name: "package.json" },
      { id: "readme.md", name: "README.md" },
    ],
  },
});

const TreeNode = (props: React.ComponentProps<typeof TreeViewNode>) => {
  const { node, indexPath, ...rest } = props;

  return (
    <TreeViewNode indexPath={indexPath} node={node} {...rest}>
      {node.children ? (
        <TreeViewBranch>
          <TreeViewBranchItem>{node.name}</TreeViewBranchItem>

          <TreeViewBranchContent>
            {node.children.map((child, index) => (
              <TreeNode
                indexPath={[...indexPath, index]}
                key={child.id}
                node={child}
              />
            ))}
          </TreeViewBranchContent>
        </TreeViewBranch>
      ) : (
        <TreeViewContent>
          <TreeViewItem>{node.name}</TreeViewItem>
        </TreeViewContent>
      )}
    </TreeViewNode>
  );
};

Accessibility

KeyDescription
ArrowDown
Move down.
ArrowUp
Move up.
ArrowLeft
Move left.
ArrowRight
Expand the focused branch.
Home
Go to the start.
End
Go to the end.
Space
Activate the focused item.
Enter
Activate the focused item.
*
Expand sibling nodes.
MetaA
Select all.
F2
Rename the focused item.

Demos

Checkbox Tree

Context Menu

Controlled

Custom Icons

Custom Icons Folder

Custom Icons Item

Editor

Multiple Selection

Rename

API Reference

TreeViewBranch

div

PropType
className?string
AttributeType
CSS variableType
--depthstring
--icon-sizestring
--indentationstring
--item-gapstring
--padding-blockstring
--padding-inlinestring

TreeViewBranchContent

div

PropType
AttributeType
CSS variableType
--depthstring
--icon-sizestring
--indentationstring
--padding-inlinestring

TreeViewBranchItem

div

PropType
AttributeType

TreeViewBranchIndentGuide

div

PropType
CSS variableType
--depthstring
--icon-sizestring
--indentationstring
--padding-inlinestring

TreeViewBranchIndicator

div

PropType
AttributeType

TreeViewContent

div

PropType
AttributeType
CSS variableType
--depthstring

TreeViewItem

span

PropType
AttributeType
CSS variableType
--item-gapstring

TreeViewLabel

h3

PropType
AttributeType

TreeViewCheckbox

span

PropType
AttributeType

TreeViewNodeRenameInput

input

PropType

TreeViewTree

div

PropType
AttributeType
CSS variableType
--icon-sizestring

createTreeCollection

PropType
AttributeType

createFileIcons

PropType
AttributeType

TreeViewNode

PropType
nodeT
children?React.ReactNode
AttributeType

useTreeView

PropType