26.2. A toolbar with a roving tabindex
Topics
This question is in
26. AccessibilityA text editor's toolbar has a dozen buttons: bold, italic, link, lists, alignment. If each one is a Tab stop, a keyboard user presses Tab twelve times to get past it. The accessible pattern makes the whole toolbar one Tab stop and moves between its buttons with the arrow keys. It is called a roving tabindex: exactly one button has tabindex="0", all the others tabindex="-1", and the 0 moves with focus.
Write createToolbar(element) in toolbar.js.
Signature
function createToolbar(element) // returns { destroy() }element is the toolbar container. Its items are the button elements inside it, in document order. destroy() removes the keyboard and focus handling.
Requirements
- The first button that is not
disabledbecomes the only Tab stop (tabindex="0"); every other button getstabindex="-1". - The container gets
role="toolbar", unless it already has a role. - In a horizontal toolbar (the default), ArrowRight moves to the next button and ArrowLeft to the previous one, wrapping around at both ends. Home goes to the first, End to the last.
- A move focuses the new button, makes it the only Tab stop, and calls
preventDefault(). - Buttons with the
disabledattribute are skipped. Buttons witharia-disabled="true"stay in the order. - With
aria-orientation="vertical"on the container, ArrowDown and ArrowUp move instead, and ArrowRight and ArrowLeft are left alone. A horizontal toolbar leaves ArrowUp and ArrowDown alone. - Every other key, and arrows pressed with Ctrl, Alt or Meta, are not handled and not prevented.
- When a button receives focus any other way (a click, a script), it becomes the Tab stop.
- Keys from an element that is not one of the buttons are ignored. Buttons added later are picked up at the next key press. There is never more than one Tab stop.
destroy()removes the handling. Two toolbars never affect each other.
Examples
// <div aria-label="Formatting"><button>Bold</button><button>Italic</button><button>Link</button></div>
const toolbar = createToolbar(document.querySelector("[aria-label=Formatting]"));
// role="toolbar"; tabindex: Bold 0, Italic -1, Link -1
// Tab reaches Bold; ArrowRight -> Italic (tabindex now Bold -1, Italic 0)
// ArrowRight on Link -> Bold (wraps); End -> Link
// Tab -> leaves the toolbar; Shift+Tab back -> returns to the last focused button// <div aria-orientation="vertical">…</div>
// ArrowDown and ArrowUp move; ArrowRight does nothing.Environment
- Plain JavaScript in a browser page; the tests call the global
createToolbarwith toolbars they create. - How focus is checked: in the tests' sandboxed frame,
focus()does not move real focus anddocument.activeElementnever changes. So the tests record everyfocus()call (they wrapHTMLElement.prototype.focus), dispatch realkeydownandfocusinevents on the button that is meant to have focus, and checktabindexvalues anddefaultPrevented. Find the focused button fromevent.target. - Out of scope: right-to-left layouts, items other than
button, typing a letter to jump to a button, andaria-activedescendant.
Hints
- With every button except one at
tabindex="-1", what does Tab do when it reaches the toolbar, and when it leaves it? - After a click on the third button, the user tabs away and back. Where should they land?
The explanation comes with the course
A walkthrough of every line of a solution, the mistakes the tests catch, other ways to write it, and links to the lessons that teach each idea.
- Every line explained
- The mistakes each test catches
- Links to the lessons behind it