FRENVersion française

Overlays & display

Modal dialog

Fenêtre modale

Demo

Loading demo…

A window laid over the page that blocks the rest of the interface while it is open. It asks for a decision or a short input without leaving the current screen.

Story

Did you know

The word "modal" comes from the idea of a mode: while the window is open, the whole interface switches into another mode. Larry Tesler, the inventor of copy and paste, spent his career fighting modes; his licence plate read "NO MODES".

Timeline

  1. 1980sModal dialog boxes arrive with the Mac and Windows.
  2. 2000sJS "lightboxes" replace the browser pop-ups that were being blocked.
  3. 2022<dialog> is finally supported by every browser.

Vocabulary

Names

FR
Fenêtre modale, boîte de dialogue, pop-in, popup
EN
Modal dialog, modal, dialog, dialog box, lightbox, overlay

Common uses

  • Confirm a destructive action
  • Short form without leaving the page
  • Show an image full size (lightbox)
  • Block until a decision is made (cookies, paywall)

Often paired with

The components it usually sits with, by page type, in reading order.

Code

<button id="trigger">Delete project</button>

<dialog id="dlg" aria-labelledby="dlg-title" aria-describedby="dlg-desc">
  <h2 id="dlg-title">Delete project?</h2>
  <p id="dlg-desc">This can't be undone.</p>
  <form method="dialog">
    <button value="cancel">Cancel</button>
    <button value="confirm">Delete</button>
  </form>
</dialog>

<script>
  trigger.onclick = () => { dlg.returnValue = ''; dlg.showModal() }
  dlg.onclose = () => {
    if (dlg.returnValue === 'confirm') console.log('Project deleted')
  }
</script>

Good to know

  • Only showModal() makes the page inert and puts the dialog in the top layer. The open attribute or show() give you a non-modal dialog.
  • form method="dialog" closes the dialog and puts the button value in returnValue. Gotcha: Escape doesn't reset it, so clear it before reopening.
  • Style the overlay with ::backdrop. closedby="any" closes on outside click in recent browsers, though not everywhere yet: keep a JS fallback if you need it.
  • The page behind still scrolls. Lock it with html:has(dialog:modal) { overflow: hidden }.

Accessibility

Must have

  • Name the dialog with aria-labelledby pointing to its title, and aria-describedby pointing to the explanation. For an urgent confirmation, add role="alertdialog".
  • On open, focus moves into the dialog and stays there while it is open. Escape closes it.
  • A visible button to close or cancel. Clicking the backdrop is not enough for keyboard users, or for everyone else.

Often forgotten

  • Return focus to the trigger button on close. <dialog> does it for you. If the button is gone, pick a logical target.
  • A home-made modal must make the rest of the page inert (inert attribute), or screen readers keep reading what is behind it.

Nice to have

  • For a deletion, put focus on Cancel (first button or autofocus), so a reflex Enter deletes nothing.
  • If the content is long, focus the title (tabindex="-1") so reading starts at the top.
  • Respect prefers-reduced-motion if you animate the opening.

Who uses it