Tree View

A Tree View is a hierarchical structure that provides nested levels of navigation.

Import#

import { TreeView } from '@volue/wave-react';
// you can also access TreeView's context via hook:
// import { useTreeViewContext } from '@volue/wave-react';

TreeView is a compound component that consists of multiple parts:

  • ​
    TreeView.Root: The wrapper that contains all the parts of a Tree View and provides context for its children.
  • ​
    TreeView.Item: A single item of the tree.
  • ​
    TreeView.CheckboxItem: An item with a checkbox, for trees where any number of items can be selected.
  • ​
    TreeView.SubTree: The wrapper for the items that should be nested under a parent item.

Examples#

Basic#

TreeView is best used to organise large amounts of information that can nest within multiple levels. Tree items with sub trees create a parent-child relationship and automatically use a chevron to indicate the ability to expand and collapse child items. As the tree hierarchy increases nesting levels, the size of the indentation is increased as well.

TreeView works in an uncontrolled way by default, meaning that each instance of TreeView.Item containing TreeView.Subtree is responsible for managing its own expanded state internally.

By default every subtree is collapsed. You can set defaultIsExpanded to true on the parent TreeView.Item to make it expanded by default.

If a user expands nested parent items and then collapses a parent item higher in the hierarchy, the expansion state of the parent items lower in the hierarchy is preserved.

Controlled#

The expansion state of each subtree can be controlled via isExpanded prop on its parent TreeView.Item and state changes can be listened via onIsExpandedChange prop, allowing you to sync state.

Sizes#

TreeView component comes in two sizes: medium and small. By default, it uses medium size. Use the size prop to control the size.

Active item#

TreeView.Item components can be in active state, that is indicated by a different background colour. Use isActive prop to set the active state.

No more than one item should be active at once. When a child item is active, all of it's parent items should be expanded.

Disabled item#

isDisabled shows the item as disabled and ignores onSelect and, on a TreeView.CheckboxItem, the checkbox. The item stays focusable, and its subtree can still be expanded with the chevron or arrow keys.

Selecting items#

Use onSelect prop to enable item selection. onSelect handler of TreeView.Item is called when an item is selected using either the mouse or keyboard. It can be used to track currently selected item. This is single selection, usually paired with isActive. For checkboxes, see Multiple selection.

Multiple selection#

Use TreeView.CheckboxItem to render a checkbox in the item, controlled through isChecked and onIsCheckedChange (or defaultIsChecked). Each item reflects only its own props: the tree neither derives a parent's state from its children nor decides what the selection contains. Set isIndeterminate on a partially checked parent and isDisabled on items implied by an ancestor. Toggling an indeterminate item calls onIsCheckedChange with the opposite of isChecked, so a mixed parent becomes checked. The tree gets aria-multiselectable once it contains a checkbox item. For a single current item, see Selecting items.

The row, the checkbox and Space all toggle the checkbox. The chevron only expands and collapses.

With onSelect, the row and Enter call it instead of toggling the checkbox, for example to open the item while the checkboxes collect items for a bulk action. Mark the open item with isActive.

See the implied descendants example in Storybook

Decorators#

Use startElement and endElement props to add custom elements to the start and end of an item.

These are useful for adding a Label to the item or providing an Icon that visually represents and supports the item.

Using a folder icon for parent items and a document icon for leaf items is a commonly recognised pairing of icons used in Tree View structures.

Accessibility#

Tree View implements the WAI-ARIA Tree View design pattern​.

  • ​

    All relevant ARIA attributes are automatically managed.

  • ​

    Tree View can be focused with Tab or Shift + Tab. Tab moves focus outside of the Tree View to the next focusable node.

  • ​

    After focusing the Tree View with keyboard, focus is set on the previously active item or on the first item if none of the items were active.

  • ​

    Focus movement is managed using roving focus technique​. ↓ and ↑ keys move the focus among focusable Tree View items without opening or closing them.

  • ​
    Home and Page Up moves focus to the first item without opening or closing it.
  • ​
    End and Page Down moves focus to the last item without expanding any nodes that are closed.
  • ​
    Enter performs the default action (triggering `onSelect` event when specified, toggling the selection of a `TreeView.CheckboxItem`, or toggling expansion) for the focused item.
  • ​
    Space toggles the selection of a focused `TreeView.CheckboxItem`.
  • ​

    Clicking the chevron only expands or collapses the node. Clicking anywhere else will perform the default action on the item.


API reference#

TreeView.Root#

In addition to the props below, you can pass margin props to control the spacing.

Prop
Type
Default
css
StitchesCss
No default value
aria-label*
string
No default value
size
enum
"medium"

TreeView.Item#

In addition to the props below, you can pass margin props to control the spacing.

Prop
Type
Default
css
StitchesCss
No default value
id*
string
No default value
defaultIsExpanded
boolean
No default value
isExpanded
boolean
false
onIsExpandedChange
function
No default value
isActive
boolean
false
onSelect
function
No default value
isDisabled
boolean
false
startElement
React.ReactElement
No default value
endElement
React.ReactElement
No default value

TreeView.CheckboxItem#

TreeView.CheckboxItem accepts all TreeView.Item props plus the ones below. With onSelect, the row calls it instead of toggling the checkbox, see Multiple selection.

Prop
Type
Default
isChecked
boolean
false
defaultIsChecked
boolean
No default value
onIsCheckedChange
function
No default value
isIndeterminate
boolean
false

TreeView.SubTree#

In addition to the props below, you can pass margin props to control the spacing.

Prop
Type
Default
css
StitchesCss
No default value