HTML

Native <dialog> Modal

admin by @admin ADMIN
1h ago
May 31, 2026
Public
0 0 up · 0 down Sign in to vote
The HTML `<dialog>` element gives you a real modal — focus trap, Escape-to-close, backdrop overlay — without any JS library. Method `.showModal()` opens; `.close()` closes.
HTML
Raw
<button id="open-dialog">Open dialog</button>

<dialog id="confirm-dialog">
    <form method="dialog">                              <!-- form method=dialog closes on submit -->
        <h2>Delete this snippet?</h2>
        <p>This permanently deletes the snippet and its comments.</p>
        <menu>
            <button value="cancel">Cancel</button>
            <button value="confirm" autofocus>Delete</button>
        </menu>
    </form>
</dialog>

<script>
const dlg = document.getElementById('confirm-dialog');
document.getElementById('open-dialog').onclick = () => dlg.showModal();

dlg.addEventListener('close', () => {
    if (dlg.returnValue === 'confirm') {
        // user confirmed — perform delete
    }
});
</script>

<style>
dialog { border: 0; border-radius: 12px; padding: 24px; max-width: 420px; }
dialog::backdrop { background: rgba(0,0,0,0.5); backdrop-filter: blur(4px); }
dialog menu { display: flex; gap: 8px; justify-content: flex-end; padding: 0; margin-top: 16px; }
</style>
Tags

Save your own code snippets

Create a free account and build your private vault. Share publicly whenever you want.