Skip to content
nexubDesign System
v11.9.0

Components · Navigation

Tree View

BetaAdded v6.3.0

A keyboard-friendly hierarchy (ARIA tree) with lazy children and tri-state checkboxes.

import { TreeView } from '@nexub/react';
View as Markdown

Playground

Props

  • Engineering
    • Web
      • Ada Lovelace
      • Alan Turing

Selected: ada

TSX
<TreeView aria-label="Teams" nodes={nodes} defaultExpanded={['eng', 'web']} selected={selected} onSelectedChange={setSelected} onLoadChildren={load} />

Examples

Lazy loading and selection

  • Engineering
    • Web
      • Ada Lovelace
      • Alan Turing

Selected: ada

Checkboxes

  • Reports
    • Revenue
    • Churn
  • Settings
    • Billing
    • Members

Granted: revenue

Opened: none (press Enter or double-click)

States

    • Engineering
      • Web
        • Ada Lovelace
        • Alan Turing

    Selected: ada

    default
  • Selected: ada

    loading

Props

Props
PropTypeDefaultDescription
nodesrequiredTreeNode[]—{ id, label, textValue?, icon?, children?, hasChildren?, disabled?, trailing?, href? }. trailing (11.5) is meta at the row end, e.g. a count or a date. href (11.5) makes the row a link: click and Enter follow it, Cmd/Ctrl-click and middle-click open a new tab.
aria-labelstring—Names the tree.
aria-labelledbystring—Names the tree.
expandedstring[]—Open node ids.
defaultExpandedstring[]—Open node ids.
onExpandedChange((ids: string[]) => void)—Open node ids.
selectedstring—Single selection, set by click, Enter or Space.
defaultSelectedstring—Single selection, set by click, Enter or Space.
onSelectedChange((id: string) => void)—Single selection, set by click, Enter or Space.
onActivate((node: TreeNode) => void)—11.5. Opens a node: Enter or double-click. Separate from selection, which click and Space set (Enter selects too). Disabled nodes never activate.
renderActions((node: TreeNode) => React.ReactNode)—11.5. Row actions, shown on hover and focus and always on touch screens. Their space is always reserved, so rows never shift. Tab from the focused row reaches them; the item is named by its label and trailing only.
refReact.Ref<TreeViewHandle>—11.5. scrollToNode(id, { focus? }) opens the node's ancestors, then scrolls its row into view (and focuses it).
checkablebooleanfalseCheckboxes instead of selection. A parent is checked when all its children are and mixed when some are.
checkedstring[]—Every checked id, parents included.
defaultCheckedstring[]—Every checked id, parents included.
onCheckedChange((ids: string[]) => void)—Every checked id, parents included.
onLoadChildren((node: TreeNode) => Promise<unknown>)—Called when a hasChildren node without children opens. Load them and pass the new nodes; the item is aria-busy meanwhile.
classNamestring—
emptyStateReact.ReactNode—Shown when there is no data. Defaults to the "no data" message.
errorStateReact.ReactNode—Shown in place of the data when a load failed, for example an ErrorState with a retry action. Wins over loading and empty.
loadingboolean—Shows a skeleton in place of the data and sets aria-busy.

Accessibility

ARIA roles

treetreeitem with aria-level, aria-setsize, aria-posinset, aria-expandedgroup

Keyboard

  • ArrowDown / ArrowUp

    Next or previous visible item.

  • ArrowRight

    Opens a closed item, or moves to its first child. Mirrored in RTL.

  • ArrowLeft

    Closes an open item, or moves to its parent. Mirrored in RTL.

  • Home / End

    First or last visible item.

  • Enter

    Selects the item (or toggles its checkbox), calls `onActivate` and follows a link node.

  • Space

    Selects the item, or toggles its checkbox.

  • Tab

    From the focused item to its row actions (`renderActions`), then out of the tree.

  • Type a letter

    Moves to the next item starting with the typed text.

Focus

  • The tree is one tab stop (roving tabindex); it returns to the last focused or the selected item.