<dialog> Element and Native Top Layer
HTML primitive placing modals in the browser Top Layer above any z-index, with built-in focus management and Escape key handling.
About this page
Interactive Simulator
Modal: Portal + Focus-Trap vs <dialog>
Lines of Code
Reflows / Browser Cost
BeforeDiv + React Portal + focus-trap
<Portal>
<div role="dialog" aria-modal="true"
style={{ zIndex: 9999 }}>
{/* + Escape listener + focus trap */}
</div>
</Portal>Lines: 45Reflows: Body mutationDependencies: focus-trap (~8 kB)
Remains vulnerable to z-index conflicts across portals.
After (Native)Native <dialog>.showModal()
<dialog ref={ref}>
<form method="dialog">
<button>Close</button>
</form>
</dialog>
// ref.current.showModal()Lines: 6Reflows: Isolated Top LayerDependencies: 0 kB
Inertness, Escape, and ::backdrop handled by the engine.
Live DOM Sandbox:
Under the Hood#
dialog.showModal()appends the element to theDocumentinternaltop layerlist, painted after the root CSS Stacking Context.- The engine generates a
::backdroppseudo-element immediately beneath the dialog inside the same Top Layer. - Every node outside the dialog is marked
inert: pointer clicks, text selection, and keyboard tabbing are blocked natively without global listeners.
HTML
<dialog id="confirm-modal">
<p>Delete production index?</p>
<form method="dialog">
<button value="cancel">Cancel</button>
<button value="confirm">Confirm</button>
</form>
</dialog>
<script>
document.getElementById('confirm-modal').showModal();
</script>Problem Genealogy#
- 2012 (React Portals +
z-index: 9999): Moving modals manually todocument.bodyto escapeoverflow: hidden, a ceremony that only existed because CSS lacked a real Top Layer. - 2018 (
focus-trap+ manualaria-hidden): Fragile JS boilerplate to trap Tab navigation and hide background nodes from screen readers. - 2022+ (
<dialog>Baseline): Top Layer,::backdrop,inert, andEscapedismissal built directly into the engine.
Production Pitfalls#
- Calling
.show()instead of.showModal():.show()activates neither the Top Layer, nor::backdrop, nor background inertness, leaving your modal trapped in the samez-indexswamp you were trying to escape. - Exit animations:
dialog.close()switches immediately todisplay: none; combine@starting-stylewithtransition-behavior: allow-discreteto animate exits.
Where this connects
Next & related
DOM & Events
Popover API (HTML popover Attribute)
Reuses the engine's Top Layer mechanism without making the rest of the document inert.
DOM & Events
React Portals and z-index Layering (Legacy Pattern)
Native showModal() replaces React createPortal and z-index hacks with engine-managed Top Layer and inert.
Rendering Pipeline
CSS Stacking Context
The Top Layer bypasses the entire stacking context hierarchy and overflow: hidden clipping.
Mechanical Comparisons
- Contrasts With<dialog> Element and Native Top Layer vs CSS Stacking Context
The Top Layer bypasses the entire stacking context hierarchy and overflow: hidden clipping.
- Replaces<dialog> Element and Native Top Layer vs React Portals and z-index Layering (Legacy Pattern)
Native showModal() replaces React createPortal and z-index hacks with engine-managed Top Layer and inert.