Tree
Nested, expandable rows for a sidebar, with arrow-key navigation and optional drag and drop. Built from sidebar rows.
Usage
import { Tree, TreeItem } from 'nyctoui/tree'
<Tree aria-label="Folders" defaultExpanded={['design']}>
<TreeItem id="design" label="Design" icon={<IconFolder size={16} />}>
<TreeItem id="brand" label="Brand" />
</TreeItem>
</Tree>Which items are open lives on the Tree, not on each item, so it survives items remounting and can be saved or set from the current route.
Good for
- A file tree in a code editor or a cloud drive.
- Pages and subpages in a notes or a wiki app, with drag to reorder.
- A folder list in an email client.
- Categories and subcategories in a store admin.
Accessibility
The tree follows the WAI-ARIA tree view pattern. The list is a tree and each row a treeitem with aria-level, aria-posinset and aria-setsize, so a screen reader says how deep a row is and where it sits. Branches add aria-expanded and own their group of children. Give the tree an aria-label.
The whole tree is one tab stop: Tab lands on the current page, or the row focused last, or the first row. The chevron and the drag handle are for the pointer only; the keys below do the same things. A row’s actions come next in the tab order, and only for that row.
Keyboard
- Up and Down move through the visible rows.
- Right opens a closed branch, or steps into an open one.
- Left closes an open branch, or steps out to its parent.
- Home and End jump to the first and last rows.
- * opens every branch beside the focused row.
- Typing jumps to the next row whose name starts with it.
- Enter opens or closes a row, or follows its link.
- In a tree with
onMove,Space picks the row up. Up and Down then step through every place it can go, Space or Enter drops it, and Esc puts it back.
Examples
Controlled
Pass expanded and onExpandedChange to own the open state.
Links and actions
With render, a row is a link: pressing it navigates and opens the branch, and the chevron closes it again. A parent with collapsible={false} always shows its children. actions appear on hover.
Drag and drop
Pass onMove and each row gets a drag handle ahead of its icon. A line shows where the item will land, and how far left the pointer sits picks the level; a ring means it goes inside that row. Resting on a closed folder opens it. An item can never be dropped into itself or anything beneath it.
The tree reports the move and leaves the data to you. moveTreeItem applies it to nested { id, children } data. Set droppable={false} on items that cannot hold others, and use canMove for any other rule.
Actions and drag and drop
Row actions sit at the trailing end and show on hover, while the drag handle leads, so the two never compete for the same spot. Here the plus adds a page inside the row and the menu duplicates or deletes it; the tree is controlled, so adding a page also opens its parent.
Best practices
- Give each item a clear label and an icon that shows its type, such as folder or file.
- Open the branches people need first with
defaultExpanded. - Check moves in
canMove, so items can only be dropped where they belong.
- Nest more than four or five levels. Flatten the structure or split it up.
- Use a tree for a flat list. Use a sidebar or a list.
API reference
Tree
A nested list of rows with the tree role. Give it an aria-label.
| Prop | Type | Default |
|---|---|---|
expandedIds of the expanded items, when controlled. | ReadonlyArray<string> | — |
defaultExpandedIds expanded on first render, when uncontrolled. | ReadonlyArray<string> | — |
onExpandedChangeCalled when an item opens or closes. | (expanded: string[]) => void | — |
onMoveCalled with { id, parentId, index } when an item is dropped somewhere new. Passing it turns dragging on. | (move: TreeMove) => void | — |
canMoveReturn false to refuse a drop. | (move: TreeMove) => boolean | — |
sxStyleX styles merged after the built-in ones. | StyleXStyles | — |
TreeItem
One row and its nested items.
| Prop | Type | Default |
|---|---|---|
idUnique within the tree; identifies the item in expanded. | string | — |
labelThe row’s text. | ReactNode | — |
textValuePlain-text name for typeahead and move announcements, when label is not a string. | string | — |
iconIcon before the label. | ReactNode | — |
expandedIconShown instead of icon while the item is expanded. | ReactNode | — |
renderMakes the row a link. Without it the row toggles the item. | ReactElement | function | — |
activeMarks the row as the current page. | boolean | — |
actionsTrailing controls, shown on hover or focus. | ReactNode | — |
collapsiblefalse for a parent that always shows its children. | boolean | true |
draggablefalse to pin the item in place. Needs onMove on the tree. | boolean | true |
droppablefalse for an item that cannot take children, such as a file. | boolean | true |
onClickCalled when the row is pressed. | (event) => void | — |
childrenNested TreeItems. An item with children can expand. | ReactNode | — |
sxStyleX styles merged after the built-in ones. | StyleXStyles | — |