Course contentsShow
← Web Development
Lesson 1201 of 2,98221. Accessible ComponentsFree lesson

Tree View Pattern

Build hierarchical tree views with expand/collapse functionality and proper keyboard navigation.

Tree View Pattern

You'll learn: How to create accessible, keyboard-navigable tree structures that display hierarchical data with expand/collapse functionality.

What Is a Tree View?

A tree view displays hierarchical information—like a file system, organizational chart, or nested navigation menu—where items can have children that expand and collapse. Think of it like the folder structure in file explorers: click a folder to reveal its contents, or collapse it to hide them again.

Unlike simple lists, tree views show parent-child relationships at multiple levels. Each item (called a "node") can be a parent, a child, or both.

Core Components

A proper tree view requires:

  • Semantic structure: Use role="tree" on the container, role="treeitem" on each node
  • Expanded state: aria-expanded="true" or "false" indicates whether a parent node is open
  • Hierarchy depth: aria-level tells screen readers how deeply nested each item is
  • Set size: aria-setsize and aria-posinset describe position within siblings

Keyboard Navigation

Tree views must support:

  • Arrow Up/Down: Move between visible nodes
  • Arrow Right: Expand a collapsed node or move to first child
  • Arrow Left: Collapse an expanded node or move to parent
  • Enter/Space: Activate the selected node
  • Home/End: Jump to first/last visible node

Focus should move through only the visible nodes—collapsed children shouldn't receive focus.

Focus Management

Only one treeitem should be in the tab order at a time (tabindex="0" on the active node, tabindex="-1" on others). This prevents users from tabbing through dozens of items. Use the roving tabindex pattern you've learned in previous widget patterns.

Key Takeaway: Tree views require role="tree" semantics, multi-level aria-expanded states, comprehensive arrow-key navigation, and careful focus management to represent hierarchical data accessibly.