shadcn.io is not affiliated with official shadcn/ui
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.
app
components
package.json
README.md
Installation
bunx --bun shadcn@latest add https://kit.dev/r/radix/tree-view.jsonAnatomy
TreeViewRoot
├── TreeViewBranch
│ ├── TreeViewBranchContent
│ │ └── TreeViewBranchIndentGuide
│ ├── TreeViewBranchIndicator
│ ├── TreeViewBranchItem
│ │ └── TreeViewBranchControl
│ └── TreeViewBranchTrigger
├── TreeViewContent
│ ├── TreeViewItemIndicator
│ └── TreeViewItem
├── TreeViewLabel
├── TreeViewCheckbox
│ └── TreeViewNodeRenameInput
└── TreeViewTreeUsage
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
| Key | Description |
|---|---|
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. |
Meta+A | Select all. |
F2 | Rename the focused item. |
Demos
Checkbox Tree
Context Menu
Controlled
Custom Icons
Custom Icons Folder
Custom Icons Item
Editor
Links
Multiple Selection
Rename
API Reference
TreeViewBranch
div
PropType
className?stringAttributeType
CSS variableType
--depthstring--icon-sizestring--indentationstring--item-gapstring--padding-blockstring--padding-inlinestringTreeViewBranchContent
div
PropType
AttributeType
CSS variableType
--depthstring--icon-sizestring--indentationstring--padding-inlinestringTreeViewBranchItem
div
PropType
AttributeType
TreeViewBranchIndentGuide
div
PropType
CSS variableType
--depthstring--icon-sizestring--indentationstring--padding-inlinestringTreeViewBranchIndicator
div
PropType
AttributeType
TreeViewContent
div
PropType
AttributeType
CSS variableType
--depthstringTreeViewItem
span
PropType
AttributeType
CSS variableType
--item-gapstringTreeViewLabel
h3
PropType
AttributeType
TreeViewCheckbox
span
PropType
AttributeType
TreeViewNodeRenameInput
input
PropType
TreeViewTree
div
PropType
AttributeType
CSS variableType
--icon-sizestringcreateTreeCollection
PropType
AttributeType
createFileIcons
PropType
AttributeType
TreeViewNode
PropType
nodeTchildren?React.ReactNodeAttributeType
useTreeView
PropType