On this page
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
- tuts/web/tutorials Linked here; links to this page; shared tags: #platform, #tutorials, #web
- tuts/web/tutorials/forms_validation_and_fetch Linked here; links to this page; shared tags: #platform, #tutorials, #web
- tuts/html/tutorials/native_interactive_patterns Linked here; links to this page; shared tags: #tutorials, #web
- tuts/web/dom/events_and_delegation Linked here; links to this page; shared tags: #platform, #web
- tuts/css/selectors/pseudo_elements Linked here; shared tags: #web
- tuts/javascript links to this page; shared tags: #web
- tuts/web/tutorials/devtools_and_debugging shared tags: #platform, #tutorials, #web
- tuts/web/tutorials/progressive_enhancement_and_accessibility shared tags: #platform, #tutorials, #web
- tuts/css/tutorials shared tags: #tutorials, #web
- tuts/css/tutorials/box_model_and_formatting_contexts shared tags: #tutorials, #web