Advertisement

Interactive Sidebar Tree Navigation

| | 3 min read | code by Jhey
A11y Ready Advanced

Tech & Dependencies

HTML CSS Babel
Tweakpane GSAP

Features

  • Keyboard Navigation
  • Real-time Search Filter
  • Smooth Grid Animation

Browser Support

Chrome 80+ Edge 80+ Firefox 75+ Safari 13.1+

Core

This is an Interactive Sidebar Tree Navigation. It acts as a custom Web Component (<sidebar-tree>) that generates and manages a complex, multi-level navigation structure from a JSON data object. Its primary function is to provide a highly accessible, keyboard-navigable document outline with built-in real-time search filtering and smooth expand/collapse animations.

Specs

  • Weight: ~12 KB (logic only, excluding GSAP/Tweakpane used for the demo background/controls).
  • Performance: High. The tree is generated once. Expand/collapse animations use a clever CSS Grid technique (grid-template-rows: 0fr to 1fr) combined with the inert attribute to handle reflows efficiently and manage focus states natively.
  • Theming / Customization: CSS variables control the focus color, border colors, and transition durations. Built-in support for light-dark() color schemes.
  • Responsiveness: Designed as a sidebar, it occupies a fixed width within a resizable container for demo purposes, but adapts cleanly to its parent container.
  • Graceful Degradation: The component heavily relies on JavaScript to build the DOM from JSON and manage interactions. If JS is disabled, the sidebar will be completely empty.

Anatomy

The component merges dynamic DOM generation with strict ARIA standards.

  • HTML (The Skeleton): The generateTreeHTML function builds a nested list structure (<ul role="tree">, <li role="none">, <a role="treeitem">, <ul role="group">). It automatically assigns aria-level, aria-setsize, aria-posinset, and aria-expanded attributes.
  • CSS (The Skin): Utilizes nested selectors to indent groups. The [inert] attribute is used as a CSS selector hook to trigger the 0fr grid track collapse, hiding children both visually and from assistive tech simultaneously.
  • JS (The Nervous System): The SidebarTree class extends HTMLElement. It builds a nodeMap for fast lookups, handles complex keyboard event routing (Up, Down, Right, Left, Home, End, asterisk), and implements a recursive filtering algorithm that highlights matches while keeping parent groups expanded.

Logic

The standout feature is the Robust Keyboard Navigation Routing.

handleKeydown(event) {
  // ...
  case 'ArrowRight':
    this.handleRightArrow(treeItem); break;
  case 'ArrowLeft':
    this.handleLeftArrow(treeItem); break;
}

handleRightArrow(item) {
  if (item.hasAttribute('aria-expanded')) {
    if (!this.isExpanded(item)) this.toggleExpanded(item);
    else this.focusItem(this.getGroupFromItem(item).querySelector('[role="treeitem"]'));
  }
}

The component perfectly mimics native OS tree-view controls. Instead of just tabbing through links, the user navigates spatially. Pressing Right Arrow on a closed folder opens it; pressing it again moves focus to the first child. Pressing Left Arrow closes an open folder, or if already closed, jumps focus up to the parent folder. This requires maintaining an internal map of all visible items and understanding the hierarchical relationships (parentId), which the class builds during connectedCallback.

Feel

Professional, snappy, and deeply intuitive. The use of CSS Grid 1fr to 0fr transitions makes expanding nested folders feel smooth and organic, avoiding the abrupt jumps associated with display: none toggles. The search functionality is instantaneous, dimming non-relevant items and highlighting matches with a distinct background color. The keyboard support is flawless, making power-users feel immediately at home navigating complex documentation structures without touching the mouse.

Advertisement