Remember
Store the control that opened the dialog.
Commerce · Concept
A modal is more than an overlay. It needs a name, deliberate focus, contained keyboard navigation, Escape support, and a reliable return point.
The principle
Use <dialog> and showModal() before rebuilding modal behavior with divs and ARIA.
Focus lifecycle
The modal journey begins before opening and ends only after focus returns to a logical place in the page.
Store the control that opened the dialog.
Call showModal() so outside content becomes inert.
Move focus to the title or first logical control inside.
After close or Escape, return focus to the opener.
Keyboard-testable demo
cancel behavior.Do / Don’t
Visual layering does not make the background inert, contain focus, expose a dialog name, or implement Escape.
Keyboard focus can escape behind it, and assistive technology may not know a dialog opened.
<div class="overlay">
<div class="cart">…</div>
</div>Add an accessible name, logical initial focus, visible close control, and focus restoration around native behavior.
<dialog aria-labelledby="cart-title">
<h2 id="cart-title" tabindex="-1">Your cart</h2>
…
</dialog>
dialog.showModal();Quick test