Skip to Content
Tree ALPHA

Tree

Hierarchical list of items that expand and collapse, such as pages or folders.

#

Documentation

#

Usage

Pass the items to options, each with its nested items in children. Pressing a row selects it, and pressing its chevron expands or collapses it. Give the tree an aria-label that names what it lists.

Website
Products
Features
Pricing
About us
Contact
Landing page
import { IconFileLines, IconFolder } from "@optiaxiom/icons"; import { Tree } from "@optiaxiom/react/unstable"; export function App() { return ( <Tree aria-label="Pages" defaultExpanded={["website", "products"]} defaultValue={["pricing"]} options={[ { children: [ { children: [ { icon: <IconFileLines />, label: "Features", value: "features", }, { icon: <IconFileLines />, label: "Pricing", value: "pricing", }, ], icon: <IconFolder />, label: "Products", value: "products", }, { icon: <IconFileLines />, label: "About us", value: "about", }, { icon: <IconFileLines />, label: "Contact", value: "contact", }, ], icon: <IconFolder />, label: "Website", value: "website", }, { icon: <IconFileLines />, label: "Landing page", value: "landing", }, ]} w="224" /> ); }

#

Structure

Tree renders its items from the options prop. Each item is a TreeOption and at minimum must contain the following two properties:

  • label
  • value

Values must be unique across the whole tree, since selection and expansion refer to items by their value. An item with children is a branch, and one without is a leaf.

#

Multiple selection

Enable the multiple prop to let users select several items. Each press toggles an item, Shift+click and Shift+Arrow extend the selection, and Ctrl/Cmd+A selects every visible item, or clears the selection when every item is selected.

Blog
Product launch
Roadmap update
Customer story
Newsletter
import { IconFileLines, IconFolder } from "@optiaxiom/icons"; import { Tree } from "@optiaxiom/react/unstable"; export function App() { return ( <Tree aria-label="Pages" defaultExpanded={["blog"]} defaultValue={["launch", "roadmap"]} multiple options={[ { children: [ { icon: <IconFileLines />, label: "Product launch", value: "launch", }, { icon: <IconFileLines />, label: "Roadmap update", value: "roadmap", }, { icon: <IconFileLines />, label: "Customer story", value: "story", }, ], icon: <IconFolder />, label: "Blog", value: "blog", }, { icon: <IconFileLines />, label: "Newsletter", value: "newsletter", }, ]} w="224" /> ); }

#

Controlled

The selection is always an array of values, even without multiple. Use value, defaultValue and onValueChange to control which items are selected, and expanded, defaultExpanded and onExpandedChange to control which branches are open.

Products
Features
Pricing
About us

Selected: features

Expanded: products

"use client"; import { IconFileLines, IconFolder } from "@optiaxiom/icons"; import { Group, Text } from "@optiaxiom/react"; import { Tree, type TreeOption } from "@optiaxiom/react/unstable"; import { useState } from "react"; const options: TreeOption[] = [ { children: [ { icon: <IconFileLines />, label: "Features", value: "features" }, { icon: <IconFileLines />, label: "Pricing", value: "pricing" }, ], icon: <IconFolder />, label: "Products", value: "products", }, { icon: <IconFileLines />, label: "About us", value: "about" }, ]; export function App() { const [expanded, setExpanded] = useState(["products"]); const [value, setValue] = useState(["features"]); return ( <Group alignItems="start" flexDirection="column" gap="16"> <Tree aria-label="Pages" expanded={expanded} onExpandedChange={setExpanded} onValueChange={setValue} options={options} value={value} w="224" /> <Text color="fg.secondary" fontSize="sm"> Selected: {value.join(", ") || "none"} </Text> <Text color="fg.secondary" fontSize="sm"> Expanded: {expanded.join(", ") || "none"} </Text> </Group> ); }

#

Content

Use the icon property to show an icon before the label, description to show secondary text after it, and detail to show a badge or count after that.

Blog
2
LaunchDraft
RoadmapScheduled
import { IconFileLines, IconFolder } from "@optiaxiom/icons"; import { Badge } from "@optiaxiom/react"; import { Tree } from "@optiaxiom/react/unstable"; export function App() { return ( <Tree aria-label="Pages" defaultExpanded={["blog"]} options={[ { children: [ { description: "Draft", icon: <IconFileLines />, label: "Launch", value: "launch", }, { description: "Scheduled", icon: <IconFileLines />, label: "Roadmap", value: "roadmap", }, ], detail: <Badge>2</Badge>, icon: <IconFolder />, label: "Blog", value: "blog", }, { children: [ { icon: <IconFileLines />, label: "Holiday sale", value: "sale", }, { icon: <IconFileLines />, label: "Year in review", value: "review", }, ], detail: <Badge>2</Badge>, icon: <IconFolder />, label: "Archive", value: "archive", }, ]} w="224" /> ); }

#

Disabled items

Use the disabledReason property to disable an item. Disabled items stay focusable so screen readers can find them, and show the reason in a tooltip. They can’t be selected, renamed or open their actions.

Website
About us
Careers
Contact
import { IconFileLines, IconFolder } from "@optiaxiom/icons"; import { Tree } from "@optiaxiom/react/unstable"; export function App() { return ( <Tree aria-label="Pages" defaultExpanded={["website"]} options={[ { children: [ { icon: <IconFileLines />, label: "About us", value: "about", }, { disabledReason: "You don't have access to this page", icon: <IconFileLines />, label: "Careers", value: "careers", }, { icon: <IconFileLines />, label: "Contact", value: "contact", }, ], icon: <IconFolder />, label: "Website", value: "website", }, ]} w="224" /> ); }

#

Lazy children

Pass a function returning a promise to children to load an item’s children when it first expands. The chevron shows a spinner while they load. The function runs only once, so pass an array instead for children that change after they load.

If the promise rejects, the item stays collapsed, and expanding it again calls the function once more. The tree shows nothing on failure; to tell users, show a toast or similar from your function and rethrow the error.

Newsletter
"use client"; import { IconFileLines, IconFolder } from "@optiaxiom/icons"; import { Tree, type TreeOption } from "@optiaxiom/react/unstable"; const fetchPages = async (): Promise<TreeOption[]> => { await new Promise((resolve) => setTimeout(resolve, 1000)); return [ { icon: <IconFileLines />, label: "Product launch", value: "launch", }, { icon: <IconFileLines />, label: "Roadmap update", value: "roadmap", }, ]; }; export function App() { return ( <Tree aria-label="Pages" options={[ { children: fetchPages, icon: <IconFolder />, label: "Blog", value: "blog", }, { icon: <IconFileLines />, label: "Newsletter", value: "newsletter", }, ]} w="224" /> ); }

#

Paging

To load a level a page at a time, return { nextPageLabel, nextPageParam, options } from the children function. The tree ends the level with an item labelled nextPageLabel, such as “Load more posts”, and pressing it calls the function again with that pageParam. Return no nextPageParam on the last page. The param can be a page index or a cursor, as with React Query’s getNextPageParam.

Focus moves to the first new item once the page loads. If a page’s promise rejects, that item stays in place, and pressing it again retries.

"use client"; import { IconFileLines, IconFolder } from "@optiaxiom/icons"; import { Tree, type TreeOption } from "@optiaxiom/react/unstable"; const posts = Array.from({ length: 8 }, (_, index) => `Post ${index + 1}`); const pageSize = 3; const fetchPosts = async ({ pageParam, }: { pageParam: number | string | undefined; }) => { await new Promise((resolve) => setTimeout(resolve, 600)); const page = typeof pageParam === "number" ? pageParam : 0; const items = posts.slice(page * pageSize, (page + 1) * pageSize); return { nextPageLabel: "Load more posts", nextPageParam: (page + 1) * pageSize < posts.length ? page + 1 : undefined, options: items.map((label): TreeOption => ({ icon: <IconFileLines />, label, value: label, })), }; }; export function App() { return ( <Tree aria-label="Pages" options={[ { children: fetchPosts, icon: <IconFolder />, label: "Blog", value: "blog", }, ]} w="224" /> ); }

#

Actions

Use the actions property to give an item a context menu. It takes the same options as Menu.

Pointer users open the menu from the ”…” button that shows on the hovered or selected row, or right-click the row. On touch screens the selected row shows the button too, and a long press of 500ms opens the menu. Keyboard users open it on the focused item with Shift+F10 or the Menu key; the button stays out of the tab order, since the item already announces its menu and shortcut. Focus returns to the item when the menu closes, unless an action moved focus elsewhere, such as into a dialog.

Website
About us
Pricing
Contact
"use client"; import { IconCopy, IconFileLines, IconFolder, IconTrashCan, } from "@optiaxiom/icons"; import { toaster } from "@optiaxiom/react"; import { Tree, type TreeOption } from "@optiaxiom/react/unstable"; const page = ( value: string, label: string, children?: TreeOption[], ): TreeOption => ({ actions: [ { addon: <IconCopy />, execute: () => toaster.create(`Duplicated ${label}`), label: "Duplicate", }, { addon: <IconTrashCan />, disabledReason: value === "website" ? "The site root can't be deleted" : undefined, execute: () => toaster.create(`Deleted ${label}`), group: { hidden: true, label: "Danger", separator: true }, intent: "danger", label: "Delete", }, ], children, icon: children ? <IconFolder /> : <IconFileLines />, label, value, }); export function App() { return ( <Tree aria-label="Pages" defaultExpanded={["website"]} options={[ page("website", "Website", [ page("about", "About us"), page("pricing", "Pricing"), page("contact", "Contact"), ]), ]} w="224" /> ); }

#

Renaming

Use the onLabelChange prop to let users rename items in place. Pressing F2 or double-clicking a row turns its label into a text field: Enter or leaving the field saves, and Escape cancels. Enter and Escape return focus to the item. An empty or unchanged label isn’t saved.

To start renaming from the context menu, pass actions a function: it receives rename, which an action can use as its execute. Set editable to false on items whose label can’t change, and leave out their Rename action.

Website
About us
Pricing
Contact
"use client"; import { IconFileLines, IconFolder, IconPen } from "@optiaxiom/icons"; import { Tree, type TreeOption } from "@optiaxiom/react/unstable"; import { useState } from "react"; type Page = { children?: Page[]; label: string; value: string }; const renamePage = (pages: Page[], value: string, label: string): Page[] => pages.map((page) => page.value === value ? { ...page, label } : { ...page, children: page.children && renamePage(page.children, value, label), }, ); export function App() { const [pages, setPages] = useState<Page[]>([ { children: [ { label: "About us", value: "about" }, { label: "Pricing", value: "pricing" }, { label: "Contact", value: "contact" }, ], label: "Website", value: "website", }, ]); const toOption = (page: Page): TreeOption => { // The site root keeps its name. const editable = page.value !== "website"; return { actions: editable ? ({ rename }) => [ { addon: <IconPen />, execute: rename, label: "Rename" }, ] : [], children: page.children?.map(toOption), editable, icon: page.children ? <IconFolder /> : <IconFileLines />, label: page.label, value: page.value, }; }; return ( <Tree aria-label="Pages" defaultExpanded={["website"]} onLabelChange={(value, label) => setPages((pages) => renamePage(pages, value, label)) } options={pages.map(toOption)} w="224" /> ); }

Menu

Dropdown menu for displaying actions.

Sidebar

Primary navigation menu for left side of the page, with support for branding and links.

#

Props

#

Tree

import { Tree } from "@optiaxiom/react/unstable";

Supports all Box props in addition to its own. Renders a <div> element.

Prop

asChild

Change the default rendered element for the one passed as a child, merging their props and behavior.

Read the Composition guide for more details.

false | true

className

string

defaultExpanded

The values of the items expanded at first, in uncontrolled mode.

string[]

defaultValue

The values of the items selected at first, in uncontrolled mode.

string[]

expanded

The values of the expanded items, in controlled mode.

string[]

multiple

Whether more than one item can be selected. Shift+Arrow and Shift+click extend the selection, and Ctrl/Cmd+A selects every visible item, or clears the selection when every item is selected.

false | true

onExpandedChange

Handler that is called when items expand or collapse.

(expanded: string[]) => void

onLabelChange

Handler that is called with an item’s edited label. Setting it lets users rename items in place: F2 or a double-click starts editing, Enter or leaving the field saves, and Escape cancels. Items with editable: false keep their label.

(value: string, label: string) => void

onValueChange

Handler that is called when the selection changes. It receives at most one value unless multiple is set.

(value: string[]) => void

options*

The items to render, each with its nested items in children.

readonly TreeOption[] | TreeOption[]

value

The values of the selected items, in controlled mode.

string[]

#

Accessibility

#

Keyboard interactions

The tree is a single tab stop. Tab moves focus to the selected item, or to the first item, and then returns to the item focused last.

Key

Description

ArrowDownArrowUp

Moves focus to the next/previous visible item.

ArrowRight

When focus is on a collapsed branch, expands it. When focus is on an expanded branch, moves focus to its first child.

ArrowLeft

When focus is on an expanded branch, collapses it. Otherwise moves focus to the parent item.

HomeEnd

Moves focus to the first/last visible item.

Enter

Selects the focused item. When multiple is enabled, toggles its selection.

Space

Toggles the focused item’s selection.

ShiftArrowDownShiftArrowUp

When multiple is enabled, extends the selection.

CtrlA

When multiple is enabled, selects every visible item, or clears the selection when every item is selected.

F2

When onLabelChange is set, edits the focused item’s label in place.

ShiftF10ContextMenu

Opens the focused item’s actions in a context menu.

A - Z

Moves focus to the next item whose label starts with the typed text.

Last updated on