Skip to main content
InterviewHubPRO
Section Overview
Q37

Question 37 of 50

Part 3: Advanced Internals, Scale & Production Architecture (Senior / Lead)

74%
HTML5 & Web APIs (50 Qs)•Part 3: Advanced Internals, Scale & Production Architecture (Senior / Lead)

HTML `<dialog>` Element vs Custom `<div>` Modals: Focus Trapping and `::backdrop`.

senior LevelCode ExampleSystem Design

In Simple Words: The native <dialog> element replaces custom modal libraries:

  • dialog.showModal(): Opens a true accessible modal in the browser's #top-layer, disables interaction with the rest of the page (inert), automatically traps keyboard focus, and closes on Escape.
  • dialog.show(): Opens a non-modal dialog where background elements remain interactive.
  • ::backdrop pseudo-element: Styles the dimmed overlay behind the modal.
html
<dialog id="fav-dialog">
  <form method="dialog">
    <p>Confirm account deletion?</p>
    <button value="cancel">Cancel</button>
    <button value="confirm">Confirm</button>
  </form>
</dialog>

<script>
  const dialog = document.getElementById('fav-dialog');
  dialog.showModal(); // Opens with native focus trap and top-layer rendering
  dialog.addEventListener('close', () => {
    console.log('Dialog closed with return value:', dialog.returnValue);
  });
</script>