WebCraft

showModal()

A modal that traps focus for you

Focus is trapped

Tab around: focus stays inside. Escape closes it. The backdrop behind is a real pseudo-element you can style.

Caveatshow() and showModal() are different elements in practice. Only the modal one gets ::backdrop, makes the rest of the page inert, traps focus and closes on Escape. Call the wrong one and you have a floating box with none of it.

FallbackClose it with <form method="dialog"> and the button needs no script at all. Escape fires cancel, not close, so intercept that one if you need to confirm.

HTML + JS
<dialog>
  <form method="dialog">
    <button>Close</button>
  </form>
</dialog>

// not .show() — that skips all of it
dialog.showModal();

Published