Tree
A hierarchical Shadcn-styled tree view with branch expansion, embedded sidebar appearance, keyboard navigation, and RTL support.
Tree displays nested hierarchies—such as file browsers or document outlines—with single selection, expandable branches, and full keyboard navigation. The examples on this page use the Shadcn Rhea style preset.
Preview
Section titled “Preview”<proto-ui-shadcn-tree-root
id="project-tree"
class="w-full max-w-sm"
>
<div class="flex items-center gap-1">
<proto-ui-shadcn-tree-toggle data-toggle="src">
<span aria-hidden="true">▾</span>
<span class="sr-only">Toggle src</span>
</proto-ui-shadcn-tree-toggle>
<proto-ui-shadcn-tree-item data-node="src" class="flex-1">
src/
</proto-ui-shadcn-tree-item>
</div>
<proto-ui-shadcn-tree-group data-group="src">
<div class="flex items-center gap-1">
<proto-ui-shadcn-tree-toggle data-toggle="components">
<span aria-hidden="true">▾</span>
<span class="sr-only">Toggle components</span>
</proto-ui-shadcn-tree-toggle>
<proto-ui-shadcn-tree-item
data-node="components"
class="flex-1"
>
components/
</proto-ui-shadcn-tree-item>
</div>
<proto-ui-shadcn-tree-group data-group="components">
<proto-ui-shadcn-tree-item data-node="button">
button.tsx
</proto-ui-shadcn-tree-item>
<proto-ui-shadcn-tree-item data-node="tree">
tree.tsx
</proto-ui-shadcn-tree-item>
</proto-ui-shadcn-tree-group>
<proto-ui-shadcn-tree-item data-node="index">
index.ts
</proto-ui-shadcn-tree-item>
</proto-ui-shadcn-tree-group>
<proto-ui-shadcn-tree-item data-node="package">
package.json
</proto-ui-shadcn-tree-item>
<proto-ui-shadcn-tree-item data-node="readme">
README.md
</proto-ui-shadcn-tree-item>
</proto-ui-shadcn-tree-root>
<script type="module">
import { setElementProps } from '@proto.ui/adapter-web-component';
const root = document.querySelector('#project-tree');
setElementProps(root, {
defaultValue: 'button',
defaultExpandedKeys: ['src', 'components'],
styleProfile: 'rhea',
});
setElementProps(root.querySelector('[data-node="src"]'), {
nodeKey: 'src',
textValue: 'src',
});
setElementProps(root.querySelector('[data-toggle="src"]'), {
nodeKey: 'src',
size: 'icon',
});
setElementProps(root.querySelector('[data-group="src"]'), {
nodeKey: 'src',
});
setElementProps(root.querySelector('[data-node="components"]'), {
nodeKey: 'components',
parentKey: 'src',
textValue: 'components',
});
setElementProps(
root.querySelector('[data-toggle="components"]'),
{ nodeKey: 'components', size: 'icon' }
);
setElementProps(root.querySelector('[data-group="components"]'), {
nodeKey: 'components',
});
setElementProps(root.querySelector('[data-node="button"]'), {
nodeKey: 'button',
parentKey: 'components',
textValue: 'button.tsx',
});
setElementProps(root.querySelector('[data-node="tree"]'), {
nodeKey: 'tree',
parentKey: 'components',
textValue: 'tree.tsx',
});
setElementProps(root.querySelector('[data-node="index"]'), {
nodeKey: 'index',
parentKey: 'src',
textValue: 'index.ts',
});
setElementProps(root.querySelector('[data-node="package"]'), {
nodeKey: 'package',
textValue: 'package.json',
});
setElementProps(root.querySelector('[data-node="readme"]'), {
nodeKey: 'readme',
textValue: 'README.md',
});
</script>Installation
Section titled “Installation”Available starting in 0.3.0-rc.1 and later releases. Import the prototypes from @proto.ui/prototypes-shadcn/tree and adapt them for your target runtime:
import { treeRoot, treeItem, treeGroup, treeToggle } from '@proto.ui/prototypes-shadcn/tree';<ShadcnTreeRoot defaultValue="button" defaultExpandedKeys={['src', 'components']}> <div className="flex items-center gap-1"> <ShadcnTreeToggle nodeKey="src" size="icon"> ▾ </ShadcnTreeToggle> <ShadcnTreeItem nodeKey="src" textValue="src" className="flex-1"> src/ </ShadcnTreeItem> </div> <ShadcnTreeGroup nodeKey="src"> <ShadcnTreeItem nodeKey="button" parentKey="src" textValue="button.tsx"> button.tsx </ShadcnTreeItem> </ShadcnTreeGroup></ShadcnTreeRoot>Structure & Accessibility
Section titled “Structure & Accessibility”- Give each
Itema uniquenodeKeyand setparentKeyto its parent branch’s key. Invalid configurations such as duplicate keys, missing parents, or cycles are ignored so the active tree stays valid. - Place child items inside a
TreeGroupwhosenodeKeymatches its branchItem; Tree automatically links and cleans uparia-controlsandaria-labelledbyattributes as nodes mount and unmount. - Collapsing a branch hides its
TreeGroupwithout removing the underlying hierarchy. If focus was inside the collapsed branch, focus moves to the visible ancestor without stealing focus from outside the tree. - Keyboard navigation:
ArrowUp/ArrowDown,Home/End, and first-letter typeahead move focus in visual order. In LTR mode,ArrowRightexpands a branch or enters its first child andArrowLeftcollapses or moves to the parent;direction="rtl"mirrors indentation guides and swaps horizontal arrow mappings.
Style Profiles (styleProfile)
Section titled “Style Profiles (styleProfile)”Supports vega, nova, maia, lyra, mira, luma, sera, rhea, and new-york-v4 (new-york alias). When omitted or set to inherit, components inherit from the nearest Style Scope or fall back to nova.
Examples
Section titled “Examples”Embedded Sidebar Appearance
Section titled “Embedded Sidebar Appearance”Set appearance="embedded" on Root to strip the outer card border and propagate a subtle sidebar row style automatically to descendant Items (any individual Item can still override appearance).
<div
class="w-full max-w-xs rounded-xl border border-border bg-muted/30 p-3"
>
<div class="mb-2 px-2 text-xs font-medium text-muted-foreground">
Workspace
</div>
<proto-ui-shadcn-tree-root id="embedded-tree">
<div class="flex items-center gap-1">
<proto-ui-shadcn-tree-toggle data-toggle="docs">
<span aria-hidden="true">▾</span>
<span class="sr-only">Toggle Documentation</span>
</proto-ui-shadcn-tree-toggle>
<proto-ui-shadcn-tree-item data-node="docs" class="flex-1">
Documentation
</proto-ui-shadcn-tree-item>
</div>
<proto-ui-shadcn-tree-group data-group="docs">
<proto-ui-shadcn-tree-item data-node="getting-started">
Getting Started
</proto-ui-shadcn-tree-item>
<proto-ui-shadcn-tree-item data-node="theming">
Theming & Tokens
</proto-ui-shadcn-tree-item>
<proto-ui-shadcn-tree-item data-node="changelog">
Changelog
</proto-ui-shadcn-tree-item>
</proto-ui-shadcn-tree-group>
<proto-ui-shadcn-tree-item data-node="settings">
Settings
</proto-ui-shadcn-tree-item>
</proto-ui-shadcn-tree-root>
</div>
<script type="module">
import { setElementProps } from '@proto.ui/adapter-web-component';
const root = document.querySelector('#embedded-tree');
setElementProps(root, {
styleProfile: 'rhea',
appearance: 'embedded',
defaultValue: 'getting-started',
defaultExpandedKeys: ['docs'],
});
setElementProps(root.querySelector('[data-node="docs"]'), {
nodeKey: 'docs',
textValue: 'Documentation',
});
setElementProps(root.querySelector('[data-toggle="docs"]'), {
nodeKey: 'docs',
size: 'icon',
});
setElementProps(root.querySelector('[data-group="docs"]'), {
nodeKey: 'docs',
});
setElementProps(
root.querySelector('[data-node="getting-started"]'),
{
nodeKey: 'getting-started',
parentKey: 'docs',
textValue: 'Getting Started',
}
);
setElementProps(root.querySelector('[data-node="theming"]'), {
nodeKey: 'theming',
parentKey: 'docs',
textValue: 'Theming & Tokens',
});
setElementProps(root.querySelector('[data-node="changelog"]'), {
nodeKey: 'changelog',
parentKey: 'docs',
textValue: 'Changelog',
});
setElementProps(root.querySelector('[data-node="settings"]'), {
nodeKey: 'settings',
textValue: 'Settings',
});
</script>Controlled Selection & Expansion
Section titled “Controlled Selection & Expansion”Pass value and/or expandedKeys to control selection and branch expansion externally via valueChange and expandedChange.
<div class="grid w-full max-w-sm gap-2">
<proto-ui-shadcn-tree-root id="controlled-tree">
<div class="flex items-center gap-1">
<proto-ui-shadcn-tree-toggle data-toggle="src">
<span aria-hidden="true">▾</span>
<span class="sr-only">Toggle src</span>
</proto-ui-shadcn-tree-toggle>
<proto-ui-shadcn-tree-item data-node="src" class="flex-1">
src/
</proto-ui-shadcn-tree-item>
</div>
<proto-ui-shadcn-tree-group data-group="src">
<div class="flex items-center gap-1">
<proto-ui-shadcn-tree-toggle data-toggle="components">
<span aria-hidden="true">▾</span>
<span class="sr-only">Toggle components</span>
</proto-ui-shadcn-tree-toggle>
<proto-ui-shadcn-tree-item
data-node="components"
class="flex-1"
>
components/
</proto-ui-shadcn-tree-item>
</div>
<proto-ui-shadcn-tree-group data-group="components">
<proto-ui-shadcn-tree-item data-node="button">
button.tsx
</proto-ui-shadcn-tree-item>
<proto-ui-shadcn-tree-item data-node="tree">
tree.tsx
</proto-ui-shadcn-tree-item>
</proto-ui-shadcn-tree-group>
</proto-ui-shadcn-tree-group>
<proto-ui-shadcn-tree-item data-node="readme">
README.md
</proto-ui-shadcn-tree-item>
</proto-ui-shadcn-tree-root>
<div id="tree-status" role="status" aria-live="polite">
Selected: button · Expanded: src, components
</div>
</div>
<script type="module">
import { setElementProps } from '@proto.ui/adapter-web-component';
const root = document.querySelector('#controlled-tree');
const status = document.querySelector('#tree-status');
let value = 'button';
let expandedKeys = ['src', 'components'];
const sync = () => {
setElementProps(root, {
value,
expandedKeys,
styleProfile: 'rhea',
});
status.textContent =
'Selected: ' +
value +
' · Expanded: ' +
(expandedKeys.join(', ') || 'none');
};
setElementProps(root.querySelector('[data-node="src"]'), {
nodeKey: 'src',
textValue: 'src',
});
setElementProps(root.querySelector('[data-toggle="src"]'), {
nodeKey: 'src',
size: 'icon',
});
setElementProps(root.querySelector('[data-group="src"]'), {
nodeKey: 'src',
});
setElementProps(root.querySelector('[data-node="components"]'), {
nodeKey: 'components',
parentKey: 'src',
textValue: 'components',
});
setElementProps(
root.querySelector('[data-toggle="components"]'),
{ nodeKey: 'components', size: 'icon' }
);
setElementProps(root.querySelector('[data-group="components"]'), {
nodeKey: 'components',
});
setElementProps(root.querySelector('[data-node="button"]'), {
nodeKey: 'button',
parentKey: 'components',
textValue: 'button.tsx',
});
setElementProps(root.querySelector('[data-node="tree"]'), {
nodeKey: 'tree',
parentKey: 'components',
textValue: 'tree.tsx',
});
setElementProps(root.querySelector('[data-node="readme"]'), {
nodeKey: 'readme',
textValue: 'README.md',
});
root.addEventListener('valueChange', (e) => {
value = e.detail.value;
sync();
});
root.addEventListener('expandedChange', (e) => {
expandedKeys = e.detail.expandedKeys;
sync();
});
sync();
</script>Disabled & Read-Only
Section titled “Disabled & Read-Only”Disable the entire tree or individual items with disabled, or set readOnly on Root to lock selection while preserving navigation and branch expansion.
<div class="grid w-full max-w-2xl gap-4 sm:grid-cols-2">
<proto-ui-shadcn-tree-root id="disabled-tree">
<div class="flex items-center gap-1">
<proto-ui-shadcn-tree-toggle data-toggle="packages">
<span aria-hidden="true">▾</span>
<span class="sr-only">Toggle packages</span>
</proto-ui-shadcn-tree-toggle>
<proto-ui-shadcn-tree-item
data-node="packages"
class="flex-1"
>
packages/
</proto-ui-shadcn-tree-item>
</div>
<proto-ui-shadcn-tree-group data-group="packages">
<proto-ui-shadcn-tree-item data-node="active">
core
</proto-ui-shadcn-tree-item>
<proto-ui-shadcn-tree-item data-node="deprecated">
legacy (disabled)
</proto-ui-shadcn-tree-item>
</proto-ui-shadcn-tree-group>
<proto-ui-shadcn-tree-item data-node="license">
LICENSE
</proto-ui-shadcn-tree-item>
</proto-ui-shadcn-tree-root>
<proto-ui-shadcn-tree-root id="readonly-tree">
<div class="flex items-center gap-1">
<proto-ui-shadcn-tree-toggle data-toggle="docs">
<span aria-hidden="true">▾</span>
<span class="sr-only">Toggle docs</span>
</proto-ui-shadcn-tree-toggle>
<proto-ui-shadcn-tree-item data-node="docs" class="flex-1">
docs/
</proto-ui-shadcn-tree-item>
</div>
<proto-ui-shadcn-tree-group data-group="docs">
<proto-ui-shadcn-tree-item data-node="overview">
overview.mdx
</proto-ui-shadcn-tree-item>
<proto-ui-shadcn-tree-item data-node="api">
api.mdx
</proto-ui-shadcn-tree-item>
</proto-ui-shadcn-tree-group>
</proto-ui-shadcn-tree-root>
</div>
<script type="module">
import { setElementProps } from '@proto.ui/adapter-web-component';
const disabledTree = document.querySelector('#disabled-tree');
setElementProps(disabledTree, {
defaultValue: 'active',
defaultExpandedKeys: ['packages'],
styleProfile: 'rhea',
});
setElementProps(
disabledTree.querySelector('[data-node="packages"]'),
{ nodeKey: 'packages', textValue: 'packages' }
);
setElementProps(
disabledTree.querySelector('[data-toggle="packages"]'),
{ nodeKey: 'packages', size: 'icon' }
);
setElementProps(
disabledTree.querySelector('[data-group="packages"]'),
{ nodeKey: 'packages' }
);
setElementProps(
disabledTree.querySelector('[data-node="active"]'),
{ nodeKey: 'active', parentKey: 'packages', textValue: 'core' }
);
setElementProps(
disabledTree.querySelector('[data-node="deprecated"]'),
{
nodeKey: 'deprecated',
parentKey: 'packages',
textValue: 'legacy',
disabled: true,
}
);
setElementProps(
disabledTree.querySelector('[data-node="license"]'),
{ nodeKey: 'license', textValue: 'LICENSE' }
);
const readonlyTree = document.querySelector('#readonly-tree');
setElementProps(readonlyTree, {
readOnly: true,
defaultValue: 'overview',
defaultExpandedKeys: ['docs'],
styleProfile: 'rhea',
});
setElementProps(
readonlyTree.querySelector('[data-node="docs"]'),
{ nodeKey: 'docs', textValue: 'docs' }
);
setElementProps(
readonlyTree.querySelector('[data-toggle="docs"]'),
{ nodeKey: 'docs', size: 'icon' }
);
setElementProps(
readonlyTree.querySelector('[data-group="docs"]'),
{ nodeKey: 'docs' }
);
setElementProps(
readonlyTree.querySelector('[data-node="overview"]'),
{
nodeKey: 'overview',
parentKey: 'docs',
textValue: 'overview.mdx',
}
);
setElementProps(readonlyTree.querySelector('[data-node="api"]'), {
nodeKey: 'api',
parentKey: 'docs',
textValue: 'api.mdx',
});
</script>Right-to-Left (RTL)
Section titled “Right-to-Left (RTL)”Set direction="rtl" on Root to mirror indentation guides and swap Left/Right arrow navigation.
<proto-ui-shadcn-tree-root id="rtl-tree" class="w-full max-w-sm">
<div class="flex items-center gap-1">
<proto-ui-shadcn-tree-toggle data-toggle="project">
<span aria-hidden="true">▾</span>
<span class="sr-only">تبديل مجلد المشروع</span>
</proto-ui-shadcn-tree-toggle>
<proto-ui-shadcn-tree-item data-node="project" class="flex-1">
المشروع
</proto-ui-shadcn-tree-item>
</div>
<proto-ui-shadcn-tree-group data-group="project">
<proto-ui-shadcn-tree-item data-node="components">
المكونات
</proto-ui-shadcn-tree-item>
<proto-ui-shadcn-tree-item data-node="styles">
الأنماط
</proto-ui-shadcn-tree-item>
</proto-ui-shadcn-tree-group>
<proto-ui-shadcn-tree-item data-node="readme">
الدليل
</proto-ui-shadcn-tree-item>
</proto-ui-shadcn-tree-root>
<script type="module">
import { setElementProps } from '@proto.ui/adapter-web-component';
const root = document.querySelector('#rtl-tree');
setElementProps(root, {
direction: 'rtl',
defaultValue: 'components',
defaultExpandedKeys: ['project'],
styleProfile: 'rhea',
});
setElementProps(root.querySelector('[data-node="project"]'), {
nodeKey: 'project',
textValue: 'المشروع',
});
setElementProps(root.querySelector('[data-toggle="project"]'), {
nodeKey: 'project',
size: 'icon',
});
setElementProps(root.querySelector('[data-group="project"]'), {
nodeKey: 'project',
});
setElementProps(root.querySelector('[data-node="components"]'), {
nodeKey: 'components',
parentKey: 'project',
textValue: 'المكونات',
});
setElementProps(root.querySelector('[data-node="styles"]'), {
nodeKey: 'styles',
parentKey: 'project',
textValue: 'الأنماط',
});
setElementProps(root.querySelector('[data-node="readme"]'), {
nodeKey: 'readme',
textValue: 'الدليل',
});
</script>| Prop | Type | Default |
|---|---|---|
value | string | undefined (controlled) |
defaultValue | string | '' |
expandedKeys | readonly string[] | undefined (controlled) |
defaultExpandedKeys | readonly string[] | [] |
appearance | 'default' 'embedded' | default |
styleProfile | ShadcnStyleChoice | inherit → nearest scope → nova |
direction | 'ltr' 'rtl' | ltr |
disabled | boolean | false |
readOnly | boolean | false |
a11yLabel | string | Tree |
| Prop | Type | Default |
|---|---|---|
nodeKey | string | '' |
parentKey | string | '' |
textValue | string | '' |
disabled | boolean | false |
appearance | 'inherit' 'default' 'embedded' | inherit |
styleProfile | ShadcnStyleChoice | inherit → nearest scope → nova |
Group & Toggle
Section titled “Group & Toggle”| Prop | Type | Default |
|---|---|---|
Group.nodeKey | string | '' |
Toggle.nodeKey | string | '' |
Toggle.size | 'default' 'icon' | default |
Toggle.disabled | boolean | false |
Toggle.styleProfile | ShadcnStyleChoice | inherit → nearest scope → nova |
Events & Methods (Root)
Section titled “Events & Methods (Root)”| Name | Signature / Payload | Description |
|---|---|---|
valueChange | { value: string } | Emitted when a selection change is requested |
expandedChange | { expandedKeys: string[] } | Emitted when branch expansion changes are requested |
getTree | () => TreeEntry[] | Returns the current validated tree snapshot |
getExpandedKeys | () => string[] | Returns the list of expanded branch keys |
requestValue | (key: string) => boolean | Programmatically requests selecting a node |
requestExpanded | (key: string, open: boolean) => boolean | Programmatically requests expanding or collapsing a branch |
refresh | () => void | Recomputes and publishes the tree hierarchy snapshot |