TreeView
Keyboard-accessible hierarchical view with multi-selection and drop states.
Preview
A live, theme-aware example rendered with the published component.
Loading preview…
Import
Import from the focused entry point below, or use the same named export from @tavojs/ui. Both are public APIs; measure the built application when comparing their bundle cost.
tsximport { TreeView } from "@tavojs/ui/tree-view";The component import loads compiled component CSS. Enable tavoUi() for your project theme variables, or import @tavojs/ui/theme.css once for the package default theme. The equivalent CSS-safe component alias is @tavojs/ui/css/tree-view.
When to use
Use it when
Choose something else when
Examples
These examples include imports and local state where interaction requires it. Render the exported component in a page after completing UI installation. Demo state stays in the mounted component; connect file handling, persistence, and data loading to your application as needed.
TreeView example
tsximport { createTavo } from "@tavojs/core";
import { TreeView } from "@tavojs/ui/tree-view";
import { Text } from "@tavojs/ui";
type State = { selectedIds: string[] };
export const TreeViewExample1 = createTavo<Record<string, never>, State>({
model: () => ({ selectedIds: [] }),
view: ({ state, model }) => (
<>
<TreeView
aria-label="Page layers"
selectedIds={state.selectedIds}
selectionMode="multiple"
defaultExpandedIds={["hero"]}
onSelectionChange={(selectedIds) => model.set("selectedIds", selectedIds)}
>
<TreeView.Item id="hero" label="Hero" description="Section">
<TreeView.Item id="heading" label="Heading" />
</TreeView.Item>
</TreeView>
<Text>Selected: {state.selectedIds.join(", ") || "None"}</Text>
</>
)
});Common props and defaults
A curated starting point. The complete TypeScript API below includes additional props and compound member contracts.
| Prop | Type | Default | Description |
|---|---|---|---|
className | string | — | Optional class hook applied to the public root element owned by the component. |
selectedIds | readonly string[] | — | Controlled selected identifiers. |
defaultSelectedIds | readonly string[] | — | Initial selection. |
selectionMode | "none" | "single" | "multiple" | — | Selection behavior. |
selectionAnchorId | string | — | Explicit contiguous-range anchor. |
expandedIds | readonly string[] | — | Controlled expanded identifiers. |
onSelectionChange | (ids: string[]) => void | — | Selection callback. |
onActivate | (id: string) => void | — | Enter or double-click activation. |
onDrop | (event: TreeViewDropEvent) => void | — | Opaque in-memory source/target drop request. |
Complete TypeScript API
Import from @tavojs/ui/tree-view. Published exports: Tree, TreeItem, TreeView, TreeViewDropEvent, TreeViewDropPlacement, TreeViewDropState, TreeViewItem, TreeViewItemProps, TreeViewProps, TreeViewRoot, TreeViewSelectionMode, nextTreeSelection.
TreeViewProps
| Property | Type | Required | Description |
|---|---|---|---|
children | Child | No | Inherited children attribute accepted by TreeView. |
className | string | No | Optional class hook applied to the public root element owned by the component. |
sx | Sx | No | Inherited sx attribute accepted by TreeView. |
id | string | No | Inherited id attribute accepted by TreeView. |
role | string | No | Inherited role attribute accepted by TreeView. |
style | Record<string, unknown> | No | Inherited style attribute accepted by TreeView. |
title | string | No | Inherited title attribute accepted by TreeView. |
tabIndex | number | No | Inherited tabIndex attribute accepted by TreeView. |
hidden | boolean | No | Inherited hidden attribute accepted by TreeView. |
disabled | boolean | No | Inherited disabled attribute accepted by TreeView. |
onClick | TavoEventHandler<MouseEvent> | No | Inherited onClick attribute accepted by TreeView. |
onChange | TavoEventHandler<Event> | No | Inherited onChange attribute accepted by TreeView. |
onInput | TavoEventHandler<Event> | No | Inherited onInput attribute accepted by TreeView. |
onKeyDown | TavoEventHandler<KeyboardEvent> | No | Inherited onKeyDown attribute accepted by TreeView. |
onFocus | TavoEventHandler<FocusEvent> | No | Inherited onFocus attribute accepted by TreeView. |
onBlur | TavoEventHandler<FocusEvent> | No | Inherited onBlur attribute accepted by TreeView. |
selectedIds | readonly string[] | No | Controlled selected identifiers. |
defaultSelectedIds | readonly string[] | No | Initial selection. |
selectionMode | TreeViewSelectionMode | No | Selection behavior. |
selectionAnchorId | string | No | Explicit contiguous-range anchor. |
expandedIds | readonly string[] | No | Controlled expanded identifiers. |
defaultExpandedIds | readonly string[] | No | Inherited defaultExpandedIds attribute accepted by TreeView. |
onSelectionChange | (selectedIds: string[]) => void | No | Selection callback. |
onExpandedChange | (expandedIds: string[]) => void | No | Inherited onExpandedChange attribute accepted by TreeView. |
onActivate | (id: string) => void | No | Enter or double-click activation. |
onDrop | (event: TreeViewDropEvent) => void | No | Opaque in-memory source/target drop request. |
TreeViewItemProps
| Property | Type | Required | Description |
|---|---|---|---|
children | Child | No | Inherited children attribute accepted by TreeView. |
className | string | No | Optional class hook applied to the public root element owned by the component. |
sx | Sx | No | Inherited sx attribute accepted by TreeView. |
id | string | Yes | Inherited id attribute accepted by TreeView. |
role | string | No | Inherited role attribute accepted by TreeView. |
style | Record<string, unknown> | No | Inherited style attribute accepted by TreeView. |
title | string | No | Inherited title attribute accepted by TreeView. |
tabIndex | number | No | Inherited tabIndex attribute accepted by TreeView. |
hidden | boolean | No | Inherited hidden attribute accepted by TreeView. |
disabled | boolean | No | Inherited disabled attribute accepted by TreeView. |
onClick | TavoEventHandler<MouseEvent> | No | Inherited onClick attribute accepted by TreeView. |
onChange | TavoEventHandler<Event> | No | Inherited onChange attribute accepted by TreeView. |
onInput | TavoEventHandler<Event> | No | Inherited onInput attribute accepted by TreeView. |
onKeyDown | TavoEventHandler<KeyboardEvent> | No | Inherited onKeyDown attribute accepted by TreeView. |
onFocus | TavoEventHandler<FocusEvent> | No | Inherited onFocus attribute accepted by TreeView. |
onBlur | TavoEventHandler<FocusEvent> | No | Inherited onBlur attribute accepted by TreeView. |
label | Child | Yes | Inherited label attribute accepted by TreeView. |
description | Child | No | Inherited description attribute accepted by TreeView. |
depth | number | No | Inherited depth attribute accepted by TreeView. |
draggable | boolean | No | Inherited draggable attribute accepted by TreeView. |
expanded | boolean | No | Inherited expanded attribute accepted by TreeView. |
dropState | TreeViewDropState | No | Inherited dropState attribute accepted by TreeView. |
expandLabel | string | No | Inherited expandLabel attribute accepted by TreeView. |
State ownership and DOM target
| Concern | Contract |
|---|---|
| State | Selection and expansion are independent. Use selectedIds/onSelectionChange and expandedIds/onExpandedChange, or their defaultSelectedIds/defaultExpandedIds initial values. |
| DOM target | Public passthrough props target the component root. Treat nested elements and private class names as implementation details. |
Anatomy and related components
Public compound members: Root, Item
Limitations
Avoid TreeView when using data components as decorative layout containers.
Use only the documented props, entry points, and compound members. Internal DOM nesting and CSS class names can change without becoming part of the public component contract.
Accessibility
Items use ARIA tree semantics and roving tab stops. Arrow, Home, End, Space, and Enter behavior is keyboard complete; expansion buttons are separately named.
Items use ARIA tree semantics and roving tab stops. Arrow, Home, End, Space, and Enter behavior is keyboard complete; expansion buttons are separately named.
Component status
| Contract | Current value |
|---|---|
| Maturity | stable |
| Category | data |
| Component import | @tavojs/ui/tree-view |
| Explicit CSS import | @tavojs/ui/css/tree-view |
| Preview | Interactive preview available on this page |