On this page
  1. Native Interactive Patterns
  2. Contents
  3. Zero-JS Popover Menu (popover)
  4. Native Modal Dialog (<dialog>)

Native Interactive Patterns

Prev: Media & Responsive Images | Up: tutorials | Next: css
Rel: Interactive & Scripting, Global Attributes, Vanilla Interactive Components
Ref: MDN Web Docs — Popover API
Tags: #public #web #html #tutorials #interactive


Contents


Zero-JS Popover Menu (popover)

The popover attribute promotes an element to the top layer with built-in Escape and click-outside ("light dismiss") behavior:

<button popovertarget="info-tip">Show info</button>
<div id="info-tip" popover>
  <p>This popover closes automatically when you press Escape or click outside.</p>
</div>

Native Modal Dialog (<dialog>)

Pair <dialog> with <form method="dialog"> to close the modal natively and capture dialog.returnValue:

<dialog id="confirm-dialog">
  <form method="dialog">
    <p>Delete this item?</p>
    <button value="cancel">Cancel</button>
    <button value="confirm">Delete</button>
  </form>
</dialog>

Open with document.getElementById("confirm-dialog").showModal() so the browser traps focus, renders ::backdrop, and restores focus to the trigger button on close.

Linked from

Related notes