← Hub · Open the note

Dialogs (modals) — demo — BEFORE/AFTER

Goal: when a modal opens, focus goes into it, stays there, Escape closes it, and focus returns to the opener.

BEFORELooks like a modal, but behaves like a random popup

Problems: no dialog semantics, no focus move, no focus trap, no Escape close. Keyboard users can tab “behind” the overlay.

Background controls (still reachable):

Account settings

  • Try: click Edit profile, then press Tab repeatedly.
  • Notice: focus can move to the background, even though the overlay is visible.
  • Try: press Esc (nothing happens).
AFTERDialog with semantics + focus trap + Escape + focus restore

Pattern: role="dialog" + aria-modal="true", move focus in on open, keep Tab inside, close on Escape, restore focus to opener.

Background controls (disabled for keyboard focus while modal open):

Account settings

Tip: when the dialog is open, press Tab and Shift+Tab to see the focus trap.

Notes: this demo uses a simple JavaScript focus trap. In production, also ensure background content can’t be interacted with (for example, using inert where supported).

Quick checklist