The delete-account modal on the neighbourhood head’s page: where does Bayu’s Tab go from the last button? A trap, a button added later, inert, showModal()…
Why it matters
Modals are everywhere: login, delete confirmations, forms. For mouse users a div and a dim backdrop look fine. For keyboard and screen reader users, with no trap they "Tab behind" the dialog; with a trap that has no door, they are locked in. Both bugs slip past QA because a mouse never shows them.
The key idea
Four jobs of a modal: hold focus, switch off the page behind, keep a door, give focus back
Common mistakes
Links behind the modal still take mouse clicks although focus is trapped A Tab loop only holds Tab. A backdrop that is merely dimmed still takes clicks, and screen readers still read it. Put inert on everything outside the modal (or use <dialog>.showModal(), which does it for you).
The modal closes and focus drifts to <body> The focused element is hidden too, so the browser moves focus to <body>. Keep the trigger when opening, call trigger.focus() when closing.
A button added to the modal is skipped by Tab Do not keep first/last from when the modal opened. Look up the focusable elements on every Tab (a[href], button, input, select, textarea, [tabindex]:not([tabindex="-1"]), not disabled and visible), or use <dialog>.showModal().
Try it in this playground
Trap none: open the modal, keep tabbing After "Hapus", focus lands on "Artikel terbaru" behind the modal, and Bayu is startled.
Trap loop: Tab past the last button The handler stops the default Tab and moves focus to the start. Watch the e.preventDefault() line light up.
Close the modal without "return focus"document.activeElement becomes <body>: Mr. Sugeng goes quiet, and the next Tab starts again from the top.
Open The clean-up ad Tab is held and Esc is off: a keyboard trap. Bayu cannot get out.