Tree
A collapsible, hierarchical tree component for organizing nested items like folders and files.
Installation
Usage
The new tree package uses a modern, optimized architecture that accepts flat data (arrays with parentId relationships) and handles tree construction internally.
Basic Example
import { Tree, useTree } from "@notion-kit/tree";
import { TreeList } from "@notion-kit/tree/presets";
const data = [
{ id: "1", parentId: null, title: "Folder 1" },
{ id: "1.a", parentId: "1", title: "File A" },
{ id: "2", parentId: null, title: "Folder 2" },
];
export default function MyTree() {
const tree = useTree(data, {});
return (
<Tree tree={tree}>
<TreeList nodeIds={tree.entity.rootIds} />
</Tree>
);
}Examples
Non-Collapsible Tree
Disable collapsing to show all nodes expanded by default.
Multi-Select
Enable selection of multiple tree items.
Show Empty Children
Display an indicator when a folder has no children.
Command Tree
A searchable tree interface for command palettes.
API Reference
useTree
Creates a tree instance from flat data.
const tree = useTree<T>(data, options);Parameters:
| Parameter | Type | Description |
|---|---|---|
data | T[] | Flat array of items with id and parentId |
options | TreeOptions | Configuration options for the tree |
TreeOptions:
| Option | Type | Default | Description |
|---|---|---|---|
collapsible | boolean | true | Whether nodes can be collapsed |
showEmptyChild | boolean | false | Show indicator for empty folders |
selectionMode | "single" | "multiple" | "single" | Selection behavior |
initialExpanded | string[] | [] | IDs of initially expanded nodes |
initialSelected | string[] | [] | IDs of initially selected nodes |
onSelectionChange | (id: string) => void | - | Called when selection changes |
Returns: TreeInstance<T>
The tree instance exposes:
entity: The tree entity withrootIds,nodes, etc.state: Current tree state (expanded, selected nodes)expand(id): Expand/collapse a nodeselect(id): Select a nodegetVisibleIds(): Get currently visible node IDs
Tree
Root container providing tree context.
| Prop | Type | Description |
|---|---|---|
tree* | TreeInstance<T> | Tree instance from useTree |
children | React.ReactNode | Tree content (typically Tree.List) |
className | string | Additional CSS classes |
Tree.Item
A slottable tree item.
Tree.ExpandIndicator
A slottable button to expand/collapse a node.
| Prop | Type | Description |
|---|---|---|
onToggle | () => void | A handler that is called when the node is toggled |
Tree.EmptyIndicator
A slottable indicator to show when a node has no children.
Tree.Group
A slottable tree group.
Tree.List
Renders tree nodes recursively.
| Prop | Type | Description |
|---|---|---|
nodeIds* | string[] | Array of node IDs to render |
renderItem | (props) => JSX | Custom renderer for tree items (optional) |
renderEmpty | (props) => JSX | Custom renderer for empty children (optional) |
TreeList (Preset)
Pre-styled tree list component from @notion-kit/tree/presets.
| Prop | Type | Description |
|---|---|---|
nodeIds* | string[] | Array of node IDs to render |
CommandTree (Preset)
Searchable tree component for command interfaces.
| Prop | Type | Description |
|---|---|---|
tree* | TreeInstance<T> | Tree instance from useTree |
type TreeItemData (Preset)
Base interface for tree item data.
interface TreeItemData {
id: string;
parentId: string | null;
title: string;
icon?: IconData;
group?: string | null;
}Architecture
The tree package uses an optimized O(N) architecture:
- Flat Data Input: Accepts arrays with
parentIdrelationships (e.g., from database queries) - Automatic Tree Building: Constructs tree structure internally via BFS traversal
- ID-Based Rendering: Uses node IDs for efficient recursive rendering
- Derived Visibility: Computes visible nodes on-demand via
getVisibleIds() - Smart State Management: Handles expand/collapse, selection via tree instance
This design eliminates the need for pre-structured tree data and provides better performance.