Skip to content

TreeView

A hierarchical list of items where nested items can be expanded and collapsed.
  • Draft
  • Review pending by accessibility team

Examples

With controlled expanded state

With stateful visuals

To render stateful visuals, pass a render function to TreeView.LeadingVisual or TreeView.TrailingVisual. The function will be called with the expanded state of the item.

Since stateful directory icons are a common use case for TreeView, we provide a TreeView.DirectoryIcon component for convenience. The previous example can be rewritten as:

With asynchronously loaded items

See Storybook for examples with asynchronously loaded items.

Props

TreeView

NameTypeDefaultDescription
children Required
React.ReactNode

TreeView.Item

NameTypeDefaultDescription
children Required
React.ReactNode
current
boolean
falseWhether the item is the current item. No more than one item should be current at once. The path to the current item will be expanded by default.
defaultExpanded
boolean
The expanded state of the item when it is initially rendered. Use when you do not need to control the state.
expanded
boolean
The controlled expanded state of item. Must be used in conjunction with onExpandedChange.
onExpandedChange
(expanded: boolean) => void
Event handler called when the expanded state of the item changes.
onSelect
(event: React.MouseEvent<HTMLElement> | React.KeyboardEvent<HTMLElement>) => void
ref
React.RefObject<HTMLElement>
A ref to the element rendered by this component.

TreeView.LinkItem

NameTypeDefaultDescription
children Required
React.ReactNode
href
string
The URL that the item navigates to. href is passed to the underlying <a> element. If as is specified, the component may need different props. If the item contains a sub-nav, the item is rendered as a <button> and href is ignored.
current
boolean
falseWhether the item is the current item. No more than one item should be current at once. The path to the current item will be expanded by default.
defaultExpanded
boolean
The expanded state of the item when it is initially rendered. Use when you do not need to control its state.
expanded
boolean
The controlled expanded state of item. Must be used in conjunction with onExpandedChange.
onExpandedChange
(expanded: boolean) => void
Event handler called when the expanded state of the item changes.
onSelect
(event: React.MouseEvent<HTMLElement> | React.KeyboardEvent<HTMLElement>) => void
ref
React.RefObject<HTMLElement>
A ref to the element rendered by this component.

TreeView.SubTree

NameTypeDefaultDescription
children
React.ReactNode
state
| 'initial' 
| 'loading'
| 'done'
| 'error'

Specify a state if items in the subtree are loaded asynchronously.

An asynchronous subtree can be in one of the following states:

  • initial: Items are neither loaded nor loading.
  • loading: Currently loading items. The subtree will render a loading indicator when in this state (after a 300ms delay).
  • done: Done loading items. Screen readers will announce when a subtree enters this state.
  • error: An error occured while loading items.

TreeView.LeadingVisual

NameTypeDefaultDescription
children
| React.ReactNode
| (props: {isExpanded: boolean}) => React.ReactNode
label
string
Provide an accessible label for the leading visual. This is not necessary for decorative visuals

TreeView.TrailingVisual

NameTypeDefaultDescription
children
| React.ReactNode
| (props: {isExpanded: boolean}) => React.ReactNode
label
string
Provide an accessible label for the trailing visual. This is not necessary for decorative visuals

TreeView.DirectoryIcon

NameTypeDefaultDescription

Status

Alpha

  • Component props are documented on primer.style/react.
  • Component does not have any unnecessary third-party dependencies.
  • Component can adapt to different themes.
  • Component can adapt to different screen sizes.
  • Component has 100% test coverage.

Beta

  • Component is used in a production application.
  • Common usage examples are documented on primer.style/react.
  • Common usage examples are documented in storybook stories.
  • Component has been reviewed by a systems designer and any resulting issues have been addressed.
  • Component has been manually reviewed by the accessibility team and any resulting issues have been addressed.

Stable

  • Component API has been stable with no breaking changes for at least one month.
  • Feedback on API usability has been sought from developers using the component and any resulting issues have been addressed.
  • Component has corresponding design guidelines documented in the interface guidelines.
  • Component has corresponding Figma component in the Primer Web library.
Edit this page on GitHub
2 contributorscolebemisjoshblack
Last edited by colebemis on October 18, 2022