docs/components/tree-view
Sections
Components
Utilities
Forms
Hooks
Copy Markdown
Hierarchical data in a tree structure.
PreviewCode
app
components
package.json
README.md
CLIManual
pnpmbunnpmyarn
pnpm dlx shadcn@latest add @shark/tree-view
TreeView
├── TreeViewLabel
└── TreeViewTree
└── TreeViewNode
├── TreeViewBranch
│ ├── TreeViewBranchItem
│ │ └── TreeViewBranchIndicator
│ └── TreeViewBranchContent
│ └── TreeViewNode …
└── TreeViewContent
└── TreeViewItem
└── TreeViewCheckbox
import {
TreeView,
TreeViewLabel,
TreeViewTree,
TreeViewNode,
TreeViewBranch,
TreeViewBranchItem,
TreeViewBranchContent,
TreeViewContent,
TreeViewItem,
createTreeCollection,
} from "@/components/ui/tree-view";
const collection = createTreeCollection({
rootNode: { id: "ROOT", name: "", children: [...] },
});
const Example = () => (
<TreeView collection={collection}>
<TreeViewTree>
{collection.rootNode.children?.map((node, index) => (
<TreeNode indexPath={[index]} key={node.id} node={node} />
))}
</TreeViewTree>
</TreeView>
)
const TreeNode = (props: React.ComponentProps<typeof TreeViewNode>) => {
const { node, indexPath } = props;
return (
<TreeViewNode indexPath={indexPath} key={node.id} node={node}>
{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>
);
};
Use selectedValue and onSelectionChange to control the selected node and react to selection changes.
PreviewCode
Select input.tsx
components
package.json
❌
Use selectionMode="multiple" to allow selecting multiple nodes.
Hold Shift and click to select a range, or use Ctrl/Cmd+click to toggle individual nodes.
PreviewCode
app
components
package.json
README.md
Right-click on tree items to show a context menu.
PreviewCode
app
components
package.json
README.md
Use the canRename prop and onRenameComplete callback to enable inline renaming of nodes. Press F2 to activate rename mode on the focused node.
PreviewCode
app
components
package.json
README.md
Use the checkedValue and onCheckedChange props to control the checked nodes.
PreviewCode
app
components
package.json
README.md
["readme.md"]
Tree items can be rendered as links. Use asChild on TreeViewContent and wrap an <a> element with TreeViewItem inside.
PreviewCode
Documentation
External Links
Use the tree view to create a file editor.
PreviewCode
app
components
package.json
README.md
There are three ways to customize icons:
icon and expandedIcon props on TreeViewBranchItem, or per-node via TreeNodeTypeicon prop on TreeViewItemfileIcons prop on TreeView with createFileIconsUse the icon and expandedIcon props on TreeViewBranchItem to customize folder icons. Pass null to hide the icon.
PreviewCode
app
components
package.json
README.md
Pass the icon prop to TreeViewItem to override the icon for a specific leaf node.
PreviewCode
app
components
package.json
README.md
Use the fileIcons prop on TreeView with createFileIcons to map file extensions to icon components. Icons are resolved from each node's id; unmatched extensions fall back to FileIcon.
PreviewCode
app
components
package.json
README.md
Root component. Provides tree context and manages expand/selection state.
| Prop | Type | Default |
|---|---|---|
collection | TreeCollection | required |
fileIcons | `Record<string, React.JSX.ElementType | null>` |
selectedValue | string[] | - |
checkedValue | string[] | - |
onCheckedChange | (details: CheckedChangeDetails) => void | - |
canRename | (details: RenameDetails) => boolean | - |
onRenameComplete | (details: RenameCompleteDetails) => void | - |
defaultSelectedValue | string[] | - |
onSelectionChange | (details: SelectionChangeDetails) => void | - |
expandedValue | string[] | - |
defaultExpandedValue | string[] | - |
onExpandedChange | (details: ExpandedChangeDetails) => void | - |
selectionMode | `"single" | "multiple"` |
lazyMount | boolean | true |
unmountOnExit | boolean | true |
className | string | - |
| Attribute | Default |
|---|---|
--indentation | --spacing(4) |
--item-gap | --spacing(2) |
--padding-block | --spacing(1.5) |
--padding-inline | --spacing(3) |
--icon-size | --spacing(4) |
Accessible label for the tree.
| Prop | Type | Default |
|---|---|---|
asChild | boolean | false |
className | string | - |
Wraps the root-level tree nodes.
| Prop | Type | Default |
|---|---|---|
asChild | boolean | false |
className | string | - |
Provides tree node context. Must wrap each branch or item.
| Prop | Type | Default |
|---|---|---|
node | TreeNodeType | required |
indexPath | number[] | required |
className | string | - |
Expandable branch with children. Toggles open/closed.
| Prop | Type | Default |
|---|---|---|
asChild | boolean | false |
className | string | - |
Clickable area to expand or collapse a branch.
| Prop | Type | Default |
|---|---|---|
asChild | boolean | false |
icon | `React.JSX.ElementType | null` |
expandedIcon | `React.JSX.ElementType | null` |
className | string | - |
Holds the branch children. Shown when expanded.
| Prop | Type | Default |
|---|---|---|
asChild | boolean | false |
className | string | - |
Wrapper for leaf (non-expandable) items.
| Prop | Type | Default |
|---|---|---|
asChild | boolean | false |
className | string | - |
Leaf node (non-expandable). The icon prop is fallback when no fileIcons match.
| Prop | Type | Default |
|---|---|---|
icon | React.JSX.ElementType | FileIcon |
className | string | - |
Checkbox for selecting a node in multi-select mode.
| Prop | Type | Default |
|---|---|---|
className | string | - |
Creates a tree collection from node data. Use with collection prop on TreeView.
Options
| Prop | Type | Default |
|---|---|---|
rootNode | T extends TreeNodeType | required |
nodeToValue | (node: T) => string | (node) => node.id |
nodeToString | (node: T) => string | (node) => node.name |
TreeNodeType — Shape of each node (extend with generic T for custom props):
| Property | Type | Default |
|---|---|---|
id | string | - |
name | string | - |
children | TreeNodeType<T>[] | undefined |
icon | `React.JSX.ElementType | null` |
expandedIcon | `React.JSX.ElementType | null` |
const collection = createTreeCollection({
rootNode: { id: "ROOT", name: "", children: [...] },
});
// With custom node props
type LinkNode = TreeNodeType & { href?: string };
const links = createTreeCollection<LinkNode>({
rootNode: {
id: "docs",
name: "Docs",
children: [{ id: "intro", name: "Intro", href: "/intro" }],
},
});
Creates a type-safe mapping of file extensions to icon components. Use with fileIcons prop on TreeView.
Keys use dotted extensions.
const fileIcons = createFileIcons({
".tsx": FileCode,
".json": FileJson,
".md": FileText,
});
For a complete list of props, see the Ark UI documentation.
[
Previous page
Tour ](/docs/components/tour)[
Next page
Client Only ](/docs/utilities/client-only)
On This Page
InstallationAnatomyUsageControlledExamplesMultiple selectionWith Context MenuRenaming nodesWith checkboxesLinksMini EditorCustom iconsFolder iconsSingle item iconWith file extensionsAPI ReferenceTreeViewTreeViewLabelTreeViewTreeTreeViewNodeTreeViewBranchTreeViewBranchItemTreeViewBranchContentTreeViewContentTreeViewItemTreeViewCheckboxcreateTreeCollectioncreateFileIcons