Files and folders
import { Tree } from "@senvenli/react/collection";
export default function Example() {
return (
<Tree
aria-label="Files"
defaultExpandedKeys={["src"]}
items={[
{
id: "src",
label: "src",
icon: "folder",
children: [
{ id: "components", label: "components", icon: "folder", children: [{ id: "button", label: "button.tsx", icon: "file-code" }] },
{ id: "index", label: "index.ts", icon: "file-code" },
],
},
{ id: "readme", label: "README.md", icon: "file-text" },
]}
/>
);
}import { Tree } from "@senvenli/react/collection";Guidelines
When to use it
- Data with parents and children people browse: files, categories, an org chart.
Use something else when
Do and avoid
- Keep labels short; three or four levels at most.
Accessibility
Built in
- Arrows move, Right opens and Left closes a node, Home and End jump; the chevron and Enter expand.
You provide
- An aria-label naming the tree.
API reference
Tree
| Prop | Type | Default | Description |
|---|---|---|---|
aria-labelRequired | string | – | Names the tree for screen readers ("Files", "Categories"). |
itemsRequired | readonly TreeNode[] | – | |
selectionMode | "none" | "single" | "multiple" | "none" |
|
selectedKeys | Iterable<Key> | – | |
defaultSelectedKeys | Iterable<Key> | – | |
onSelectionChange | (keys: Selection) => void | – | |
expandedKeys | Iterable<Key> | – | |
defaultExpandedKeys | Iterable<Key> | – | |
onExpandedChange | (keys: Set<Key>) => void | – | |
onAction | (key: Key) => void | – | What pressing a node does (open the file, go to the category). |
aria-labelRequiredstringNames the tree for screen readers ("Files", "Categories").
itemsRequiredreadonly TreeNode[]selectionMode"none" | "single" | "multiple"Default"none"none(default) for navigation withonAction,singleormultipleto pick nodes.selectedKeysIterable<Key>defaultSelectedKeysIterable<Key>onSelectionChange(keys: Selection) => voidexpandedKeysIterable<Key>defaultExpandedKeysIterable<Key>onExpandedChange(keys: Set<Key>) => voidonAction(key: Key) => voidWhat pressing a node does (open the file, go to the category).