Roving Tabindex and Focus Order Mechanics
Roving tabindex gives a composite widget (tablist, toolbar, radio group, grid) exactly one Tab stop. Only the currently active child carries tabindex="0", while all sibling items hold tabindex="-1" and receive programmatic .focus() on Arrow key presses.
Roving Tabindex State Machine in a Tablist
Watch tabindex='0' and focus() migrate across tabs on ArrowRight while Tab exits the widget in a single stop.
Pressing Tab lands on Tab 1 (the sole item with tabindex='0'). Tabs 2 and 3 have tabindex='-1' so Tab skips them.
<button role="tab" tabindex="0">Overview</button>- activeIndex
- 0
- tab0Tabindex
- 0
- tab1Tabindex
- -1
- tab2Tabindex
- -1
Under the Hood
Without roving tabindex, a 40-button toolbar forces keyboard users to mash Tab forty times just to reach the next section; roving tabindex turns the entire toolbar into one exit-ramps-included roundabout.
- Sequential Focus Order: The browser's
Tabwalker visits elements withtabindex="0"in DOM order and skipstabindex="-1", thoughelement.focus()still moves focus to-1nodes. - Delegated Key Listener: A single
keydownhandler on the parent container interceptsArrowLeft/ArrowRight(orHome/End) via DOM Event Propagation. - Focus Restoration: Leaving the widget via
Taband returning later viaShift+Tablands directly on the last active item because itstabindexremained0in the Accessibility Tree.
toolbar.addEventListener('keydown', (e) => {
const items = [...toolbar.querySelectorAll('[role="button"]')];
const idx = items.indexOf(document.activeElement);
if (idx === -1) return;
const next = e.key === 'ArrowRight' ? (idx + 1) % items.length
: e.key === 'ArrowLeft' ? (idx - 1 + items.length) % items.length : -1;
if (next === -1) return;
items[idx].tabIndex = -1;
items[next].tabIndex = 0;
items[next].focus();
});Problem Genealogy
- 2000s (Positive
tabindex="1","2"): Brittle manual ordering caused chaotic focus jumps whenever CSS Grid or Flexbox reordered visual elements. - 2014 (WAI-ARIA APG Roving Tabindex): Standardized
0/-1swapping for desktop-grade web widgets. - 2020+ (
aria-activedescendantvs Roving Tabindex): Virtualized lists adoptedaria-activedescendant, while fixed toolbars and tablists standardized on rovingtabindex.
Production Pitfalls
- Using positive
tabindex > 0: Even a singletabindex="1"hijacks the entire document'sTaborder ahead of skip links and navigation; restrict values to0and-1. - Removing the active node without shifting
tabindex="0": Deleting the currently active tab without promoting a neighbor leaves the widget unreachable by keyboardTab.
- When not to use roving
tabindex: Virtualized lists with 10,000 off-screen rows cannot mount DOM nodes to focus; usearia-activedescendanton the scroll container instead.
Connections & Logical Next Steps
Composite widgets like menus and tabs manage arrow-key focus via roving tabindex contracts.
Roving tabindex pairs with composite ARIA roles (tablist, menu, toolbar) so screen readers announce item positions.
A single keydown listener on the container intercepts Arrow keys via event bubbling.
Popover handles Top Layer light-dismiss, while roving tabindex manages arrow-key focus inside menus.