Skip to curriculum
Jump to the editor

26.2. A toolbar with a roving tabindex

Medium
Topics

This question is in

26. Accessibility
20–30 min

A 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

JAVASCRIPT
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

  1. The first button that is not disabled becomes the only Tab stop (tabindex="0"); every other button gets tabindex="-1".
  2. The container gets role="toolbar", unless it already has a role.
  3. 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.
  4. A move focuses the new button, makes it the only Tab stop, and calls preventDefault().
  5. Buttons with the disabled attribute are skipped. Buttons with aria-disabled="true" stay in the order.
  6. 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.
  7. Every other key, and arrows pressed with Ctrl, Alt or Meta, are not handled and not prevented.
  8. When a button receives focus any other way (a click, a script), it becomes the Tab stop.
  9. 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.
  10. destroy() removes the handling. Two toolbars never affect each other.

Examples

JAVASCRIPT
// <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
JAVASCRIPT
// <div aria-orientation="vertical">…</div>
// ArrowDown and ArrowUp move; ArrowRight does nothing.

Environment

  • Plain JavaScript in a browser page; the tests call the global createToolbar with toolbars they create.
  • How focus is checked: in the tests' sandboxed frame, focus() does not move real focus and document.activeElement never changes. So the tests record every focus() call (they wrap HTMLElement.prototype.focus), dispatch real keydown and focusin events on the button that is meant to have focus, and check tabindex values and defaultPrevented. Find the focused button from event.target.
  • Out of scope: right-to-left layouts, items other than button, typing a letter to jump to a button, and aria-activedescendant.

Hints

  1. With every button except one at tabindex="-1", what does Tab do when it reaches the toolbar, and when it leaves it?
  2. 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
Back to the question
Loading the editor…