Toast
A short message that rises in at the bottom and leaves by itself, for success, an error or plain news, with room for one action such as Undo.
Workspace
Draft
Changes saved
Options
Code
import { useEffect, useState } from "react";
import "./toast.css";
/* One toast. It calls onDone after four seconds, unless the pointer or the
keyboard is on it.
<Toast kind="bad" action="Try again" onAction={retry} onDone={clear}>Could not save</Toast> */
export function Toast({ kind = "info", action, onAction, onDone, children }) {
const [held, setHeld] = useState(false);
useEffect(() => {
if (held || !onDone) return;
const timer = setTimeout(onDone, 4000);
return () => clearTimeout(timer);
}, [held, onDone]);
return (
<div
className="dh-toast"
data-kind={kind}
role={kind === "bad" ? "alert" : "status"}
onPointerEnter={() => setHeld(true)}
onPointerLeave={() => setHeld(false)}
onFocus={() => setHeld(true)}
onBlur={() => setHeld(false)}
>
<span>{children}</span>
{action && (
<button type="button" onClick={onAction}>
{action}
</button>
)}
</div>
);
}
const samples = [
{ label: "Invite", kind: "info", text: "Invite sent to Maya Okafor", action: "Undo" },
{ label: "Publish", kind: "bad", text: "Could not publish", action: "Try again" },
{ label: "Save", kind: "good", text: "Changes saved", tone: "primary" },
];
export function Example() {
const [toast, setToast] = useState({ id: 0, kind: "good", text: "Changes saved", stay: true });
const clear = () => setToast(null);
return (
<div className="dh-toast-demo">
<div className="dh-toast-card">
<header>
<h3>Workspace</h3>
<em>Draft</em>
</header>
<label>
<span>Name</span>
<input type="text" defaultValue="Logoipsum studio" />
</label>
<footer>
{samples.map((sample) => (
<button
className="dh-toast-btn"
type="button"
data-tone={sample.tone}
key={sample.label}
onClick={() => setToast({ ...sample, id: Date.now() })}
>
{sample.label}
</button>
))}
</footer>
</div>
<div className="dh-toast-area" aria-live="polite">
{toast && (
<Toast
key={toast.id}
kind={toast.kind}
action={toast.action}
onAction={clear}
onDone={toast.stay ? undefined : clear}
>
{toast.text}
</Toast>
)}
</div>
</div>
);
}/* A toast: a short message that rises in under what you were doing and leaves
by itself. Here a small settings card fires one from each of its buttons.
Sizes are in em, 1em to the em at the usual 1.2308em. */
.dh-toast-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);
position: relative;
width: 23.0769em;
max-width: 100%;
padding-bottom: 4em;
color: var(--dh-fg, #171717);
font-size: 0.8125em;
line-height: 1.5385em;
text-align: left;
}
.dh-toast-card {
overflow: hidden;
border-radius: 0.9231em;
background: var(--dh-raised, #fff);
box-shadow: var(--ring);
}
.dh-toast-card header {
display: flex;
align-items: center;
gap: 0.6154em;
padding: 0.9231em 1.2308em 0.7692em;
}
.dh-toast-card h3 {
margin: 0;
font-size: 1.0769em;
font-weight: 500;
line-height: 1.5385em;
letter-spacing: -0.015em;
}
.dh-toast-card em {
padding: 0.0769em 0.3846em;
border-radius: 0.3077em;
background: color-mix(in srgb, var(--accent) 12%, transparent);
color: color-mix(in srgb, var(--accent) 80%, var(--dh-fg, #171717));
font-size: 0.8462em;
font-style: normal;
font-weight: 500;
line-height: 1.2308em;
}
.dh-toast-card label {
display: grid;
gap: 0.3846em;
padding: 0 1.2308em 1.2308em;
}
.dh-toast-card label span {
color: var(--dh-muted, #5c5c5c);
font-size: 0.8462em;
line-height: 1.2308em;
}
.dh-toast-card 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-toast-card input:focus-visible {
box-shadow: 0 0 0 2px var(--dh-faint, #8a8a8a);
}
.dh-toast-card footer {
display: flex;
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));
}
.dh-toast-btn {
height: 2.1538em;
padding: 0 0.7692em;
border: 0;
border-radius: 0.5385em;
background: var(--dh-raised, #fff);
color: inherit;
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-toast-btn[data-tone="primary"] {
margin-left: auto;
background: linear-gradient(color-mix(in srgb, var(--accent) 84%, #fff), var(--accent));
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(--accent) 78%, #000), 0 1px 2px color-mix(in srgb, var(--dh-fg, #171717) 22%, transparent);
}
.dh-toast-btn:active,
.dh-toast button:active {
transform: scale(0.97);
}
.dh-toast-demo button:focus-visible {
outline: 2px solid var(--dh-faint, #8a8a8a);
outline-offset: 1px;
}
/* For a real page, make the area position: fixed at the bottom of the window. */
.dh-toast-area {
position: absolute;
inset: auto 0 0;
display: grid;
justify-items: center;
pointer-events: none;
}
.dh-toast {
--hue: var(--accent);
grid-area: 1 / 1;
display: flex;
align-items: center;
gap: 0.6923em;
height: 2.4615em;
padding: 0 0.9231em 0 1em;
border-radius: 0.7692em;
background: var(--dh-fg, #171717);
color: var(--dh-bg, #f7f7f6);
font-weight: 500;
letter-spacing: -0.01em;
white-space: nowrap;
pointer-events: auto;
box-shadow: 0 1px 2px rgb(0 0 0 / 0.2), 0 12px 24px -12px rgb(0 0 0 / 0.45);
animation: dh-toast-in 280ms cubic-bezier(0.23, 1, 0.32, 1) both;
}
.dh-toast::before {
content: "";
flex: none;
width: 0.4615em;
height: 0.4615em;
border-radius: 50%;
background: var(--hue);
box-shadow: 0 0 0 3px color-mix(in srgb, var(--hue) 32%, transparent);
}
.dh-toast[data-kind="good"] {
--hue: #1fa750;
}
.dh-toast[data-kind="bad"] {
--hue: #d92d20;
}
.dh-toast:has(button) {
padding-right: 0.3077em;
}
.dh-toast button {
height: 1.8462em;
margin-left: 0.1538em;
padding: 0 0.6154em;
border: 0;
border-radius: 0.5385em;
background: color-mix(in srgb, var(--dh-bg, #f7f7f6) 16%, transparent);
color: inherit;
font: inherit;
cursor: pointer;
transition: transform 150ms cubic-bezier(0.23, 1, 0.32, 1), background-color 150ms cubic-bezier(0.23, 1, 0.32, 1);
}
.dh-toast[data-leaving] {
animation: dh-toast-out 150ms cubic-bezier(0.23, 1, 0.32, 1) both;
}
@media (hover: hover) and (pointer: fine) {
.dh-toast button:hover {
background: color-mix(in srgb, var(--dh-bg, #f7f7f6) 26%, transparent);
}
}
@keyframes dh-toast-in {
from {
opacity: 0;
filter: blur(4px);
transform: translateY(0.6154em) scale(0.97);
}
}
@keyframes dh-toast-out {
to {
opacity: 0;
filter: blur(4px);
transform: translateY(0.3077em) scale(0.97);
}
}
@media (prefers-reduced-motion: reduce) {
.dh-toast,
.dh-toast[data-leaving] {
animation: dh-toast-fade 150ms both;
}
.dh-toast[data-leaving] {
animation-direction: reverse;
}
.dh-toast-btn,
.dh-toast button {
transition: none;
}
}
@keyframes dh-toast-fade {
from {
opacity: 0;
}
}<div class="dh-toast-demo">
<div class="dh-toast-card">
<header>
<h3>Workspace</h3>
<em>Draft</em>
</header>
<label>
<span>Name</span>
<input type="text" value="Logoipsum studio" />
</label>
<footer>
<button class="dh-toast-btn" type="button" data-kind="info" data-text="Invite sent to Maya Okafor" data-action="Undo">Invite</button>
<button class="dh-toast-btn" type="button" data-kind="bad" data-text="Could not publish" data-action="Try again">Publish</button>
<button class="dh-toast-btn" type="button" data-tone="primary" data-kind="good" data-text="Changes saved">Save</button>
</footer>
</div>
<div class="dh-toast-area" aria-live="polite">
<div class="dh-toast" data-kind="good" role="status">
<span>Changes saved</span>
</div>
</div>
</div>/* A toast: a short message that rises in under what you were doing and leaves
by itself. Here a small settings card fires one from each of its buttons.
Sizes are in em, 1em to the em at the usual 1.2308em. */
.dh-toast-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);
position: relative;
width: 23.0769em;
max-width: 100%;
padding-bottom: 4em;
color: var(--dh-fg, #171717);
font-size: 0.8125em;
line-height: 1.5385em;
text-align: left;
}
.dh-toast-card {
overflow: hidden;
border-radius: 0.9231em;
background: var(--dh-raised, #fff);
box-shadow: var(--ring);
}
.dh-toast-card header {
display: flex;
align-items: center;
gap: 0.6154em;
padding: 0.9231em 1.2308em 0.7692em;
}
.dh-toast-card h3 {
margin: 0;
font-size: 1.0769em;
font-weight: 500;
line-height: 1.5385em;
letter-spacing: -0.015em;
}
.dh-toast-card em {
padding: 0.0769em 0.3846em;
border-radius: 0.3077em;
background: color-mix(in srgb, var(--accent) 12%, transparent);
color: color-mix(in srgb, var(--accent) 80%, var(--dh-fg, #171717));
font-size: 0.8462em;
font-style: normal;
font-weight: 500;
line-height: 1.2308em;
}
.dh-toast-card label {
display: grid;
gap: 0.3846em;
padding: 0 1.2308em 1.2308em;
}
.dh-toast-card label span {
color: var(--dh-muted, #5c5c5c);
font-size: 0.8462em;
line-height: 1.2308em;
}
.dh-toast-card 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-toast-card input:focus-visible {
box-shadow: 0 0 0 2px var(--dh-faint, #8a8a8a);
}
.dh-toast-card footer {
display: flex;
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));
}
.dh-toast-btn {
height: 2.1538em;
padding: 0 0.7692em;
border: 0;
border-radius: 0.5385em;
background: var(--dh-raised, #fff);
color: inherit;
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-toast-btn[data-tone="primary"] {
margin-left: auto;
background: linear-gradient(color-mix(in srgb, var(--accent) 84%, #fff), var(--accent));
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(--accent) 78%, #000), 0 1px 2px color-mix(in srgb, var(--dh-fg, #171717) 22%, transparent);
}
.dh-toast-btn:active,
.dh-toast button:active {
transform: scale(0.97);
}
.dh-toast-demo button:focus-visible {
outline: 2px solid var(--dh-faint, #8a8a8a);
outline-offset: 1px;
}
/* For a real page, make the area position: fixed at the bottom of the window. */
.dh-toast-area {
position: absolute;
inset: auto 0 0;
display: grid;
justify-items: center;
pointer-events: none;
}
.dh-toast {
--hue: var(--accent);
grid-area: 1 / 1;
display: flex;
align-items: center;
gap: 0.6923em;
height: 2.4615em;
padding: 0 0.9231em 0 1em;
border-radius: 0.7692em;
background: var(--dh-fg, #171717);
color: var(--dh-bg, #f7f7f6);
font-weight: 500;
letter-spacing: -0.01em;
white-space: nowrap;
pointer-events: auto;
box-shadow: 0 1px 2px rgb(0 0 0 / 0.2), 0 12px 24px -12px rgb(0 0 0 / 0.45);
animation: dh-toast-in 280ms cubic-bezier(0.23, 1, 0.32, 1) both;
}
.dh-toast::before {
content: "";
flex: none;
width: 0.4615em;
height: 0.4615em;
border-radius: 50%;
background: var(--hue);
box-shadow: 0 0 0 3px color-mix(in srgb, var(--hue) 32%, transparent);
}
.dh-toast[data-kind="good"] {
--hue: #1fa750;
}
.dh-toast[data-kind="bad"] {
--hue: #d92d20;
}
.dh-toast:has(button) {
padding-right: 0.3077em;
}
.dh-toast button {
height: 1.8462em;
margin-left: 0.1538em;
padding: 0 0.6154em;
border: 0;
border-radius: 0.5385em;
background: color-mix(in srgb, var(--dh-bg, #f7f7f6) 16%, transparent);
color: inherit;
font: inherit;
cursor: pointer;
transition: transform 150ms cubic-bezier(0.23, 1, 0.32, 1), background-color 150ms cubic-bezier(0.23, 1, 0.32, 1);
}
.dh-toast[data-leaving] {
animation: dh-toast-out 150ms cubic-bezier(0.23, 1, 0.32, 1) both;
}
@media (hover: hover) and (pointer: fine) {
.dh-toast button:hover {
background: color-mix(in srgb, var(--dh-bg, #f7f7f6) 26%, transparent);
}
}
@keyframes dh-toast-in {
from {
opacity: 0;
filter: blur(4px);
transform: translateY(0.6154em) scale(0.97);
}
}
@keyframes dh-toast-out {
to {
opacity: 0;
filter: blur(4px);
transform: translateY(0.3077em) scale(0.97);
}
}
@media (prefers-reduced-motion: reduce) {
.dh-toast,
.dh-toast[data-leaving] {
animation: dh-toast-fade 150ms both;
}
.dh-toast[data-leaving] {
animation-direction: reverse;
}
.dh-toast-btn,
.dh-toast button {
transition: none;
}
}
@keyframes dh-toast-fade {
from {
opacity: 0;
}
}/* Each button shows one toast. A new toast replaces the one on screen, and a
toast leaves after four seconds unless the pointer or keyboard is on it. */
document.querySelectorAll(".dh-toast-demo").forEach((demo) => {
const area = demo.querySelector(".dh-toast-area");
let timer;
const leave = (toast) => {
if (!toast || toast.hasAttribute("data-leaving")) return;
toast.setAttribute("data-leaving", "");
setTimeout(() => toast.remove(), 150);
};
const wait = (toast) => {
clearTimeout(timer);
timer = setTimeout(() => leave(toast), 4000);
};
const show = ({ kind, text, action }) => {
area.querySelectorAll(".dh-toast").forEach(leave);
const toast = document.createElement("div");
toast.className = "dh-toast";
toast.dataset.kind = kind;
toast.setAttribute("role", kind === "bad" ? "alert" : "status");
const words = document.createElement("span");
words.textContent = text;
toast.append(words);
if (action) {
const button = document.createElement("button");
button.type = "button";
button.textContent = action;
button.addEventListener("click", () => leave(toast));
toast.append(button);
}
toast.addEventListener("pointerenter", () => clearTimeout(timer));
toast.addEventListener("pointerleave", () => wait(toast));
toast.addEventListener("focusin", () => clearTimeout(timer));
toast.addEventListener("focusout", () => wait(toast));
area.append(toast);
wait(toast);
};
demo.querySelectorAll(".dh-toast-btn").forEach((button) => {
button.addEventListener("click", () => show(button.dataset));
});
});Toast is not in the Figma file yet.
Use the React or HTML code for now.