liveBook · Manning

Accessibility

As explained in React in Depth

The property of software being operable and understandable by people with diverse abilities and assistive technologies.

A React interface must be understandable and usable beyond its visual presentation. WAI-ARIA defines roles that help describe web elements for nonvisual interaction. Many elements have implicit roles: an anchor is a link, a list item is a listitem, and a button is a button. Elements also have accessible names, which may come from text or, for an image, its alternative text. section 10.1

Listing 10.1 Unit-testing MenuItem
import {    #1
  render,    #1
  screen,   #1
  getByRole,  #1  
} from "@testing-library/react";   #1
import "@testing-library/jest-dom";    #2
import MenuItem from "./MenuItem";     #3
test(    #4
  "MenuItem renders a link in a list item",   #4
  () => {   #4
  // ARRANGE
  render(<MenuItem href="/blog" label="Blog" />);    #5
  // ASSERT
  const listItem = screen.getByRole("listitem");  #6
  const link = getByRole(listItem, "link");    #6
  expect(link).toHaveAttribute("href", "/blog");     #7
  expect(link).toHaveAttribute("title", "Blog");   #7
  expect(link).toHaveTextContent("Blog");   #7
});

The MenuItem test renders a React component, finds its list item and nested link by role, then checks the link’s destination, title, and text. Role queries target the element as a user or assistive technology would identify it, rather than relying on a class name or tag selector. section 10.1

Role-based tests are a useful check, not a complete accessibility review. Even components that address WAI-ARIA guidance, aria-* properties, screen readers, and keyboard access can leave substantial gaps: section 10.1 section 13.3

For a reusable UI library, check whether components also:

  • Can be translated and behave correctly in right-to-left writing modes. section 13.3
  • Respect prefers-color-scheme, prefers-reduced-motion, and device high-contrast settings. section 13.3
  • Scale when the user increases the browser font size. section 13.3

Tooling can help, but it does not replace those checks. React Aria is an accessibility toolbox within the React Spectrum family, alongside the React Spectrum UI library and React Stately state-flow library. Choose tools in the context of the components and behaviours you need to support. section 1.4.1 section 13.3

How React in Depth covers it

How deep it goes
Explains implicit roles and accessible names through a component test, then inventories UI-library gaps without detailing WAI-ARIA internals.
Examples
Uses a MenuItem role-query test and a UI-library checklist covering translation, display preferences, motion, contrast, and font scaling.
What it assumes
Assumes React component work; introduces ARIA roles for testing while leaving most WAI-ARIA specification detail aside.
What you can do after
Apply it via React Testing Library role queries, and evaluate reusable components against the identified accessibility gaps.
How it connects
Connects accessibility to WAI-ARIA, accessible names, React component testing, internationalization, keyboard access, and browser preferences.

Also explained in

Compare how 6 books explain accessibility →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of React in Depth. The section links point to the parts of the book it draws on.