Tree View
Shows nested items as a tree whose branches open and close, with one selected item at a time.
<arc-tree-view>Overview
TreeView shows nested data as a tree whose branches open and close, such as a file browser, a navigation menu or a documentation outline. Each node is an <arc-tree-item>, nested to any depth, with a label, an optional icon (rendered as text) and an expanded attribute for its starting state.
Clicking a branch selects it and opens or closes it. Clicking a leaf selects it. The selected row is tinted and drawn in --interactive, a vertical guide line runs beside each nested group, and the chevron turns as a branch opens or closes.
ArrowDown and ArrowUp move between visible rows, ArrowRight opens a branch, ArrowLeft closes it, and Enter or Space selects. Home, End and typeahead from the WAI-ARIA tree pattern aren't supported, and ArrowRight and ArrowLeft don't move to a child or back to the parent. arc-select fires with the item and its path, the chain of labels from the root, and arc-toggle fires when a branch opens or closes.
Guidelines
Do
- Give every node a descriptive `label`
- Set `expanded` on the top-level branches readers should see first
- Add an `icon` when readers scan by type, such as folder and file glyphs in a file browser
- Listen to `arc-select` to update the main content when a node is picked
- Keep the tree to three or four levels. Deeper trees are hard to scan
Don't
- Don't use it for flat lists. Use List or NavigationMenu
- Don't load hundreds of top-level nodes at once. Load them in pages or on demand
- Don't use it to select several items. It tracks one selection
- Don't rely on icons alone. Every item needs a text label
- Don't use very long labels. They truncate and can't be read in full
Features
- Roles `tree`, `treeitem` and `group`, with `aria-expanded` on branches
- Guide lines are drawn in `--divider`
- Long labels truncate with an ellipsis
Preview
Usage
This component needs JavaScript. Use the Web Component or a framework wrapper.
<arc-tree-view>
<arc-tree-item label="src" icon="π" expanded>
<arc-tree-item label="index.ts" icon="π"></arc-tree-item>
</arc-tree-item>
<arc-tree-item label="package.json" icon="π"></arc-tree-item>
</arc-tree-view>import { TreeItem, TreeView } from '@arclux/arc-ui-react';
export default function Example() {
return (
<TreeView>
<TreeItem label="src" icon="π" expanded>
<TreeItem label="index.ts" icon="π" />
</TreeItem>
<TreeItem label="package.json" icon="π" />
</TreeView>
);
}<script setup>
import { TreeItem, TreeView } from '@arclux/arc-ui-vue';
</script>
<template>
<TreeView>
<TreeItem label="src" icon="π" expanded>
<TreeItem label="index.ts" icon="π" />
</TreeItem>
<TreeItem label="package.json" icon="π" />
</TreeView>
</template><script>
import { TreeItem, TreeView } from '@arclux/arc-ui-svelte';
</script>
<TreeView>
<TreeItem label="src" icon="π" expanded>
<TreeItem label="index.ts" icon="π" />
</TreeItem>
<TreeItem label="package.json" icon="π" />
</TreeView>import { Component } from '@angular/core';
import { TreeItem, TreeView } from '@arclux/arc-ui-angular';
@Component({
imports: [TreeItem, TreeView],
template: `
<arc-tree-view>
<arc-tree-item label="src" icon="π" expanded>
<arc-tree-item label="index.ts" icon="π"></arc-tree-item>
</arc-tree-item>
<arc-tree-item label="package.json" icon="π"></arc-tree-item>
</arc-tree-view>
`,
})
export class MyComponent {}import { TreeItem, TreeView } from '@arclux/arc-ui-solid';
export default function Example() {
return (
<TreeView>
<TreeItem label="src" icon="π" expanded>
<TreeItem label="index.ts" icon="π" />
</TreeItem>
<TreeItem label="package.json" icon="π" />
</TreeView>
);
}import { TreeItem, TreeView } from '@arclux/arc-ui-preact';
export default function Example() {
return (
<TreeView>
<TreeItem label="src" icon="π" expanded>
<TreeItem label="index.ts" icon="π" />
</TreeItem>
<TreeItem label="package.json" icon="π" />
</TreeView>
);
}Events
arc-toggledetail:{ item: { label: string, icon: string }, path: string[], expanded: boolean }- Fired when a tree node is expanded or collapsed.
pathis the node's label chain from the root and is what identifies it: two nodes may share a label. arc-selectdetail:{ value: string, item: { label: string, icon: string }, path: string[] }- Fired when a tree item is selected.
pathis the node's label chain from the root, matchingarc-toggle.
TreeItem
<arc-tree-item>Node within a TreeView. Can nest for sub-trees.
items- Nested arc-tree-item children
hasChildrenlabelstring''- Item label text
iconstring''- Icon or emoji
expandedbooleanfalse- Expand child items