Tree
A hierarchical list (WAI-ARIA Tree View pattern) with a single tab stop: Up/Down move between visible items, Home/End to the first/last, Right expands or moves to the first child (or into the row's actions, see below), Left collapses or moves to the parent (both mirrored in RTL), Enter activates (and toggles a parent), and so does Space unless the tree has checkboxes, * expands the siblings, and typing jumps to an item by its text.
Keyboard focus enters at the selected item, else the first item; while focus is inside, the tab stop follows it, so Shift+Tab leaves the tree.
A child group opens and closes its space with a short collapse and fade (the Tree's motion; none under reduced motion or a WaveProvider with motion="none"), and focus inside a group that collapses moves to its item.
selectionMode="multiple" draws a checkbox on every item, bound to checkedItems: Space toggles the focused item's check, a click on the checkbox toggles it, and a click elsewhere on the row activates the item (as Enter does). Checks cascade: checking an item checks every item nested in it, and an item shows checked when all of its nested items are checked, mixed when some are. onCheckedItemsChange receives every checked item in tree order (fully checked parents included), followed by listed values that match no item, and details (value, checked, event).
Row content: actions (buttons at the end of the row, in a toolbar named by the item, shown on hover and focus, and always where there is no hover or colors are forced), aside (a count or a badge that describes the item), iconAfter and expandIcon. Tab from the focused item enters its actions and Shift+Tab returns. Keys and clicks on the actions never activate or check the item. With navigationMode="treegrid", Right (Left in right-to-left layouts) moves from an item into its actions and Left returns, the actions take no Tab stop, and on an open parent Right moves into its actions rather than to its first child (Down does). That mode changes the keyboard only: the roles stay tree and treeitem. For real treegrid, row and gridcell roles use FlatTree with navigationMode="treegrid".
Items are Tree.Item elements; nested items are written in their parent's children (directly, in Fragments, or returned by a render function), not rendered by a component of their own. Every item needs a value that is unique within the Tree. Tree.ItemPersonaLayout lays out a person (media, name and description) as an item's label.
Tree.Item and Tree.ItemPersonaLayout are also exported as TreeItem and TreeItemPersonaLayout for React Server Components, which cannot use the dotted form.
Examples
Default
export const Default: Story = {
render: (args) => <Tree {...args}>{files}</Tree>,
};Default Expanded
export const DefaultExpanded: Story = {
args: {
'aria-label': 'Source files',
defaultExpandedItems: ['src'],
},
render: (args) => (
<Tree {...args}>
<Tree.Item value="src">
src
<Tree.Item value="components">
components
<Tree.Item value="button">Button.tsx</Tree.Item>
<Tree.Item value="input">Input.tsx</Tree.Item>
</Tree.Item>
<Tree.Item value="index">index.ts</Tree.Item>
</Tree.Item>
<Tree.Item value="package">package.json</Tree.Item>
</Tree>
),
};With Icons
export const WithIcons: Story = {
args: {
'aria-label': 'Files with icons',
},
render: (args) => (
<Tree {...args}>
<Tree.Item value="folder" icon={{ children: '📁', style: { fontSize: 14 } }}>
Folder
<Tree.Item value="file1" icon={{ children: '📄', style: { fontSize: 14 } }}>
Document.txt
</Tree.Item>
<Tree.Item value="file2" icon={{ children: '📄', style: { fontSize: 14 } }}>
Notes.md
</Tree.Item>
</Tree.Item>
</Tree>
),
};Controlled Selection
export const ControlledSelection: Story = {
render: function ControlledTree(args) {
const [expanded, setExpanded] = React.useState<readonly string[]>(['docs', 'work']);
const [selected, setSelected] = React.useState<string | null>('report');
return (
<Tree
{...args}
expandedItems={expanded}
onExpandedItemsChange={(next) => {
setExpanded(next);
args.onExpandedItemsChange?.(next);
}}
selected={selected}
current={selected}
onItemSelect={(value) => {
setSelected(value);
args.onItemSelect?.(value);
}}
>
{files}
</Tree>
);
},
};Multi Select
export const MultiSelect: Story = {
args: {
'aria-label': 'Files to back up',
selectionMode: 'multiple',
defaultExpandedItems: ['docs', 'work'],
defaultCheckedItems: ['report'],
},
render: (args) => <Tree {...args}>{files}</Tree>,
};Controlled Checks
Checked: personal
export const ControlledChecks: Story = {
args: {
'aria-label': 'Files to share',
selectionMode: 'multiple',
defaultExpandedItems: ['docs'],
},
render: function ControlledChecksTree(args) {
const [checked, setChecked] = React.useState<string[]>(['personal']);
return (
<div>
<Tree
{...args}
checkedItems={checked}
onCheckedItemsChange={(next, details) => {
setChecked(next);
args.onCheckedItemsChange?.(next, details);
}}
>
{files}
</Tree>
<p className="mt-2 text-wave-caption-1 text-wave-muted-foreground">
Checked: {checked.length > 0 ? checked.join(', ') : 'none'}
</p>
</div>
);
},
};Single Select
export const SingleSelect: Story = {
args: {
'aria-label': 'Save to folder',
selectionMode: 'single',
defaultExpandedItems: ['docs'],
},
render: function SingleSelectTree(args) {
const [selected, setSelected] = React.useState<string | null>('work');
return (
<Tree
{...args}
selected={selected}
onItemSelect={(value) => {
setSelected(value);
args.onItemSelect?.(value);
}}
>
{files}
</Tree>
);
},
};Lazy Children
export const LazyChildren: Story = {
args: {
'aria-label': 'Remote files',
selectionMode: 'multiple',
},
render: function LazyChildrenTree(args) {
const [checked, setChecked] = React.useState<string[]>(['remote']);
const [expanded, setExpanded] = React.useState<string[]>([]);
const [loaded, setLoaded] = React.useState(false);
React.useEffect(() => {
if (loaded || !expanded.includes('remote')) return undefined;
const timer = window.setTimeout(() => {
setLoaded(true);
setChecked((current) => current.filter((value) => value !== 'remote-loading'));
}, 800);
return () => window.clearTimeout(timer);
}, [expanded, loaded]);
return (
<Tree
{...args}
expandedItems={expanded}
onExpandedItemsChange={(next) => {
setExpanded(next);
args.onExpandedItemsChange?.(next);
}}
checkedItems={checked}
onCheckedItemsChange={(next, details) => {
setChecked(next);
args.onCheckedItemsChange?.(next, details);
}}
>
<Tree.Item value="remote">
Remote drive
{loaded ? (
<>
<Tree.Item value="budget">Budget.xlsx</Tree.Item>
<Tree.Item value="minutes">Minutes.docx</Tree.Item>
</>
) : (
<Tree.Item value="remote-loading">Loading…</Tree.Item>
)}
</Tree.Item>
<Tree.Item value="local">Local notes.md</Tree.Item>
</Tree>
);
},
};Row Actions
export const RowActions: Story = {
args: {
'aria-label': 'Files with actions',
defaultExpandedItems: ['docs'],
},
render: (args) => <Tree {...args}>{filesWithActions}</Tree>,
};Treegrid Navigation
export const TreegridNavigation: Story = {
args: {
'aria-label': 'Files, grid navigation',
navigationMode: 'treegrid',
defaultExpandedItems: ['docs'],
},
render: (args) => <Tree {...args}>{filesWithActions}</Tree>,
};Aside Content
export const AsideContent: Story = {
args: {
'aria-label': 'Mail folders',
},
render: (args) => (
<Tree {...args}>
<Tree.Item value="inbox" aside="12 unread">
Inbox
</Tree.Item>
<Tree.Item value="drafts" aside={<Badge appearance="tint">New</Badge>}>
Drafts
</Tree.Item>
<Tree.Item
value="junk"
aside="40"
actions={
<Button size="small" appearance="subtle">
Empty
</Button>
}
>
Junk
</Tree.Item>
</Tree>
),
};Custom Expand Icon
export const CustomExpandIcon: Story = {
args: {
'aria-label': 'Projects',
defaultExpandedItems: ['web'],
},
render: (args) => (
<Tree {...args}>
<Tree.Item value="web" expandIcon={{ children: '📂', style: { fontSize: 12 } }}>
Website
<Tree.Item value="home">Home page</Tree.Item>
</Tree.Item>
<Tree.Item value="app" expandIcon={false}>
App
<Tree.Item value="login">Login screen</Tree.Item>
</Tree.Item>
</Tree>
),
};Persona Rows
export const PersonaRows: Story = {
args: {
'aria-label': 'Team',
defaultExpandedItems: ['design'],
},
render: (args) => (
<Tree {...args}>
<Tree.Item value="design">
Design
{team.map((person) => (
<Tree.Item key={person.id} value={person.id}>
<Tree.ItemPersonaLayout
media={<Avatar decorative name={person.name} size="small" />}
description={person.role}
>
{person.name}
</Tree.ItemPersonaLayout>
</Tree.Item>
))}
</Tree.Item>
</Tree>
),
};Motion
export const Motion: Story = {
args: {
defaultExpandedItems: ['docs'],
},
render: ({ 'aria-label': _label, ...args }) => (
<div className="flex flex-wrap gap-6">
{motionCases(slowCollapse).map(({ label, motion }) => (
// The story renders one tree per case; every other arg is forwarded.
<Tree key={label} {...args} aria-label={label} motion={motion}>
{files}
</Tree>
))}
</div>
),
};Independent Checks
export const IndependentChecks: Story = {
args: {
'aria-label': 'Independent checks',
selectionMode: 'multiple',
independentChecks: true,
defaultExpandedItems: ['docs', 'work'],
defaultCheckedItems: ['docs'],
},
render: (args) => <Tree {...args}>{files}</Tree>,
};Sizes And Appearances
export const SizesAndAppearances: Story = {
args: { selected: 'work', defaultExpandedItems: ['docs'] },
render: (args) => (
<div className="flex flex-wrap gap-6">
{(['subtle', 'subtle-alpha', 'transparent'] as const).flatMap((appearance) =>
(['small', 'medium'] as const).map((size) => (
<Tree
key={`${appearance}-${size}`}
{...args}
aria-label={`${appearance} ${size}`}
size={size}
appearance={appearance}
style={{ width: 200 }}
>
{files}
</Tree>
)),
)}
</div>
),
};Controlled Item Open
export const ControlledItemOpen: Story = {
render: function ControlledItemOpenTree(args) {
const [open, setOpen] = React.useState(true);
return (
<div className="flex flex-col gap-3">
<Button onClick={() => setOpen((value) => !value)}>
{open ? 'Collapse' : 'Expand'} Documents
</Button>
<Tree {...args}>
<Tree.Item value="docs" open={open} onOpenChange={(next) => setOpen(next)}>
Documents
<Tree.Item value="work">Work</Tree.Item>
<Tree.Item value="personal">Personal</Tree.Item>
</Tree.Item>
<Tree.Item value="readme">README.md</Tree.Item>
</Tree>
</div>
);
},
};Props
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
expandedItems | readonly string[] | No | Controlled list of expanded item values. | |
defaultExpandedItems | readonly string[] | No | [] | Item values that are expanded initially (uncontrolled). |
onExpandedItemsChange | (expandedItems: string[], details?: TreeOpenChangeDetails) => void | No | Called with the new list of expanded item values when an item expands or collapses, and with the item, its new state and the click or key press behind it (Fluent UI's | |
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. | |
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 | |
selectionMode | 'none' | 'single' | 'multiple' | No | 'none' | How items are selected. |
checkedItems | readonly string[] | No | Controlled checked items ( | |
defaultCheckedItems | readonly string[] | No | [] | Initially checked items (uncontrolled). |
onCheckedItemsChange | (checkedItems: string[], details: TreeCheckedItemsChangeDetails) => void | No | Called with every checked item (in tree order) when a check changes (Fluent's | |
size | 'small' | 'medium' | No | 'medium' | Size of the rows (Fluent UI's |
independentChecks | boolean | No | false | With |
appearance | 'subtle' | 'subtle-alpha' | 'transparent' | No | 'subtle' | Look of the rows (Fluent UI's |
navigationMode | 'tree' | 'treegrid' | No | 'tree' |
|
motion | MotionSlot | null | No | the collapse described above | Replaces the collapse of every child group in the tree (Fluent's |
ref | React.Ref<HTMLDivElement> | No | Ref to the |
Any other prop is passed to the element it renders (HTMLDivElement).
Parts
Tree.ItemTree.ItemPersonaLayout
Stable class names
| Part | Class |
|---|---|
root | wave-tree |