// This is the same as before, setting up variables.
let button = document.querySelector('#example')
let modal = document.querySelector('#dialog') // Now one for our `dialog`.
let closeButton = modal.querySelector('.close') // Only looking within `modal`.
button.onclick = () => { // “Listen” for clicks.
modal.showModal() // This opens it up.
}
closeButton.onclick = () => {
modal.close() // And this closes it!
}
modal.onclick = (event) => { // Listen on our `modal` also…
if (event.target == modal) { // Only if clicks are to itself (the background).
modal.close() // Close it then too.
}
}
dialog {
color: white;
gap: calc(4 * var(--base));
inset: 0; /* Fill the viewport! */
place-content: center; /* Center the tracks. */
place-items: center; /* Center within the tracks. */
&[open] { display: grid } /* `block` is default, but we can override. */
}
.close { background-color: tomato; } /* Always have a clear “close” `button`. */
::backdrop {
background-color: rgb(0 0 0 / 75%); /* A dark overlay. */
backdrop-filter: blur(8px); /* Maybe a little “glassmorphism”? */
}