On this page
  1. Vanilla Interactive Components
  2. Contents
  3. Accessible Modal Dialog (HTML + CSS + JS)
  4. Accessible Tabs with Event Delegation

Vanilla Interactive Components

Prev: tutorials | Up: tutorials | Next: Forms, Validation & Fetch
Rel: Native Interactive Patterns, Events & Delegation, Pseudo-Elements
Ref: MDN Web Docs — dialog
Tags: #public #web #platform #tutorials #components


Contents


Accessible Modal Dialog (HTML + CSS + JS)

Combining <dialog>, ::backdrop, and showModal() delivers focus trapping, Escape handling, and return values with minimal code:

<button type="button" id="open-modal">Delete Item</button>

<dialog id="confirm-modal">
  <form method="dialog">
    <h2>Confirm Deletion</h2>
    <p>This action cannot be undone.</p>
    <menu>
      <button value="cancel">Cancel</button>
      <button value="delete">Delete</button>
    </menu>
  </form>
</dialog>
const modal = document.getElementById("confirm-modal");
document.getElementById("open-modal").addEventListener("click", () => modal.showModal());
modal.addEventListener("close", () => {
  if (modal.returnValue === "delete") {
    console.log("Confirmed delete");
  }
});

Accessible Tabs with Event Delegation

Toggle aria-selected on [role="tab"] buttons and hidden on their associated [role="tabpanel"] elements using a single delegated click listener on [role="tablist"].

Linked from

Related notes