For AI agents: the complete documentation index is available at https://a3s-lab.github.io/UI/v0.4.0/en/llms.txt, the full documentation bundle is available at https://a3s-lab.github.io/UI/v0.4.0/en/llms-full.txt, and this page is available as Markdown at https://a3s-lab.github.io/UI/v0.4.0/en/components/tree.md.

Tree

Tree presents nested data as an expandable hierarchy so users can locate, select, and act on nodes one level at a time.

Live preview
src3 items
components3 items
button.css4 KB
tree.css3 KB
legacy.csslocked
index.ts2 KB
package.json1 KB

Usage

Tree is an A3S application component for file explorers, resource hierarchies, and nested configuration. It is intentionally distinct from Accordion: focus moves between visible nodes, selection is explicit, and branches expose a parent-child hierarchy to assistive technology.

Include CSS

Import Tailwind and one complete A3S UI style bundle.

@import "tailwindcss";
@import "@a3s-lab/ui/a3s.css";

Or import only the base CSS, Tree component CSS, and one style pack.

@import "tailwindcss";
@import "@a3s-lab/ui/base.css";
@import "@a3s-lab/ui/components/tree.css";
@import "@a3s-lab/ui/styles/a3s.css";

Include JavaScript

Load the complete runtime bundle, or load the runtime before the Tree controller.

<script src="/assets/js/runtime.min.js" defer></script>
<script src="/assets/js/tree.min.js" defer></script>

Add the tree HTML

<div class="tree" role="tree" aria-label="Project files">
  <div
    role="treeitem"
    aria-label="src"
    aria-expanded="true"
    aria-selected="false"
    data-value="src"
  >
    <div data-tree-row>
      <svg aria-hidden="true"><!-- folder icon --></svg>
      <span data-tree-label>src</span>
    </div>
    <div role="group">
      <div
        role="treeitem"
        aria-label="tree.js"
        aria-selected="true"
        data-value="src/tree.js"
      >
        <div data-tree-row>
          <svg aria-hidden="true"><!-- file icon --></svg>
          <span data-tree-label>tree.js</span>
        </div>
      </div>
    </div>
  </div>
</div>

Parameters

MarkupPurpose
.tree[role="tree"]Root composite widget. Give it an accessible name with aria-label or aria-labelledby.
[role="treeitem"]One selectable node. Use aria-selected, optional aria-expanded, and optional aria-disabled.
[data-tree-row]The visual row directly inside a tree item. Clicking an expandable row selects and toggles its branch.
[role="group"]Direct child group owned by an expandable tree item.
[data-tree-label]Primary visible label used for truncation and typeahead fallback.
[data-tree-meta]Optional trailing metadata such as size or item count.
data-valueStable value returned in event details. The item id or label is used when omitted.

The controller owns tabindex, synchronizes child-group visibility with aria-expanded, and keeps exactly one enabled visible node in the Tab order.

JavaScript API

const tree = document.querySelector("#project-tree");
const item = tree.querySelector('[data-value="src/components"]');

tree.expand(item, true);
tree.select(item, true);
tree.refresh();
MethodBehavior
tree.select(item, focus = false)Sets the single selected item and optionally moves focus.
tree.expand(item, expanded = true)Opens or closes an item that directly owns a group.
tree.toggle(item)Toggles an expandable item.
tree.refresh()Re-reads dynamically added, removed, or disabled nodes.

Selection emits a bubbling change event with { item, value }. Expansion emits a3s:tree-toggle with { item, value, expanded }.

States and variants

Disabled nodes

Set aria-disabled="true" on a tree item. Disabled nodes remain visible but are skipped by pointer interaction, arrow navigation, and typeahead.

RTL

Live preview
المصدرعنصران
المكونات
الفهرس

Logical indentation, guide lines, and chevrons follow document direction. In RTL, ArrowLeft expands or enters a branch and ArrowRight collapses or returns to its parent.

Accessibility

  • Keep tree items as direct children of the root or a direct child role="group".
  • Give every item a concise accessible name. An explicit aria-label prevents descendant groups or trailing metadata from becoming part of a branch name.
  • Do not place buttons, links, or form controls inside a tree row. The tree item itself owns focus and selection.
  • Use Tree for hierarchical choices. Use Accordion for independent disclosure sections and Sidebar for page navigation.
KeyAction
ArrowDown / ArrowUpMoves to the next or previous enabled visible node.
Home / EndMoves to the first or last enabled visible node.
ArrowRight / ArrowLeftExpands, enters, collapses, or returns to a parent; directions reverse in RTL.
Enter / SpaceSelects the focused node.
Printable charactersMoves to the next visible node whose label starts with the typed text.