Dialog
A modal with a header, content and two actions, plus a small one that asks before deleting, both opened from a project row. It is built on the dialog element, so focus stays inside and Escape closes it, and on a phone it becomes a bottom sheet.
Q4 launch plan12 files · Edited 2 days ago
Options
Code
import { useEffect, useId, useRef, useState } from "react";
import "./dialog.css";
/* A modal on the dialog element. Give it open and onClose; size="small" and
danger make the confirmation that asks before deleting.
<Dialog open={open} onClose={close} title="Rename project" confirm="Save" onConfirm={save}>…</Dialog> */
export function Dialog({
open,
onClose,
title,
text,
size,
danger = false,
cancel = "Cancel",
confirm = "Save",
onConfirm,
children,
}) {
const dialog = useRef(null);
const id = useId();
/* Opening is at once. Closing plays the way out first. */
useEffect(() => {
const element = dialog.current;
if (open) {
element.removeAttribute("data-closing");
if (!element.open) element.showModal();
return;
}
if (!element.open) return;
element.setAttribute("data-closing", "");
const timer = setTimeout(() => {
element.removeAttribute("data-closing");
element.close();
}, 200);
return () => clearTimeout(timer);
}, [open]);
const submit = (event) => {
event.preventDefault();
if (onConfirm) onConfirm(event);
onClose();
};
return (
<dialog
className="dh-dialog"
ref={dialog}
data-size={size}
role={danger ? "alertdialog" : undefined}
aria-labelledby={id + "-title"}
aria-describedby={text ? id + "-text" : undefined}
onCancel={(event) => {
event.preventDefault();
onClose();
}}
onClick={(event) => {
if (event.target === dialog.current) onClose();
}}
>
<form method="dialog" onSubmit={submit}>
<header>
<h2 id={id + "-title"}>{title}</h2>
{text && <p id={id + "-text"}>{text}</p>}
</header>
{children}
<footer>
<button className="dh-dialog-btn" type="button" onClick={onClose}>
{cancel}
</button>
<button className="dh-dialog-btn" type="submit" data-tone={danger ? "danger" : "primary"}>
{confirm}
</button>
</footer>
</form>
</dialog>
);
}
export function Example() {
const [shown, setShown] = useState(null);
const close = () => setShown(null);
return (
<div className="dh-dialog-demo">
<div className="dh-dialog-card">
<i aria-hidden="true" />
<p>
<b>Q4 launch plan</b>
<span>12 files · Edited 2 days ago</span>
</p>
<button className="dh-dialog-btn" type="button" onClick={() => setShown("edit")}>
Rename
</button>
<button className="dh-dialog-btn" type="button" data-tone="quiet-danger" onClick={() => setShown("delete")}>
Delete
</button>
</div>
<Dialog
open={shown === "edit"}
onClose={close}
title="Rename project"
text="The new name shows up for everyone on your team."
confirm="Save"
>
<label>
<span>Name</span>
<input type="text" defaultValue="Q4 launch plan" />
</label>
</Dialog>
<Dialog
open={shown === "delete"}
onClose={close}
size="small"
danger
title="Delete this project?"
text="Q4 launch plan and its 12 files will be gone for good. This cannot be undone."
confirm="Delete project"
/>
</div>
);
}/* A modal built on the dialog element, so the browser keeps focus inside it
and Escape closes it. A project row opens two: one with a field, and a small
one that asks before deleting. On a phone it becomes a sheet at the bottom
of the screen. Sizes are in em, 1em to the em at the usual 1.2308em. */
.dh-dialog-demo {
--accent: var(--dh-accent, #2563ff);
--line: color-mix(in srgb, var(--dh-fg, #171717) 9%, transparent);
--ring: 0 0 0 1px var(--line), 0 1px 2px color-mix(in srgb, var(--dh-fg, #171717) 10%, transparent);
width: 30.7692em;
max-width: 100%;
color: var(--dh-fg, #171717);
font-size: 0.8125em;
line-height: 1.5385em;
text-align: left;
}
.dh-dialog-card {
display: flex;
align-items: center;
gap: 0.6154em;
padding: 0.7692em 0.7692em 0.7692em 0.9231em;
border-radius: 0.9231em;
background: var(--dh-raised, #fff);
box-shadow: var(--ring);
}
/* The project's picture: a small accent tile that thins out into dots. */
.dh-dialog-card > i {
flex: none;
width: 2.4615em;
height: 2.4615em;
border-radius: 0.6154em;
background: linear-gradient(color-mix(in srgb, var(--accent) 82%, #000), var(--accent));
-webkit-mask-image: linear-gradient(#000 30%, transparent), radial-gradient(circle, #000 0.085em, transparent 0.108em);
mask-image: linear-gradient(#000 30%, transparent), radial-gradient(circle, #000 0.085em, transparent 0.108em);
-webkit-mask-size: auto, 0.308em 0.308em;
mask-size: auto, 0.308em 0.308em;
}
.dh-dialog-card > p {
display: grid;
flex: 1;
min-width: 0;
margin: 0 0 0 0.1538em;
}
.dh-dialog-card b {
overflow: hidden;
font-weight: 500;
line-height: 1.3846em;
letter-spacing: -0.01em;
text-overflow: ellipsis;
white-space: nowrap;
}
.dh-dialog-card span {
overflow: hidden;
color: var(--dh-muted, #5c5c5c);
font-size: 0.9231em;
line-height: 1.2308em;
text-overflow: ellipsis;
white-space: nowrap;
}
.dh-dialog-btn {
display: inline-flex;
flex: none;
align-items: center;
justify-content: center;
height: 2.4615em;
padding: 0 0.9231em;
border: 0;
border-radius: 0.6154em;
background: var(--dh-raised, #fff);
color: var(--dh-fg, #171717);
font: inherit;
font-weight: 500;
letter-spacing: -0.01em;
cursor: pointer;
box-shadow: var(--ring);
transition: transform 150ms cubic-bezier(0.23, 1, 0.32, 1);
}
.dh-dialog-btn[data-tone="quiet-danger"] {
color: #d92d20;
}
.dh-dialog-btn:is([data-tone="primary"], [data-tone="danger"]) {
--fill: var(--accent);
background: linear-gradient(color-mix(in srgb, var(--fill) 84%, #fff), var(--fill));
color: var(--dh-on-accent, #fff);
box-shadow: inset 0 1px 0 rgb(255 255 255 / 0.3), 0 0 0 1px color-mix(in srgb, var(--fill) 78%, #000), 0 1px 2px color-mix(in srgb, var(--dh-fg, #171717) 22%, transparent);
}
.dh-dialog-btn[data-tone="danger"] {
--fill: #d92d20;
color: #fff;
}
.dh-dialog-btn:active {
transform: scale(0.97);
}
.dh-dialog-demo :is(button, input):focus-visible {
outline: 2px solid var(--dh-faint, #8a8a8a);
outline-offset: 1px;
}
.dh-dialog {
overflow: hidden;
width: 29.2308em;
max-width: calc(100% - 2.4615em);
padding: 0;
border: 0;
border-radius: 1.0769em;
background: var(--dh-raised, #fff);
color: var(--dh-fg, #171717);
font-size: 1em;
line-height: 1.5385em;
text-align: left;
box-shadow: var(--ring), 0 14px 28px -14px rgb(0 0 0 / 0.35);
}
.dh-dialog[data-size="small"] {
width: 26.1538em;
}
.dh-dialog[open] {
animation: dh-dialog-in 240ms cubic-bezier(0.23, 1, 0.32, 1) both;
}
.dh-dialog::backdrop {
background: rgb(0 0 0 / 0.3);
-webkit-backdrop-filter: blur(2px);
backdrop-filter: blur(2px);
animation: dh-dialog-fade 240ms cubic-bezier(0.23, 1, 0.32, 1) both;
}
.dh-dialog[data-closing] {
animation: dh-dialog-out 200ms cubic-bezier(0.23, 1, 0.32, 1) both;
}
.dh-dialog[data-closing]::backdrop {
animation: dh-dialog-fade 200ms cubic-bezier(0.23, 1, 0.32, 1) reverse both;
}
.dh-dialog header {
padding: 1.0769em 1.2308em 1.0769em;
}
.dh-dialog h2 {
margin: 0;
font-size: 1.0769em;
font-weight: 500;
line-height: 1.5385em;
letter-spacing: -0.015em;
}
.dh-dialog p {
margin: 0.1538em 0 0;
color: var(--dh-muted, #5c5c5c);
}
.dh-dialog label {
display: grid;
gap: 0.3846em;
padding: 0 1.2308em 1.2308em;
}
.dh-dialog label span {
color: var(--dh-muted, #5c5c5c);
font-size: 0.8462em;
line-height: 1.2308em;
}
.dh-dialog input {
min-width: 0;
height: 2.4615em;
padding: 0 0.7692em;
border: 0;
border-radius: 0.5385em;
background: var(--dh-well, #ededeb);
color: inherit;
font: inherit;
outline: none;
transition: box-shadow 150ms cubic-bezier(0.23, 1, 0.32, 1);
}
.dh-dialog-demo .dh-dialog input:focus-visible {
outline: none;
box-shadow: 0 0 0 2px var(--dh-faint, #8a8a8a);
}
.dh-dialog footer {
display: flex;
justify-content: space-between;
gap: 0.6154em;
padding: 0.7692em 0.9231em;
border-top: 1px solid var(--line);
background: color-mix(in srgb, var(--dh-fg, #171717) 3%, var(--dh-raised, #fff));
}
/* A sheet on a phone: it rises from the bottom, and the confirm goes on top. */
@media (max-width: 480px) {
.dh-dialog,
.dh-dialog[data-size="small"] {
width: 100%;
max-width: none;
margin-bottom: 0;
border-radius: 1.0769em 1.0769em 0 0;
}
.dh-dialog[open] {
animation-name: dh-dialog-up;
animation-duration: 320ms;
}
.dh-dialog[data-closing] {
animation-name: dh-dialog-down;
}
.dh-dialog footer {
flex-direction: column-reverse;
padding: 0.9231em 1.2308em max(0.9231em, env(safe-area-inset-bottom));
}
.dh-dialog footer .dh-dialog-btn,
.dh-dialog input {
height: 3.3846em;
}
.dh-dialog input {
font-size: 1rem;
}
}
@keyframes dh-dialog-in {
from {
opacity: 0;
transform: translateY(0.9231em) scale(0.98);
filter: blur(4px);
}
}
@keyframes dh-dialog-out {
to {
opacity: 0;
transform: translateY(0.4615em) scale(0.98);
filter: blur(4px);
}
}
@keyframes dh-dialog-up {
from {
transform: translateY(100%);
}
}
@keyframes dh-dialog-down {
to {
transform: translateY(100%);
}
}
@keyframes dh-dialog-fade {
from {
opacity: 0;
}
}
@media (prefers-reduced-motion: reduce) {
.dh-dialog[open] {
animation: dh-dialog-fade 200ms both;
}
.dh-dialog[data-closing] {
animation: dh-dialog-fade 150ms reverse both;
}
.dh-dialog-btn,
.dh-dialog input {
transition: none;
}
}<div class="dh-dialog-demo">
<div class="dh-dialog-card">
<i aria-hidden="true"></i>
<p><b>Q4 launch plan</b><span>12 files · Edited 2 days ago</span></p>
<button class="dh-dialog-btn" type="button" data-opens="dh-dialog-edit">Rename</button>
<button class="dh-dialog-btn" type="button" data-tone="quiet-danger" data-opens="dh-dialog-delete">Delete</button>
</div>
<dialog class="dh-dialog" id="dh-dialog-edit" aria-labelledby="dh-dialog-edit-title">
<form method="dialog">
<header>
<h2 id="dh-dialog-edit-title">Rename project</h2>
<p>The new name shows up for everyone on your team.</p>
</header>
<label>
<span>Name</span>
<input type="text" value="Q4 launch plan" autofocus />
</label>
<footer>
<button class="dh-dialog-btn" type="button" data-close>Cancel</button>
<button class="dh-dialog-btn" type="submit" data-tone="primary">Save</button>
</footer>
</form>
</dialog>
<dialog class="dh-dialog" id="dh-dialog-delete" data-size="small" role="alertdialog" aria-labelledby="dh-dialog-delete-title" aria-describedby="dh-dialog-delete-text">
<form method="dialog">
<header>
<h2 id="dh-dialog-delete-title">Delete this project?</h2>
<p id="dh-dialog-delete-text">Q4 launch plan and its 12 files will be gone for good. This cannot be undone.</p>
</header>
<footer>
<button class="dh-dialog-btn" type="button" data-close autofocus>Cancel</button>
<button class="dh-dialog-btn" type="submit" data-tone="danger">Delete project</button>
</footer>
</form>
</dialog>
</div>/* A modal built on the dialog element, so the browser keeps focus inside it
and Escape closes it. A project row opens two: one with a field, and a small
one that asks before deleting. On a phone it becomes a sheet at the bottom
of the screen. Sizes are in em, 1em to the em at the usual 1.2308em. */
.dh-dialog-demo {
--accent: var(--dh-accent, #2563ff);
--line: color-mix(in srgb, var(--dh-fg, #171717) 9%, transparent);
--ring: 0 0 0 1px var(--line), 0 1px 2px color-mix(in srgb, var(--dh-fg, #171717) 10%, transparent);
width: 30.7692em;
max-width: 100%;
color: var(--dh-fg, #171717);
font-size: 0.8125em;
line-height: 1.5385em;
text-align: left;
}
.dh-dialog-card {
display: flex;
align-items: center;
gap: 0.6154em;
padding: 0.7692em 0.7692em 0.7692em 0.9231em;
border-radius: 0.9231em;
background: var(--dh-raised, #fff);
box-shadow: var(--ring);
}
/* The project's picture: a small accent tile that thins out into dots. */
.dh-dialog-card > i {
flex: none;
width: 2.4615em;
height: 2.4615em;
border-radius: 0.6154em;
background: linear-gradient(color-mix(in srgb, var(--accent) 82%, #000), var(--accent));
-webkit-mask-image: linear-gradient(#000 30%, transparent), radial-gradient(circle, #000 0.085em, transparent 0.108em);
mask-image: linear-gradient(#000 30%, transparent), radial-gradient(circle, #000 0.085em, transparent 0.108em);
-webkit-mask-size: auto, 0.308em 0.308em;
mask-size: auto, 0.308em 0.308em;
}
.dh-dialog-card > p {
display: grid;
flex: 1;
min-width: 0;
margin: 0 0 0 0.1538em;
}
.dh-dialog-card b {
overflow: hidden;
font-weight: 500;
line-height: 1.3846em;
letter-spacing: -0.01em;
text-overflow: ellipsis;
white-space: nowrap;
}
.dh-dialog-card span {
overflow: hidden;
color: var(--dh-muted, #5c5c5c);
font-size: 0.9231em;
line-height: 1.2308em;
text-overflow: ellipsis;
white-space: nowrap;
}
.dh-dialog-btn {
display: inline-flex;
flex: none;
align-items: center;
justify-content: center;
height: 2.4615em;
padding: 0 0.9231em;
border: 0;
border-radius: 0.6154em;
background: var(--dh-raised, #fff);
color: var(--dh-fg, #171717);
font: inherit;
font-weight: 500;
letter-spacing: -0.01em;
cursor: pointer;
box-shadow: var(--ring);
transition: transform 150ms cubic-bezier(0.23, 1, 0.32, 1);
}
.dh-dialog-btn[data-tone="quiet-danger"] {
color: #d92d20;
}
.dh-dialog-btn:is([data-tone="primary"], [data-tone="danger"]) {
--fill: var(--accent);
background: linear-gradient(color-mix(in srgb, var(--fill) 84%, #fff), var(--fill));
color: var(--dh-on-accent, #fff);
box-shadow: inset 0 1px 0 rgb(255 255 255 / 0.3), 0 0 0 1px color-mix(in srgb, var(--fill) 78%, #000), 0 1px 2px color-mix(in srgb, var(--dh-fg, #171717) 22%, transparent);
}
.dh-dialog-btn[data-tone="danger"] {
--fill: #d92d20;
color: #fff;
}
.dh-dialog-btn:active {
transform: scale(0.97);
}
.dh-dialog-demo :is(button, input):focus-visible {
outline: 2px solid var(--dh-faint, #8a8a8a);
outline-offset: 1px;
}
.dh-dialog {
overflow: hidden;
width: 29.2308em;
max-width: calc(100% - 2.4615em);
padding: 0;
border: 0;
border-radius: 1.0769em;
background: var(--dh-raised, #fff);
color: var(--dh-fg, #171717);
font-size: 1em;
line-height: 1.5385em;
text-align: left;
box-shadow: var(--ring), 0 14px 28px -14px rgb(0 0 0 / 0.35);
}
.dh-dialog[data-size="small"] {
width: 26.1538em;
}
.dh-dialog[open] {
animation: dh-dialog-in 240ms cubic-bezier(0.23, 1, 0.32, 1) both;
}
.dh-dialog::backdrop {
background: rgb(0 0 0 / 0.3);
-webkit-backdrop-filter: blur(2px);
backdrop-filter: blur(2px);
animation: dh-dialog-fade 240ms cubic-bezier(0.23, 1, 0.32, 1) both;
}
.dh-dialog[data-closing] {
animation: dh-dialog-out 200ms cubic-bezier(0.23, 1, 0.32, 1) both;
}
.dh-dialog[data-closing]::backdrop {
animation: dh-dialog-fade 200ms cubic-bezier(0.23, 1, 0.32, 1) reverse both;
}
.dh-dialog header {
padding: 1.0769em 1.2308em 1.0769em;
}
.dh-dialog h2 {
margin: 0;
font-size: 1.0769em;
font-weight: 500;
line-height: 1.5385em;
letter-spacing: -0.015em;
}
.dh-dialog p {
margin: 0.1538em 0 0;
color: var(--dh-muted, #5c5c5c);
}
.dh-dialog label {
display: grid;
gap: 0.3846em;
padding: 0 1.2308em 1.2308em;
}
.dh-dialog label span {
color: var(--dh-muted, #5c5c5c);
font-size: 0.8462em;
line-height: 1.2308em;
}
.dh-dialog input {
min-width: 0;
height: 2.4615em;
padding: 0 0.7692em;
border: 0;
border-radius: 0.5385em;
background: var(--dh-well, #ededeb);
color: inherit;
font: inherit;
outline: none;
transition: box-shadow 150ms cubic-bezier(0.23, 1, 0.32, 1);
}
.dh-dialog-demo .dh-dialog input:focus-visible {
outline: none;
box-shadow: 0 0 0 2px var(--dh-faint, #8a8a8a);
}
.dh-dialog footer {
display: flex;
justify-content: space-between;
gap: 0.6154em;
padding: 0.7692em 0.9231em;
border-top: 1px solid var(--line);
background: color-mix(in srgb, var(--dh-fg, #171717) 3%, var(--dh-raised, #fff));
}
/* A sheet on a phone: it rises from the bottom, and the confirm goes on top. */
@media (max-width: 480px) {
.dh-dialog,
.dh-dialog[data-size="small"] {
width: 100%;
max-width: none;
margin-bottom: 0;
border-radius: 1.0769em 1.0769em 0 0;
}
.dh-dialog[open] {
animation-name: dh-dialog-up;
animation-duration: 320ms;
}
.dh-dialog[data-closing] {
animation-name: dh-dialog-down;
}
.dh-dialog footer {
flex-direction: column-reverse;
padding: 0.9231em 1.2308em max(0.9231em, env(safe-area-inset-bottom));
}
.dh-dialog footer .dh-dialog-btn,
.dh-dialog input {
height: 3.3846em;
}
.dh-dialog input {
font-size: 1rem;
}
}
@keyframes dh-dialog-in {
from {
opacity: 0;
transform: translateY(0.9231em) scale(0.98);
filter: blur(4px);
}
}
@keyframes dh-dialog-out {
to {
opacity: 0;
transform: translateY(0.4615em) scale(0.98);
filter: blur(4px);
}
}
@keyframes dh-dialog-up {
from {
transform: translateY(100%);
}
}
@keyframes dh-dialog-down {
to {
transform: translateY(100%);
}
}
@keyframes dh-dialog-fade {
from {
opacity: 0;
}
}
@media (prefers-reduced-motion: reduce) {
.dh-dialog[open] {
animation: dh-dialog-fade 200ms both;
}
.dh-dialog[data-closing] {
animation: dh-dialog-fade 150ms reverse both;
}
.dh-dialog-btn,
.dh-dialog input {
transition: none;
}
}/* Opens each dialog from its button, and lets it play its way out before it
closes. The browser handles the focus trap and gives focus back. */
document.querySelectorAll(".dh-dialog-demo").forEach((demo) => {
const shut = (dialog) => {
if (dialog.hasAttribute("data-closing")) return;
dialog.setAttribute("data-closing", "");
setTimeout(() => {
dialog.removeAttribute("data-closing");
dialog.close();
}, 200);
};
demo.querySelectorAll("[data-opens]").forEach((button) => {
button.addEventListener("click", () => demo.querySelector("#" + button.dataset.opens).showModal());
});
demo.querySelectorAll(".dh-dialog").forEach((dialog) => {
/* Escape. */
dialog.addEventListener("cancel", (event) => {
event.preventDefault();
shut(dialog);
});
/* A click on the dimmed page behind it. */
dialog.addEventListener("click", (event) => {
if (event.target === dialog) shut(dialog);
});
dialog.querySelectorAll("[data-close]").forEach((button) => {
button.addEventListener("click", () => shut(dialog));
});
/* Save or Delete: do the work here, then close. */
dialog.querySelector("form").addEventListener("submit", (event) => {
event.preventDefault();
shut(dialog);
});
});
});Dialog is not in the Figma file yet.
Use the React or HTML code for now.