Hub: index · watch · videos · demo

Treeview: roving focus + expand/collapse (keyboard + ARIA)

A treeview (like a file explorer sidebar) is a classic keyboard pattern. Many implementations look fine visually but fail for keyboard and assistive tech because they don’t expose structure, state, or predictable navigation.

Live demo (no tracking):

Open treeview demo (BEFORE vs AFTER)

When to use a treeview

Semantic requirements (core ARIA roles)

In a full implementation you may also manage selection state (aria-selected) and multi-select behavior, but start with structure + expand/collapse + navigation.

Keyboard navigation (expected behaviors)

Roving tabindex (the key to a sane Tab order)

Don’t put every node in the page tab order. Instead, use roving tabindex:

Predictable focus on collapse

Collapsing a node should keep focus on the node you collapsed. Avoid teleporting focus to unrelated items or removing the focused element from the DOM while it still has focus.

Minimal markup skeleton

<div role="tree" aria-label="Files">
  <div role="treeitem" id="ti-src" aria-expanded="true" tabindex="0">src</div>
  <div role="group" aria-label="src children">
    <div role="treeitem" id="ti-app" tabindex="-1">App.jsx</div>
  </div>
</div>

You still need JS to (1) manage roving tabindex, (2) expand/collapse groups, and (3) implement the keyboard interactions above.