Skip to Content

React Sortable Tree Component

Reorder nested React tree data with accessible keyboard, pointer, and touch interactions.

Use SortableTree for editors, file managers, and navigation structures where people need to change the order or nesting of nodes. It is controlled: persist the SortableTreeMove from onMove, or use onItemsChange when the component should provide the next immutable tree value. For read-only browsing, use TreeView instead.

Stable API maturity: Stable.

Import

import {
  SortableTree,
  type SortableTreeItem,
  type SortableTreeMove,
} from "@puzzle-ui/ui";

Nested editing

The drag handle is a native button, so it remains keyboard reachable and does not make the entire row draggable. Use Space to lift the focused item, Arrow Up or Arrow Down to choose a sibling destination, Space to drop, and Escape to cancel. Pointer and long-press touch dragging use the same handle.

390 × 844

Selection, expansion, and custom content

Expansion and selection follow the same controlled conventions as TreeView. Use renderItem, renderIcon, renderLabel, or renderActions for application-specific content. Action content should be independently interactive; the tree ignores its clicks so an action does not select or move the row.

<SortableTree
  aria-label="Project files"
  items={items}
  selectionMode="multiple"
  selectedIds={selectedIds}
  onSelectionChange={setSelectedIds}
  expandedIds={expandedIds}
  onExpandedChange={setExpandedIds}
  renderActions={(item) => (
    <button type="button" onClick={() => rename(item.id)}>Rename</button>
  )}
/>

Safe moves

SortableTree rejects drops onto a node's own descendant, disabled nodes, and destinations deeper than maxDepth. Use canMove for domain-specific rules. onMove receives the source ID, destination parent, child index, target ID, and relative position ("before", "inside", or "after"). The move callback is also where an application can update a server or history stack.

The tree announces lift, movement, invalid destinations, cancellation, and drop through a polite live region. Keep the accessible tree name and the drag handle labels meaningful when labels are rendered as rich content.

API

PropTypeDescription
itemsSortableTreeItem<TData>[]Controlled nested nodes.
onMove(move: SortableTreeMove) => voidReceives a valid keyboard or pointer move.
onItemsChange(items: SortableTreeItem<TData>[]) => voidReceives the immutable result of a valid move.
maxDepthnumberMaximum resulting depth, including descendants.
canMove(move: SortableTreeMove) => booleanOptional domain-specific move guard.
expandedIds, defaultExpandedIdsstring[]Controlled or initial expanded nodes.
selectedIds, defaultSelectedIdsstring[]Controlled or initial selection.
selectionMode"none" | "single" | "multiple"Selection behavior.
renderItem, renderIcon, renderLabel, renderActionsrender callbacksCustomize node content and actions.
size, indentation"sm" | "md", string | numberDensity and hierarchy spacing.

SortableTreeItem requires a stable id and label; children, icon, actions, disabled, selectable, textValue, and data are optional.