Open a modal, keep pressing Tab from its last element, and with no handling in place, focus slides right off into the page content hidden behind it — invisible on screen but still read aloud by a screen reader, a deeply confusing state. A focus trap wires the modal's last element back to its first, so Tab only ever cycles within it.
Despite the name, the goal isn't to imprison the user — it's to make the **one way out** unmistakable, via Escape or a close button, always reachable. A trap with no way out is itself a violation of 2.1.2 (No Keyboard Trap).
The demo auto-cycles Tab through three elements inside a modal: from the last one it wraps back to the first, and focus never crosses into the dimmed background.
When to use
Prefer a battle-tested library (focus-trap, Radix) or the native <dialog> element's showModal() over hand-rolling it — <dialog> gives you the trap and Escape-to-close for free.