Format Text from a Toolbar

Write the code, see it run, pass the checks.

The problem

  • medium
  • Code

A role="toolbar" named "Text formatting" (aria-label) holds three toggle buttons, "Bold", "Italic" and "Underline", each with aria-pressed="false" until it's pressed. Clicking a button, or pressing Enter or Space on it, flips its aria-pressed between "true" and "false". After the toolbar comes a textarea labelled "Text" (the buttons may style it). The toolbar is a single Tab stop with a roving tabindex: at first Bold has tabIndex={0} and the others tabIndex={-1}. While a button has focus, the Right and Left arrows move focus to the next or previous button, wrapping round, and Home and End to the first and last; moving focus presses nothing. Whichever button last had focus, by keyboard or by click, keeps tabIndex={0}, so tabbing away and back returns to it.

Your code must pass

  • Tab reaches Bold first, and the next Tab leaves the toolbar
  • Arrow keys, Home and End move focus without pressing anything
  • Enter, Space and clicks flip aria-pressed
  • Tab comes back to the button that last had focus