Move Between Tabs with Arrow Keys

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

The problem

  • medium
  • Code

Build product tabs the full WAI-ARIA way, beyond plain clickable tabs. A role="tablist" named "Product details" (aria-label) holds three role="tab" elements, "Overview", "Specs" and "Reviews", with aria-selected="true" on the chosen tab and "false" on the others. Overview is chosen at first, and clicking a tab chooses it. Only the chosen tab's panel shows (leave the others out, or hide them with the hidden attribute): a role="tabpanel" reading "A light, fast laptop.", "14-inch screen, 1.2 kg." or "Rated 4.5 by 120 buyers.". Link each tab and its panel both ways: the tab's aria-controls holds the panel's id, and the panel's aria-labelledby holds the tab's id, so the panel is named after its tab. The tabs are a single Tab stop: the chosen tab has tabIndex={0} and the others tabIndex={-1}, and the panel has tabIndex={0}, so Tab goes from the chosen tab straight to the panel. While a tab has focus, the Right and Left arrows move focus to the next or previous tab, wrapping round, and Home and End to the first and last. The tab that gets focus is chosen at once (automatic activation).

Your code must pass

  • Overview is chosen at first, in a tablist named "Product details"
  • The chosen tab and its panel point to each other
  • Tab goes from the chosen tab to its panel
  • Arrow keys move focus and choose the tab, wrapping round
  • Home and End jump to the first and last tab