Skip to content

Tree

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

README.md
export const Default: Story = {
  render: (args) => <Tree {...args}>{files}</Tree>,
};

Default Expanded

src
index.ts
package.json
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

Documents
Work
Annual Report.docx
Presentation.pptx
README.md
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

Documents
Work
Annual Report.docx
Presentation.pptx
README.md
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

Documents
README.md

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

Documents
README.md
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

Local notes.md
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

Documents
Annual Report.docx
Presentation.pptx
README.md
export const RowActions: Story = {
  args: {
    'aria-label': 'Files with actions',
    defaultExpandedItems: ['docs'],
  },
  render: (args) => <Tree {...args}>{filesWithActions}</Tree>,
};

Treegrid Navigation

Documents
Annual Report.docx
Presentation.pptx
README.md
export const TreegridNavigation: Story = {
  args: {
    'aria-label': 'Files, grid navigation',
    navigationMode: 'treegrid',
    defaultExpandedItems: ['docs'],
  },
  render: (args) => <Tree {...args}>{filesWithActions}</Tree>,
};

Aside Content

Inbox12 unread
DraftsNew
Junk40
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

Website
Home page
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

Design
Kat SharpProduct designer
Luca MoraResearcher
Mia LopezContent designer
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

Documents
README.md
Documents
README.md
Documents
README.md
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

Documents
Work
Annual Report.docx
Presentation.pptx
README.md
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

Documents
README.md
Documents
README.md
Documents
README.md
Documents
README.md
Documents
README.md
Documents
README.md
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

Documents
Work
Personal
README.md
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

NameTypeRequiredDefaultDescription
expandedItemsreadonly string[]No

Controlled list of expanded item values.

defaultExpandedItemsreadonly string[]No[]

Item values that are expanded initially (uncontrolled).

onExpandedItemsChange(expandedItems: string[], details?: TreeOpenChangeDetails) => voidNo

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 onOpenChange(event, { open, value, openItems }); the open items are the first argument). An item with its own open is not in this list and does not call it.

onItemSelect(value: string) => voidNo

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.

selectedstring | nullNo

Value of the selected item. When set (also to null), every item carries aria-selected, and the selected item is where keyboard focus enters the tree. It has no effect with selectionMode="multiple" (a development warning): items carry aria-checked instead. With selectionMode="single" every item carries aria-selected even while it is not set (a development warning: the radios need it).

currentstring | nullNo

Value of the current item (for example the open page): it gets aria-current="true".

selectionMode'none' | 'single' | 'multiple'No'none'

How items are selected. 'none': activation only (0.7; selected highlights one item). 'single': a radio indicator on every item for selected/onItemSelect. 'multiple': a checkbox on every item, bound to checkedItems; checking an item checks its descendants, and a parent shows checked, mixed or unchecked from them. Fluent's 'multiselect' is 'multiple'.

checkedItemsreadonly string[]No

Controlled checked items (selectionMode="multiple"); see the cascade rule in the component docs.

defaultCheckedItemsreadonly string[]No[]

Initially checked items (uncontrolled).

onCheckedItemsChange(checkedItems: string[], details: TreeCheckedItemsChangeDetails) => voidNo

Called with every checked item (in tree order) when a check changes (Fluent's onCheckedChange). WaveUI always passes details.

size'small' | 'medium'No'medium'

Size of the rows (Fluent UI's Tree size): small rows are 24px high, medium 28px. Rendered as data-size on the tree.

independentChecksbooleanNofalse

With selectionMode="multiple", makes the checks independent: checking an item checks only that item, and a parent is neither checked by its children nor mixed (the default cascades, Fluent UI's behaviour). checkedItems then lists exactly the items checked. Rendered as data-independent-checks on the tree.

appearance'subtle' | 'subtle-alpha' | 'transparent'No'subtle'

Look of the rows (Fluent UI's Tree appearance): subtle fills a hovered or selected row with the subtle colors, subtle-alpha with translucent versions of them, so a surface behind the tree shows through, and transparent draws no fill at all: a selected row then shows by its bold text (and its outline in forced colors). Rendered as data-appearance on the tree.

navigationMode'tree' | 'treegrid'No'tree'

'tree': Tab from a focused item enters its actions. 'treegrid': the "next" arrow (ArrowRight, ArrowLeft in RTL) moves into an item's actions and "previous" returns; on an expanded parent "next" no longer moves to the first child (ArrowDown does). 'treegrid' changes the keyboard only: the roles stay tree and treeitem (Fluent's navigationMode), because a treegrid's rows need one flat list with aria-level. For real role="treegrid" rows and gridcells use FlatTree with navigationMode="treegrid".

motionMotionSlot | nullNothe collapse described above

Replaces the collapse of every child group in the tree (Fluent's Tree collapseMotion), on the element around each role="group" element: by default the space a group takes opens from nothing and closes back to it, with a fade, over the normal duration (200 ms) with the max easy-ease curve, both ways. null turns it off (a group appears and disappears at once); a class string or { className } replaces it, written as MotionSlot describes. A WaveProvider with motion="none" turns the default off, and a value here still applies under it. The expand chevron's turn is not part of it, and a nested Tree takes its own.

refReact.Ref<HTMLDivElement>No

Ref to the role="tree" element.

Any other prop is passed to the element it renders (HTMLDivElement).

Parts

  • Tree.Item
  • Tree.ItemPersonaLayout

Stable class names

PartClass
rootwave-tree