Skip to content
DevFlash

Junior

Tabs that work with a keyboard

The tabs pattern from the ARIA guide: roles, roving tabindex and arrow keys.

FreeJunior~30 min

This challenge is under editorial review.

Why it shows up in interviews

Accessibility questions are moving from "do you know ARIA?" to "build this widget so it works without a mouse". Tabs are the favorite because the pattern is well documented and easy to get almost right.

Almost is where candidates lose points: focus that does not follow the selection, every tab reachable with Tab, or a panel not linked to its tab. This exercise checks each of those details with tests, the same way a reviewer would.

What you will build

  1. Tabs are buttons with role="tab" inside a role="tablist"; the content sits in a role="tabpanel".
  2. The first tab starts selected (aria-selected="true"); the others are "false".
  3. Clicking a tab selects it and shows its content.
  4. ArrowRight and ArrowLeft move selection and focus, wrapping around at both ends.
  5. Home and End go to the first and the last tab.
  6. Only the selected tab has tabindex="0"; the others have "-1".
  7. The panel's aria-labelledby points to the selected tab, and the tab's aria-controls points to the panel.

Out of scope: Manual activation (selecting with Enter only). Vertical tabs.

How you will be graded

Passing the tests is the start. In the follow-up conversation, a good explanation covers:

  • Explains roving tabindex and why the other tabs get -1.
  • Says how focus follows the selection.
  • Mentions automatic versus manual activation and when each fits.

Starter code

/src/Tabs.tsx

type Tab = { id: string; label: string; content: string };
export function Tabs({ tabs }: { tabs: Tab[] }) {
// implemente aqui
return (
<div>
{tabs.map((tab) => <button key={tab.id} type="button">{tab.label}</button>)}
<div>{tabs[0].content}</div>
</div>
);
}
See the tests that will run /src/Tabs.test.tsx
import "@testing-library/jest-dom";
import { render, screen, fireEvent } from "@testing-library/react";
import { Tabs } from "./Tabs";
const TABS = [
{ id: "a", label: "Alpha", content: "First panel" },
{ id: "b", label: "Beta", content: "Second panel" },
{ id: "c", label: "Gamma", content: "Third panel" },
];
const tab = (name: string) => screen.getByRole("tab", { name });
const press = (name: string, key: string) => fireEvent.keyDown(tab(name), { key });
describe("Tabs", () => {
it("selects the first tab by default", () => {
render(<Tabs tabs={TABS} />);
expect(tab("Alpha")).toHaveAttribute("aria-selected", "true");
expect(tab("Beta")).toHaveAttribute("aria-selected", "false");
expect(screen.getByRole("tabpanel")).toHaveTextContent("First panel");
});
it("shows the content of a clicked tab", () => {
render(<Tabs tabs={TABS} />);
fireEvent.click(tab("Gamma"));
expect(tab("Gamma")).toHaveAttribute("aria-selected", "true");
expect(screen.getByRole("tabpanel")).toHaveTextContent("Third panel");
});
it("moves selection and focus with the arrow keys", () => {
render(<Tabs tabs={TABS} />);
press("Alpha", "ArrowRight");
expect(tab("Beta")).toHaveAttribute("aria-selected", "true");
expect(tab("Beta")).toHaveFocus();
});
it("wraps around at both ends", () => {
render(<Tabs tabs={TABS} />);
press("Alpha", "ArrowLeft");
expect(tab("Gamma")).toHaveAttribute("aria-selected", "true");
press("Gamma", "ArrowRight");
expect(tab("Alpha")).toHaveAttribute("aria-selected", "true");
});
it("jumps to the first and last tab with Home and End", () => {
render(<Tabs tabs={TABS} />);
press("Alpha", "End");
expect(tab("Gamma")).toHaveFocus();
press("Gamma", "Home");
expect(tab("Alpha")).toHaveFocus();
});
it("keeps only the selected tab in the tab order", () => {
render(<Tabs tabs={TABS} />);
expect(tab("Alpha")).toHaveAttribute("tabindex", "0");
expect(tab("Beta")).toHaveAttribute("tabindex", "-1");
expect(tab("Gamma")).toHaveAttribute("tabindex", "-1");
});
it("links the panel to the selected tab", () => {
render(<Tabs tabs={TABS} />);
fireEvent.click(tab("Beta"));
const panel = screen.getByRole("tabpanel");
expect(panel).toHaveAttribute("aria-labelledby", tab("Beta").id);
expect(tab("Beta")).toHaveAttribute("aria-controls", panel.id);
});
});

Hints

Open only if you get stuck. Each one gives away a little more.

Hint 1

Keep a single piece of state: the selected index. aria-selected, tabindex and the panel all come from it.

Hint 2

Changing the selection is not enough for keyboard users. Keep refs to the buttons and call focus() on the new one.

References