dialog

The native HTML modal dialog element with built-in backdrop, focus trapping, and keyboard handling.

Description

The <dialog> element provides a native way to create modal dialogs, confirmation prompts, and slide-in drawer panels. It handles complex accessibility requirements automatically: focus trapping, ESC to close, role="dialog", and inert background.

There are two ways to open a dialog:

  • HTML Invokers API (recommended) — command="show-modal" / command="close"
  • JavaScriptshowModal() / show() / close()

In Vanilla Breeze, dialogs also serve as drawer panels via data-position, and are used internally by web components like <command-palette>, <short-cuts>, and <settings-panel>.

When to Use

  • Confirmation dialogs: Confirm destructive actions before proceeding
  • Form dialogs: Collect user input in an overlay
  • Alert messages: Important information requiring acknowledgment
  • Media lightboxes: Images or videos in a focused overlay
  • Drawer panels: Slide-in navigation, settings, or filters (via data-position)

When Not to Use

Invokers API (Declarative)

The Invokers API lets you open and close dialogs with HTML attributes alone — no JavaScript required.

Available Commands

Command Description
show-modal Opens dialog as modal (equivalent to showModal())
close Closes the dialog (equivalent to close())
request-close Requests closure (can be cancelled with cancel event)

Browser Support

Invokers API is supported in Chrome 135+, Edge 135+, Safari TP, and Firefox Nightly. Include the polyfill for broader support:

showModal() vs show()

Feature showModal() show()
Backdrop Yes — via ::backdrop No
Focus trap Yes — focus stays inside No — focus can leave
ESC to close Yes — native behavior No
Inert background Yes — page is non-interactive No — page remains interactive
Top layer Yes — above all content Normal stacking context

Size Variants

Use data-size to control dialog width:

Attribute Width Use Case
data-size="s" 24rem (384px) Simple confirmations, alerts
default 32rem (512px) Standard dialogs, forms
data-size="l" 48rem (768px) Complex content, tables
data-size="full" 95vw / 95dvh Full-screen experiences

Drawer Variants

Use data-position to turn any dialog into a slide-in drawer panel. No additional JavaScript required — drawers inherit all dialog features: Invokers API, ESC to close, focus trapping, backdrop.

Attribute Position Use Case
data-position="end" Right (in LTR) Navigation, settings panels
data-position="start" Left (in LTR) Sidebar, filters
data-position="bottom" Bottom Mobile action sheets, share menus
data-position="top" Top Banners, announcements

Slide animations respect prefers-reduced-motion, falling back to a simple fade.

Bottom Sheet

The bottom drawer variant (data-position="bottom") acts as a mobile-native bottom sheet — ideal for action menus, share sheets, and quick option lists. It automatically includes safe-area padding for devices with home indicators.

Drag to Dismiss

Add data-gesture="dismiss-down" to let users swipe the bottom sheet downward to close it. VB’s gesture library handles the drag tracking, opacity falloff, and snap-back automatically. The dialog closes with returnValue set to "dismiss".

The gesture is lazy-loaded — it only ships when [data-gesture] attributes are present on the page. Buttons and links inside the sheet remain interactive; the drag only activates from non-interactive areas.

See also: Bottom Sheet on the Mobile page

Dialog Structure

For consistent styling, use <header>, content, and <footer>:

Structural CSS

Backdrop Styling

The ::backdrop pseudo-element styles the overlay behind modal dialogs. VB defaults to a semi-transparent dark background with a subtle blur.

Animation

Dialogs include smooth entry animations using both @starting-style (for supporting browsers) and keyframe fallbacks. Drawer variants use directional slide-in animations. All animations respect prefers-reduced-motion.

Drawer Animations

JavaScript API

Methods

Method Description
showModal() Opens dialog as modal with backdrop and focus trap
show() Opens dialog as non-modal
close(returnValue?) Closes dialog, optionally setting returnValue

Properties

Property Type Description
open boolean Whether the dialog is currently open
returnValue string Value set by close() or method="dialog" form submission

Events

Event Description
close Fired when the dialog is closed
cancel Fired when ESC is pressed (can be prevented with event.preventDefault())

Backdrop Click to Close

Native dialogs do not close when clicking the backdrop. Add this small script if you want that behavior:

Accessibility

Native Features

The <dialog> element provides these accessibility features automatically:

  • Focus trapping: Tab key cycles through focusable elements inside the dialog
  • Focus restoration: Focus returns to the triggering element when closed
  • Inert background: Background content is non-interactive when modal is open
  • ESC to close: Standard keyboard dismissal (can be prevented via cancel event)
  • ARIA semantics: Automatically has role="dialog"

Best Practices

  • Include a visible close button
  • Use a heading in the dialog (<h3> in <header>) for screen reader context
  • Keep dialogs focused — don't include unrelated content
  • Provide keyboard alternatives for all actions
  • Avoid nesting dialogs inside dialogs

Keyboard Support

Key Action
Tab Move focus between focusable elements (trapped inside modal)
Shift + Tab Move focus backwards (trapped inside modal)
Escape Close the modal dialog

Print Behavior

Dialogs are hidden entirely in print stylesheets — they are transient UI and should not appear on paper.

Components Using Dialog

Several VB web components use dialog patterns internally:

Component How It Uses Dialog
<command-palette> Keyboard-driven command modal with search filtering
<short-cuts> Keyboard shortcuts reference modal
<settings-panel> Settings popover with role="dialog"

Related

  • <details> — For inline expandable content (not overlays)
  • <form> — Forms inside dialogs with method="dialog"
  • <button> — Trigger buttons for opening dialogs (via Invokers API)
  • <header> — Dialog title area
  • <footer> — Dialog action button area
  • <tool-tip> — For non-blocking hints and tips
  • <toast-msg> — For transient notifications
  • <drop-down> — For dropdown menus

Snippets

Ready-to-use dialog patterns:

Modal Dialog

Basic, confirmation, form, and scrollable dialog patterns

Drawer Dialog

Slide-in panels for navigation, settings, and filters

Bottom Sheet

Mobile action menus, share sheets, and option lists

Browser Support

The <dialog> element is supported in all modern browsers.

  • Invokers API: Chrome 135+, Edge 135+, Safari TP, Firefox Nightly. Use the invokers-polyfill for broader support.
  • @starting-style: Chrome 117+, Safari 17.5+. Other browsers fall back to keyframe animations.
  • backdrop-filter: All modern browsers. The blur effect on the backdrop degrades gracefully.