HTML5 & Web APIs (50 Qs)•Part 2: Core Architecture & Implementation (Mid-Level)
What is the native HTML `<dialog>` element (`.showModal()` vs. `.show()`)?
senior LevelCode ExampleSystem Design
In Simple Words:
Before HTML5 <dialog>, building accessible modal dialogs required hundreds of lines of JavaScript to manage focus traps, overlay backdrops, and ESC key handling.
The native <dialog> handles all of this automatically:
html
<dialog id="confirm-modal">
<form method="dialog">
<p>Are you sure you want to delete this repository?</p>
<button value="cancel">Cancel</button>
<button value="confirm" autofocus>Delete</button>
</form>
</dialog>
javascript
const dialog = document.getElementById('confirm-modal');
// 1. .showModal(): Opens as true MODAL
// - Placed in the browser's native Top Layer (above all z-indices!)
// - Injects native ::backdrop pseudo-element
// - Automatically traps keyboard focus inside dialog
// - Automatically closes on ESC key press
dialog.showModal();
// 2. .show(): Opens as non-modal inline popup (does not trap focus or dim background)