StealThis .dev

Alert Dialog

Confirmation modal with destructive/cancel actions, focus trap, backdrop click to close, and keyboard Escape support.

Open in Lab
css vanilla-js
Targets: JS HTML

Code

Alert Dialog

A fully accessible alert dialog for destructive or confirmation-requiring actions. Unlike a regular modal, an alert dialog demands an explicit user decision before proceeding.

Features

  • Focus trap — keyboard focus is locked inside the dialog while open
  • Escape to close — pressing Escape dismisses the dialog
  • Backdrop click — clicking outside the panel closes it
  • Destructive action style — primary action uses a danger color to signal intent
  • ARIA roles — uses role="alertdialog" and aria-describedby for screen readers
  • Scroll lockbody scroll is disabled while the dialog is open

When to use

Use an alert dialog (not a regular modal) whenever the action is irreversible or has significant consequences — deleting data, revoking access, cancelling a subscription, etc.

Accessibility

  • role="alertdialog" signals to assistive technologies that a response is required
  • aria-labelledby and aria-describedby provide context for screen readers
  • Focus moves to the dialog on open and returns to the trigger on close