Nav Tree
NavTree shows a site's navigation as sections that open and close, with a link for each page.
Use a NavTree for navigation that is grouped into sections, such as an app's sidebar or a list of documentation chapters. The sidebar on this site is one. When pressing a row should select it rather than go somewhere, use a Tree.
A NavTree takes the same data and draws the same indent and disclosure arrow as a Tree. A node with children is a section, which is a button that opens and closes its pages. A node with href is a page, which is a real link. A node with both is a section that is also a page: its name is the link, and its arrow opens and closes it. A node with neither is plain text, unless you provide renderLink.
Set currentId to the id of the page that is open, and that row is highlighted and marked with aria-current="page". To render each page as your router's link component, use renderLink.
Rows are size 2 by default, and each row is as tall as a Button at the same size. One level of indent is the width of one icon, so it follows size too.
The NavTree uses ordinary keyboard navigation: Tab moves between sections and pages, Enter or Space opens and closes a section, and Enter follows a link. Because it renders a plain container, put it inside a <nav> element with an aria-label.
Props
Generated from the types. It lists what this component DECLARES: a prop that arrives from a shared type or from the platform is real and is not repeated here.
| Prop | Type | What it does |
|---|---|---|
currentId? | string | null | The id of the node for the current page. |
defaultExpandedIds? | readonly string[] | Uncontrolled starting expansion. |
expandedIds? | readonly string[] | Controlled expansion, paired with onExpandedChange. |
items | readonly TreeNode[] | The hierarchy, as data. |
onExpandedChange? | (ids: string[]) => void | Fires when a section opens or closes, with the whole expanded set. |
renderLink? | (node: TreeNode) => RenderElement | Renders each leaf as your router's link, for example renderLink={(node) => <Link href={node.href!} />}. |
size? | "1" | "2" | "3" | "4" | Sets the size step of the rows. |
It also takes every prop of a <div>.
role="tree"— A tree role tells screen readers to expect selection and arrow-key navigation, which navigation does not have. Sections are buttons and pages are links instead.- Selection — Navigation has a current page rather than a selection. If people choose rows instead of visiting them, use a Tree.
- Arrow-key navigation — The NavTree holds ordinary links, so Tab moves between them and Enter follows them, as in any other navigation.
- An
indentprop — One level of indent is always one icon wide at the current size, so the indent followssize.