Skip to Content
NarakUI

Tree

Hierarchical lists with expandable folders, keyboard navigation, selection, and drag-and-drop.

Basic tree

Expandable folders rendered from a flat tree.getItems() list. Folder rows with children get a chevron that expands and collapses them. Leaf rows are padded past the chevron column.

Marketing
Brand guidelines.pdf

Indentation

indent sets the width of each nesting level in pixels. The default is 16; this preview uses 24.

Marketing
Campaigns
Campaign brief.docx
Q1 report.pdf
Brand guidelines.pdf

Tree with selection

Selection state stays in the consumer. Folders expand via the chevron; leaf rows handle selection. Call event.preventDefault() in a row's onClick to skip the default click behavior.

Marketing
Campaigns
Campaign brief.docx
Q1 report.pdf
Brand guidelines.pdf

Tree with drag and drop

Enable dragAndDropFeature on the tree instance and render TreeDragLine inside the tree container. The container needs relative so the line lines up with the rows. Moving the data is up to the onDrop handler you pass to useTree; this preview shows the live drop indicator.

Marketing
Campaigns
Campaign brief.docx
Q1 report.pdf
Brand guidelines.pdf

Installation

pnpm dlx shadcn@latest add @narakui/tree

Add "@narakui": "https://ui.narakcode.dev/r/{name}.json" to components.json, or install directly from https://ui.narakcode.dev/r/tree.json.

Usage

TreeItemLabel reads the item from the surrounding TreeItem and shows item.getItemName() when it has no children.

import {
  hotkeysCoreFeature,
  syncDataLoaderFeature,
  Tree,
  TreeItem,
  TreeItemLabel,
  useTree,
} from '@/components/ui/tree';
const tree = useTree({
  rootItemId: 'root',
  getItemName: (item) => item.getItemData().name,
  isItemFolder: (item) => item.getItemData().isFolder,
  dataLoader: { getItem, getChildren },
  features: [syncDataLoaderFeature, hotkeysCoreFeature],
});

<Tree tree={tree} aria-label="Files">
  {tree.getItems().map((item) => (
    <TreeItem key={item.getId()} item={item}>
      <TreeItemLabel />
    </TreeItem>
  ))}
</Tree>;

Properties

Tree

PropTypeDefaultDescription
tree (Required)TreeInstance<T>—The tree instance returned by useTree.
indentnumber16Width of each nesting level in pixels.
showLinesbooleanfalsePositions rows relatively so a consumer can draw guide lines. It draws no lines itself.
toggleIconType'chevron' | 'plus-minus''chevron'Icon used by the folder toggle in TreeItemLabel.
aria-labelstring—Accessible label for the tree.
asChildbooleanfalseRender the child element instead of a div.
classNamestring—Additional CSS classes to apply to the tree container.

TreeItem

PropTypeDefaultDescription
item (Required)ItemInstance<T>—The tree item to render.
children (Required)ReactNode—Row content, usually a TreeItemLabel.
disabledbooleanfalseBlocks clicks and keyboard actions and removes the row from the tab order.
onToggleExpand(item: ItemInstance<T>) => void—Replaces the default expand and collapse for the toggle button and the left and right arrow keys.
onClick(event: MouseEvent<HTMLElement>) => void—Runs before the default click handler. Call preventDefault to skip it.
asChildbooleanfalseRender the child element instead of a div.
classNamestring—Additional CSS classes to apply to the row.

TreeItemLabel

PropTypeDefaultDescription
itemItemInstance<T>—The item to label. Defaults to the item of the surrounding TreeItem.
childrenReactNode—Label content. Defaults to item.getItemName().
onToggleClick(event: MouseEvent<HTMLButtonElement>) => void—Replaces the default expand and collapse when the folder toggle is clicked.
asChildbooleanfalseRender the child element instead of a span.
classNamestring—Additional CSS classes to apply to the label.

Other exports

ExportTypeDescription
TreeDragLinecomponentDrop position indicator. Renders nothing unless the tree uses dragAndDropFeature.
useTreehookBuilds a headless-tree compatible TreeInstance<T> with expansion, keyboard navigation, and optional drag-and-drop.
syncDataLoaderFeature, hotkeysCoreFeature, dragAndDropFeatureFeatureImplementationFeature tokens passed to useTree.