FlatTree
A tree built from flat data (WAI-ARIA Tree View pattern) that can render only the items in view, for trees of thousands of items. Its model comes from useFlatTree(items, options): items listed with a parentValue, in any order, the expanded and the checked state. The tree renders the visible items (the roots and the children of every expanded branch) as sibling role="treeitem" elements with aria-level, aria-setsize and aria-posinset from the data, aria-expanded on branches, and the nested Tree's chevron, checkbox and radio.
tsx const model = useFlatTree(items, { defaultExpandedItems: ['docs'] }); const virtualWindow = useVirtualWindow({ itemSize: 28 }); <FlatTree aria-label="Files" model={model} {...model.getTreeProps()} virtualWindow={virtualWindow} style={{ height: 400 }} />
Windowing: with virtualWindow the tree is the scrolling element (it needs a bounded height), renders the items in view plus the window's overscan and spacers for the others, and every item is exactly the window's itemSize tall. The server renders the first window. Keys move over the whole visible list, scrolling an item that is not rendered into view before focusing it, and the focused item stays rendered (and focused) while it is scrolled away.
Keyboard: a single tab stop, on the focused item while focus is inside, else on the selected item while it is visible, else on the first enabled item; Tab leaves the tree. Up/Down move between the visible items, Home/End to the first and last, Right expands a collapsed branch or moves to its first child, Left collapses an expanded branch or moves to the parent (both mirrored in right-to-left layouts), Enter activates (and toggles a branch), and so does Space unless the tree has checkboxes, * expands the sibling branches, and typing moves to the next visible item whose label starts with the typed text (items under a collapsed branch are not searched). The focused item stays the focused item (rendered, and the tab stop) while focus is in a popup that its content opened, such as a menu in a portal, so the popup can return focus to it.
navigationMode="treegrid" makes it a WAI-ARIA treegrid instead: role="treegrid", each item a role="row" (with aria-level, aria-posinset, aria-setsize, aria-rowindex and aria-expanded) whose content is in gridcells, with more cells from renderCells and the treegrid keys of APG (rows, cells, Enter into a cell's controls); the nested Tree's own navigationMode="treegrid" changes the keyboard only and keeps its tree roles. Default rendering is unchanged.
Selection, as the nested Tree: selected and onItemSelect (single selection and activation, with radios in selectionMode="single"), current, and with selectionMode="multiple" a checkbox on every item bound to the model's checkedItems (Space or a click on the checkbox toggles it, and checks cascade). An item whose data is disabled is aria-disabled, is skipped by the keys and typeahead, never holds the tab stop (except while a pointer press has focused it), and is neither activated, expanded nor checked by the user (a check of its parent still includes it).
When a branch collapses around the focused item (from code), focus moves to that branch, so it never falls to the page. Items expand and collapse at once, without motion: there is no child group to animate. A selected item under a collapsed branch is not the tab stop until it is visible again.
FlatTree.Item is the row the tree renders for each visible item (it is not written by hand); it is also exported as FlatTreeItem for React Server Components, which cannot use the dotted form.
Examples
Default
export const Default: Story = {
args: {
defaultExpandedItems: ['docs'],
},
};Checkable
export const Checkable: Story = {
args: {
'aria-label': 'Files to back up',
selectionMode: 'multiple',
defaultExpandedItems: ['docs', 'work'],
defaultCheckedItems: ['work'],
},
};Treegrid
export const Treegrid: Story = {
args: {
'aria-label': 'Files',
navigationMode: 'treegrid',
style: { width: 420 },
defaultExpandedItems: ['docs', 'work'],
renderCells: (row) => [
SIZES[row.item.value],
<Button
key="open"
size="small"
appearance="subtle"
aria-label={`Open ${row.item.label ?? row.item.value}`}
>
Open
</Button>,
],
slotProps: { cell: { className: 'min-w-20 justify-end' } },
},
argTypes: { renderCells: { control: false } },
};Five Thousand Nodes
Mounted items: 0 of 4,334 visible (5,000 in the tree)
export const FiveThousandNodes: Story = {
args: {
'aria-label': 'Archive',
items: FIVE_THOUSAND,
defaultExpandedItems: ['archive'],
className: 'h-100',
},
render: (args) => <FiveThousandTree {...args} />,
};Props
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
model | FlatTreeModel | Yes | The tree's model, from | |
virtualWindow | VirtualWindow | No | Renders only the items in view (and a few beyond), from The tree element is then no tab stop of its own while an item is ( | |
renderItem | (item: VisibleFlatItem & { checked?: CheckState }) => React.ReactNode | No | the item's `label`, else its `value` | The content of an item, from its row: the item data with its level, position, set size, expanded state and, in multiple mode, its check state. Called for the rendered items only. |
navigationMode | 'tree' | 'treegrid' | No | 'tree' | How the tree is built and moved through. The keys of a treegrid (APG): on a row, Down and Up move to the next and previous row, Home and End (also with Ctrl) to the first and last row, Right expands a collapsed branch, else moves into the row's first cell (it no longer moves to the first child: Down does), Left collapses an expanded branch (else it does nothing: the focus stays), and Enter, Space, |
renderCells | (item: VisibleFlatItem & { checked?: CheckState }) => readonly React.ReactNode[] | No | The content of a row's further cells, in order, in | |
selectionMode | 'none' | 'single' | 'multiple' | No | the model's mode (`model.getTreeProps().selectionMode`) | How items are selected, as the nested |
selected | string | null | No | Value of the selected item. When set (also to | |
current | string | null | No | Value of the current item (for example the open page): it gets | |
onItemSelect | (value: string) => void | No | Called with an item's value every time it is activated (a click on its row, Enter, or Space unless the tree has checkboxes), also when the same item is activated again. | |
ref | React.Ref<HTMLDivElement> | No | Ref to the | |
slotProps | { row?: SlotPart<'div'>; expandIcon?: SlotPart<'svg'>; indicator?: SlotPart<'span'>; label?: SlotPart<'span'>; cell?: SlotPart<'div'> } | No | Props for the elements every item draws, by part: each |
Any other prop is passed to the element it renders (HTMLDivElement).
Parts
rowexpandIconindicatorlabelcellFlatTree.Item
Stable class names
| Part | Class |
|---|---|
root | wave-flat-tree |
spacer | wave-flat-tree__spacer |