Skip to main content
ninna-ui

Tree

Hierarchical tree view for displaying nested data.

import { Tree } from "@ninna-ui/data-display"
pnpm add @ninna-ui/data-display

Requires @ninna-ui/core (auto-installed) + CSS setup.

View source

Usage

Import and use the Tree component in your application.

import { Tree } from "@ninna-ui/data-display";
const data = [
{ id: "1", label: "Folder", children: [
{ id: "1-1", label: "File.tsx" },
]},
];
export default function Example() {
return <Tree data={data} />;
}

Basic

A basic file tree with folders and files.

src
components
Button.tsx
Input.tsx
App.tsx
index.ts
package.json
tsconfig.json

With Selection

Track and highlight selected nodes.

src
components
Button.tsx
Input.tsx
Modal.tsx
hooks
useState.ts
useEffect.ts
App.tsx
package.json

Selected: button

Collapsed by Default

Start with all nodes collapsed.

package.json

API Reference

Complete list of props for the Tree component.

Tree

PropTypeDefaultDescription
aria-labelstring-Accessible label for the tree widget (use when no visible label exists)
aria-labelledbystring-ID of an element that labels the tree
data*TreeNode[]-Tree data structure
selectedIdstring-Currently selected node id
onSelect(id: string) => void-Callback when a node is selected
defaultExpandedIdsstring[]-Initially expanded node ids
showLinesbooleantrueShow connecting lines between nodes
showIconsbooleantrueShow folder/file icons
classNamestring-Additional CSS classes

TreeNode

PropTypeDefaultDescription
id*string-Unique node identifier
label*string-Display label for the node
childrenTreeNode[]-Child nodes (makes this a folder)
iconReactNode-Custom icon for the node

Accessibility

  • Uses role="tree" and role="treeitem" ARIA roles
  • Supports aria-label and aria-labelledby props for labeling
  • Arrow keys navigate between visible nodes
  • Right arrow expands a collapsed folder, Left arrow collapses
  • Enter/Space selects the focused node
  • Expanded state is announced via aria-expanded