Fix · Critical · ships with most lawsuits

How to fix: keyboard trap in modal or widget

Tab inside a modal cycles forever; no way to escape with the keyboard. Keyboard users must always be able to leave a focus area.

WCAG references
2.1.2
Severity
Critical

What it looks like in the wild

Tab inside a modal cycles forever; no way to escape with the keyboard.

The screen-reader user, keyboard user, or low-vision user encountering this issue does not get an error message. The page just stops working for them. Which is why this kind of bug rarely shows up in your error tracker; it shows up in support tickets, lawsuits, and abandoned conversions.

Why it fails WCAG

Keyboard users must always be able to leave a focus area.

Map this back to 2.1.2 when you log the bug. Auditors and procurement teams expect that mapping; "broken" is not enough context.

The wrong pattern (avoid)

JavaScript
// custom modal with no Esc handler

The right pattern (copy this)

JavaScript
import { FocusTrap } from 'focus-trap-react';

<FocusTrap>
  <div role="dialog" aria-modal="true">
    <button aria-label="Close" onClick={close}>×</button>
    <button onClick={close}>Cancel</button>
    <button onClick={save}>Save</button>
  </div>
</FocusTrap>

Notes from real audits

  • Esc must close. Focus must restore to the trigger when closed.

Related fixes

Find every accessibility issue on your site in 60 seconds.

Free public scan. No card. AI-generated fixes for every issue we find.