Native Dialog Element
Uses the HTML dialog element to create modal and non-modal dialogs with built-in focus management, backdrop, and JavaScript API.
Code
<!-- Modal dialog -->
<dialog id="modal" aria-labelledby="modal-title">
<form method="dialog">
<h2 id="modal-title">Confirm Action</h2>
<p>Are you sure you want to proceed?</p>
<menu>
<button value="cancel">Cancel</button>
<button value="confirm" autofocus>Confirm</button>
</menu>
</form>
</dialog>
<!-- Non-modal dialog -->
<dialog id="popup">
<p>This is a non-modal popup.</p>
<button onclick="document.getElementById('popup').close()">Close</button>
</dialog>
<button onclick="document.getElementById('modal').showModal()">
Open Modal
</button>
<button onclick="document.getElementById('popup').show()">
Open Popup
</button>
<style>
dialog {
border: none;
border-radius: 8px;
padding: 1.5rem;
max-width: 90vw;
box-shadow: 0 4px 20px rgba(0,0,0,0.15);
}
dialog::backdrop {
background: rgba(0, 0, 0, 0.5);
backdrop-filter: blur(2px);
}
</style>
<script>
const modal = document.getElementById('modal');
modal.addEventListener('close', () => {
console.log('Dialog result:', modal.returnValue);
});
</script>The <dialog> element provides native modal/non-modal dialogs. showModal() opens as modal (blocks interaction, shows backdrop), show() opens non-modal. ::backdrop pseudo-element styles the overlay. method="dialog" closes on button click with returnValue. Built-in focus management and Escape key handling. No external libraries needed.
Interactive Tools for This Topic
JSON to Zod Schema ConverterInfer typed Zod z.object schemas from JSON payloads locally, supporting nested objects, merged array shapes, enums, nullable fields, and JSDoc comments.JSON Schema ValidatorValidate JSON data against a JSON Schema definition and get detailed error reports. Supports type checking, required fields, and common constraint validation.JSONL / NDJSON InspectorValidate NDJSON and JSON Lines line-by-line, pinpoint syntax errors by line number, analyze key frequencies and types, and export a pretty JSON array.Credit Card Brand DetectorIdentify the card network from a number's prefix and length, validate the Luhn checksum, and show a masked form and the issuer identification number.
Report a problem with this exampleOpens a pre-filled issue on GitHub.