Loading component…
Loading component…
A nested list of folders and files or any hierarchy
To reuse this local React demo, copy all files from the Code tab into the same folder, then import TreeView from ./tree-view.
import { useState } from "react";
import { TreeView, type TreeNode } from "@/components/ui/tree-view";
const files: TreeNode[] = [
{
id: "prompts",
label: "prompts",
icon: <FolderIcon />,
openIcon: <FolderOpenIcon />,
children: [
{ id: "prompts/system", label: "system.md", icon: <FileIcon /> },
{ id: "prompts/tools", label: "tools.md", icon: <FileIcon /> },
],
},
{ id: "agent", label: "agent.yaml", icon: <CodeIcon />, meta: "2 KB" },
];
export function Workspace({ onOpen }: { onOpen: (id: string) => void }) {
const [selected, setSelected] = useState<string | null>(null);
return (
<TreeView
aria-label="Workspace"
items={files}
defaultExpanded={["prompts"]}
selected={selected}
onSelect={(id, node) => {
setSelected(id);
if (!node.children) onOpen(id);
}}
/>
);
}| Name | Type | Default | Description |
|---|---|---|---|
| items | TreeNode[] | { id, label, icon?, openIcon?, meta?, disabled?, children? } – ids must be unique in the whole tree; any children list (even an empty one) makes a branch | |
| expanded | string[] | Controlled: the ids of the open branches | |
| defaultExpanded | string[] | [] | Uncontrolled: the branches open at the start |
| onExpandedChange | (expanded: string[]) => void | Called with the new list of open ids when a branch opens or closes | |
| selected | string \| null | Controlled: the id of the selected row | |
| defaultSelected | string \| null | null | Uncontrolled: the row selected at the start |
| onSelect | (id: string, node: TreeNode) => void | A row was picked – a click, Enter or Space; folders too (a click on a folder also opens or closes it, its chevron only opens or closes) | |
| guides | boolean | true | Thin vertical lines under each open branch; the one around the selected row is darker |
| emptyText | string | "Empty" | Shown inside an open branch without children |
| aria-label | string | Names the tree – or use aria-labelledby | |
| className | string | Extra class on the root <ul>; give a parent a height and overflow: auto to scroll a long tree |
One line per row: long names end in … with the full name on hover; meta takes at most 45% of the row
Deep levels indent 18px each, but never past 45% of the width – names stay readable in a narrow panel
Touch screens get rows at least 40px tall
Right-to-left: the indent, guides and chevron flip, and ← / → swap
Dark mode built in: switches when an ancestor (usually <html>) has the class dark or data-theme="dark"
@import "tailwindcss"; in your main CSS file.