Accessibility in React apps

Intermediate · 36 min

Semantic JSX, focus management after route changes and modals, ARIA attributes, and automated testing with axe and jest-axe.

Before thisRendering lists and keysForms and controlled inputs

GrUSP · 36 min

Accessibility in React apps

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by GrUSPReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • A native interactive element like <button> already has the right role, keyboard handling, and focus behavior built in; a <div onClick> has none of that for free and makes you rebuild it by hand, usually incompletely.
  • React doesn't change the rules of accessible HTML — it changes how easy it is to accidentally skip them, because JSX makes it just as easy to write a clickable div as a real button.
  • After a client-side route change or opening a modal, focus does not move automatically the way a full page load would move it, so an app has to move focus deliberately or keyboard and screen reader users lose their place.
  • ARIA attributes fill gaps native HTML can't express on their own, such as aria-expanded on a custom dropdown trigger or aria-live on a region that updates without a page reload, but they should only be reached for once a native element can't do the job.
  • Automated tools like axe and jest-axe catch a meaningful slice of accessibility bugs, such as missing labels or insufficient color contrast, directly in a unit test, but they cannot verify things like whether focus order makes sense or whether an interaction is actually usable by keyboard alone.
  • Accessibility bugs in a React app are overwhelmingly about using the wrong element or abandoning focus, not about some React-specific gotcha, which is why the fixes are mostly a matter of writing more honest HTML.

Most accessibility bugs are just the wrong element

JSX makes it trivially easy to write <div onClick={handleClick}>Delete</div> instead of <button onClick={handleClick}>Delete</button>. Visually they can look identical. Functionally they are not close:

// Looks clickable. Is not keyboard-reachable, not announced as a button,
// and Space/Enter do nothing unless you wire them up yourself.
<div onClick={handleDelete} className="delete-btn">Delete</div>

// Focusable by default (Tab reaches it), activatable by Enter and Space,
// announced to screen readers with the button role — for free.
<button onClick={handleDelete} className="delete-btn">Delete</button>

The general rule: reach for the native element whose job matches what you’re building — <button> for anything clickable that isn’t a navigation, <a href> for navigation, <label> paired with a form control, <ul>/<ol> for lists. Each of these ships with keyboard behavior, a default role, and focus handling that you would otherwise have to reimplement, and reimplementations tend to miss a case the native element handles automatically.

Focus management after navigation and modals

A traditional multi-page site resets focus to the top of the document on every navigation, because the browser loads a new page. A single-page React app doesn’t get that for free — a client-side route change or a modal opening just swaps DOM in place, and focus stays exactly where it was, which can leave a keyboard or screen reader user stranded on a control that no longer makes sense.

function useFocusOnRouteChange() {
  const location = useLocation();
  const headingRef = useRef(null);

  useEffect(() => {
    headingRef.current?.focus();
  }, [location.pathname]);

  return headingRef;
}

function PageHeading({ children }) {
  const ref = useFocusOnRouteChange();
  return (
    <h1 ref={ref} tabIndex={-1}>
      {children}
    </h1>
  );
}

tabIndex={-1} makes an element programmatically focusable without adding it to the normal Tab order, which is exactly what you want for “move focus here once, as an announcement” rather than “add this to keyboard navigation.” The same idea applies to a modal: on open, move focus to the modal’s first focusable element or its heading, and on close, return focus to whatever element opened it.

ARIA: filling gaps native HTML leaves

ARIA attributes exist for the cases native HTML genuinely can’t express — a custom dropdown has no native “expanded” state, for instance:

function Dropdown({ label, isOpen, onToggle, children }) {
  return (
    <div>
      <button aria-expanded={isOpen} aria-haspopup="listbox" onClick={onToggle}>
        {label}
      </button>
      {isOpen && <ul role="listbox">{children}</ul>}
    </div>
  );
}

aria-expanded tells assistive technology whether the dropdown is currently open, information a sighted user gets for free from the visual change. The rule of thumb is ARIA as a supplement, not a substitute: a <div role="button"> with manually wired keyboard handling is strictly worse than an actual <button>, because the native element’s behavior is more complete than what role and aria-* attributes alone reconstruct.

Automated testing with axe and jest-axe

jest-axe runs the axe accessibility engine against a component’s rendered output inside an ordinary test, catching a real class of bugs automatically:

import { render } from "@testing-library/react";
import { axe, toHaveNoViolations } from "jest-axe";

expect.extend(toHaveNoViolations);

test("the signup form has no accessibility violations", async () => {
  const { container } = render(<SignupForm />);
  const results = await axe(container);
  expect(results).toHaveNoViolations();
});

This catches missing form labels, insufficient color contrast, images without alt text, and similar structural problems, directly in CI, before a human ever reviews the page. It is not a complete accessibility test, though: axe inspects the rendered markup, so it has no way to judge whether your focus-management logic actually works, or whether a feature is genuinely operable using only a keyboard. Automated checks and manual keyboard/screen-reader testing are complementary, not substitutes for each other.

What the video covers

Julia Undeutsch’s conference talk, hosted by GrUSP, frames React accessibility as the HTML and CSS rules you likely already know, applied inside components, and covers semantic markup and ARIA at the level of a real application. It doesn’t go deep on focus management after route changes or on automated testing with axe/jest-axe, which is why this lesson covers both of those directly rather than deferring to the talk.

Where this fits

This lesson depends on knowing how lists render and how forms capture input, since both are common places accessibility gets skipped. Next: choosing a React framework.

Check yourself

  1. 01Why does replacing a <div onClick={...}> with a <button onClick={...}> fix more than just styling?

    A <button> is natively focusable, reachable by Tab, activatable with both Enter and Space, and automatically exposed to assistive technology with the button role, all of which a <div> lacks unless you manually add tabIndex, a role, and keydown handling for Enter and Space — work that's easy to forget or get subtly wrong.

  2. 02Why does opening a modal typically require moving focus into it manually in React, when a traditional multi-page site handles focus automatically on navigation?

    A full page load naturally resets focus to the top of the new document, but a modal opened by client-side React just inserts new DOM into the existing page without any browser-level focus reset, so if nothing calls .focus() on an element inside the modal, keyboard and screen reader focus stays wherever it was before the modal opened, effectively leaving the user behind.

  3. 03When should you reach for an ARIA attribute instead of a native HTML element?

    Only when no native element already expresses what you need — ARIA should supplement HTML's own semantics for custom widgets like a combobox or a live-updating region, not replace a native element that already does the job, since a native element's built-in behavior is more complete and less error-prone than role and aria- attributes bolted onto a generic div.

  4. 04What kind of accessibility bug can axe or jest-axe catch automatically, and what kind can they not catch?

    They can automatically catch structural and markup-level issues like a missing form label, insufficient color contrast, or an image without alt text, but they cannot judge whether a page's focus order makes logical sense or whether a feature is genuinely usable end-to-end with only a keyboard, since those require actually exercising the interaction rather than inspecting the rendered markup.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark