Accessibility Built Once: Shared Components and Tests That Query by Role

A team adds accessibility after launch, and the audit comes back with a familiar list: inputs without labels, a modal you can tab straight out of, error text no screen reader announces. The odd part is not the length of the list. It is that the same handful of faults repeats on screen after screen, because every screen rebuilt the same widgets by hand.

Fix it where screens are made

The book's argument for accessibility is an argument about placement. The UI layer has its own gradient of change, from rarely touched building blocks at the bottom to pages that are rewritten constantly at the top. Accessibility is cheapest at the calm end, in the shared components that every feature is assembled from. If the button, the modal and the data table behave correctly, everything built on them behaves correctly too, and the book names WCAG 2.2 level AA as the target.

Think about what the alternative costs. Ten features each hand-roll a confirmation dialog, so you need ten audits, ten fixes, and a way to stop an eleventh from regressing. One well-built dialog needs one audit, and the features can only get it wrong by refusing to use it.

What "built correctly" means for one component

Take the dialog, the component that fails most often. A trustworthy one needs a short contract, none of which is specific to LVD:

  • It is built on the platform's own semantics wherever possible, and ARIA fills only the gaps the platform leaves. A real button beats a styled div with a click handler.
  • Opening it moves keyboard focus inside and keeps it there, and closing it hands focus back to whatever opened it.
  • It has an accessible name, so assistive technology can say what the dialog is for.

Write that down once, build it once, and let feature code treat it as a black box.

Tests that look like a user

Testing is where the placement argument pays off a second time. The book recommends locating elements as a person would, through their role and the name shown on them, and checking outcomes a person could observe. That has a bonus: a test that asks for a button called "Change delivery address" fails the moment the button loses its name, so the test suite doubles as an accessibility check.

it("returns focus to the trigger when the dialog is dismissed", async () => {
  render(<AddressSettings />);
  const trigger = screen.getByRole("button", { name: "Change delivery address" });

  await userEvent.click(trigger);
  expect(screen.getByRole("dialog", { name: "Delivery address" })).toBeVisible();

  await userEvent.keyboard("{Escape}");
  expect(trigger).toHaveFocus();
});

Nothing in the test mentions a CSS class, a component's internal structure or a test identifier. You can restyle the dialog, swap its internals or move it to a new library, and the test still means the same thing. If it fails, a real user would have noticed too.

Where each test should live

Put this test next to the shared dialog and not inside any feature. It then runs once, covers every dialog in the product, and fails in one obvious place. Feature tests can stay focused on what the feature does, trusting the building blocks beneath them.

What automation cannot see

A caveat from ordinary practice, not from the book: automated checks catch only a fraction of accessibility problems. A script can tell you a label is missing, but it cannot tell you whether the reading order makes sense or whether a screen reader user can complete checkout. Pair role-based tests with occasional manual passes, by keyboard alone and with a screen reader, and treat the shared components as the place where the findings get fixed permanently.

The takeaway

Accessibility is a placement decision before it is a compliance exercise. Build the behaviour once in the layer's calm bottom, query by role so the tests notice when it breaks, and let every feature inherit the result. For the testing strategy around the UI, read layer-specific testing for the upper layers, and for how the code surrounding those components should behave, thin components and honest state.

What the book covers

  • The book's full list of obligations for shared components, including how streaming agent output is announced
  • Design tokens as the UI layer's most stable artefact
  • Extracting components and styles into a versioned design-system package shared by several applications
  • The UI's own testing pyramid, ordered by the same gradient as its folders
  • Mocking at the network boundary and reserving browser tests for the costliest journeys