Alert
A banner that sits in the page with a title, a line of text and room for one action, marked and faintly tinted for success, a warning, an error or plain news. The cross dismisses it.
Payment received
Invoice 1042 is paid. Your receipt is on its way.
Your trial ends in 3 days
Add a card
Add a card to keep your projects.
We could not reach your domain
Try again
Check the DNS records for example.com.
A new version is ready
Reload
Reload the page to get it.
Options
Code
import { useState } from "react";
import "./alert.css";
/* An inline banner. kind is "good", "warn", "bad" or "info". Give it an action
to show a small button, and onDismiss to show the cross.
<Alert kind="warn" title="Your trial ends in 3 days" action="Add a card" href="/billing" onDismiss={hide}>Add a card to keep your projects.</Alert> */
export function Alert({ kind = "info", title, action, href = "#", onDismiss, children }) {
return (
<div className="dh-alert" data-kind={kind} role={kind === "bad" ? "alert" : "status"}>
<div>
<strong>{title}</strong>
<p>{children}</p>
</div>
{action && <a href={href}>{action}</a>}
{onDismiss && (
<button type="button" aria-label="Dismiss" onClick={onDismiss}>
<svg viewBox="0 0 12 12" aria-hidden="true">
<path d="M3 3l6 6M9 3l-6 6" />
</svg>
</button>
)}
</div>
);
}
export function Example() {
const [hidden, setHidden] = useState([]);
const hide = (name) => () => setHidden((list) => [...list, name]);
const shown = (name) => !hidden.includes(name);
return (
<div className="dh-alert-stack">
{shown("paid") && (
<Alert kind="good" title="Payment received" onDismiss={hide("paid")}>
Invoice 1042 is paid. Your receipt is on its way.
</Alert>
)}
{shown("trial") && (
<Alert kind="warn" title="Your trial ends in 3 days" action="Add a card" onDismiss={hide("trial")}>
Add a card to keep your projects.
</Alert>
)}
<Alert kind="bad" title="We could not reach your domain" action="Try again">
Check the DNS records for example.com.
</Alert>
{shown("version") && (
<Alert kind="info" title="A new version is ready" action="Reload" onDismiss={hide("version")}>
Reload the page to get it.
</Alert>
)}
</div>
);
}/* An alert: a banner that sits in the page, with a title, a line of text, room
for one action and a cross to dismiss it. The bar and the faint tint take
the colour of its kind: good, warn, bad or info (the accent). */
.dh-alert-stack {
display: grid;
gap: 8px;
width: 440px;
max-width: 100%;
font-size: 13px;
line-height: 20px;
text-align: left;
}
.dh-alert {
--hue: var(--dh-accent, #2563ff);
--line: color-mix(in srgb, var(--dh-fg, #171717) 9%, transparent);
position: relative;
display: flex;
align-items: center;
gap: 8px;
padding: 10px 10px 10px 22px;
border-radius: 10px;
background: color-mix(in srgb, var(--hue) 5%, var(--dh-raised, #fff));
color: var(--dh-fg, #171717);
box-shadow:
0 0 0 1px color-mix(in srgb, var(--hue) 16%, var(--line)),
0 1px 2px color-mix(in srgb, var(--dh-fg, #171717) 10%, transparent);
animation: dh-alert-in 280ms cubic-bezier(0.23, 1, 0.32, 1) both;
}
.dh-alert::before {
content: "";
position: absolute;
inset: 10px auto 10px 10px;
width: 3px;
border-radius: 2px;
background: var(--hue);
}
.dh-alert[data-kind="good"] {
--hue: #1fa750;
}
.dh-alert[data-kind="warn"] {
--hue: #e39b0b;
}
.dh-alert[data-kind="bad"] {
--hue: #d92d20;
}
.dh-alert > div {
flex: 1;
min-width: 0;
}
.dh-alert strong {
display: block;
font-weight: 500;
letter-spacing: -0.01em;
}
.dh-alert p {
margin: 0;
color: var(--dh-muted, #5c5c5c);
}
.dh-alert > a {
display: grid;
flex: none;
place-items: center;
height: 28px;
padding: 0 10px;
border-radius: 7px;
background: var(--dh-raised, #fff);
color: inherit;
font-weight: 500;
letter-spacing: -0.01em;
text-decoration: none;
box-shadow:
0 0 0 1px var(--line),
0 1px 2px color-mix(in srgb, var(--dh-fg, #171717) 10%, transparent);
transition: transform 150ms cubic-bezier(0.23, 1, 0.32, 1);
}
.dh-alert > button {
display: grid;
flex: none;
place-items: center;
width: 24px;
height: 24px;
padding: 0;
border: 0;
border-radius: 7px;
background: none;
color: var(--dh-faint, #8a8a8a);
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-alert svg {
width: 12px;
height: 12px;
fill: none;
stroke: currentColor;
stroke-width: 1.4;
stroke-linecap: round;
}
.dh-alert > a:active,
.dh-alert > button:active {
transform: scale(0.96);
}
.dh-alert :is(a, button):focus-visible {
outline: 2px solid var(--dh-faint, #8a8a8a);
outline-offset: 1px;
}
.dh-alert[data-leaving] {
animation: dh-alert-out 150ms cubic-bezier(0.23, 1, 0.32, 1) both;
}
@media (hover: hover) and (pointer: fine) {
.dh-alert > button:hover {
background: color-mix(in srgb, var(--dh-fg, #171717) 6%, transparent);
color: var(--dh-fg, #171717);
}
}
@keyframes dh-alert-in {
from {
opacity: 0;
filter: blur(4px);
transform: translateY(-4px);
}
}
@keyframes dh-alert-out {
to {
opacity: 0;
filter: blur(4px);
transform: scale(0.98);
}
}
@media (prefers-reduced-motion: reduce) {
.dh-alert,
.dh-alert[data-leaving] {
animation: none;
}
.dh-alert > a,
.dh-alert > button {
transition: none;
}
}<div class="dh-alert-stack">
<div class="dh-alert" data-kind="good" role="status">
<div>
<strong>Payment received</strong>
<p>Invoice 1042 is paid. Your receipt is on its way.</p>
</div>
<button type="button" aria-label="Dismiss">
<svg viewBox="0 0 12 12" aria-hidden="true"><path d="M3 3l6 6M9 3l-6 6" /></svg>
</button>
</div>
<div class="dh-alert" data-kind="warn" role="status">
<div>
<strong>Your trial ends in 3 days</strong>
<p>Add a card to keep your projects.</p>
</div>
<a href="#">Add a card</a>
<button type="button" aria-label="Dismiss">
<svg viewBox="0 0 12 12" aria-hidden="true"><path d="M3 3l6 6M9 3l-6 6" /></svg>
</button>
</div>
<div class="dh-alert" data-kind="bad" role="alert">
<div>
<strong>We could not reach your domain</strong>
<p>Check the DNS records for example.com.</p>
</div>
<a href="#">Try again</a>
</div>
<div class="dh-alert" data-kind="info" role="status">
<div>
<strong>A new version is ready</strong>
<p>Reload the page to get it.</p>
</div>
<a href="#">Reload</a>
<button type="button" aria-label="Dismiss">
<svg viewBox="0 0 12 12" aria-hidden="true"><path d="M3 3l6 6M9 3l-6 6" /></svg>
</button>
</div>
</div>/* An alert: a banner that sits in the page, with a title, a line of text, room
for one action and a cross to dismiss it. The bar and the faint tint take
the colour of its kind: good, warn, bad or info (the accent). */
.dh-alert-stack {
display: grid;
gap: 8px;
width: 440px;
max-width: 100%;
font-size: 13px;
line-height: 20px;
text-align: left;
}
.dh-alert {
--hue: var(--dh-accent, #2563ff);
--line: color-mix(in srgb, var(--dh-fg, #171717) 9%, transparent);
position: relative;
display: flex;
align-items: center;
gap: 8px;
padding: 10px 10px 10px 22px;
border-radius: 10px;
background: color-mix(in srgb, var(--hue) 5%, var(--dh-raised, #fff));
color: var(--dh-fg, #171717);
box-shadow:
0 0 0 1px color-mix(in srgb, var(--hue) 16%, var(--line)),
0 1px 2px color-mix(in srgb, var(--dh-fg, #171717) 10%, transparent);
animation: dh-alert-in 280ms cubic-bezier(0.23, 1, 0.32, 1) both;
}
.dh-alert::before {
content: "";
position: absolute;
inset: 10px auto 10px 10px;
width: 3px;
border-radius: 2px;
background: var(--hue);
}
.dh-alert[data-kind="good"] {
--hue: #1fa750;
}
.dh-alert[data-kind="warn"] {
--hue: #e39b0b;
}
.dh-alert[data-kind="bad"] {
--hue: #d92d20;
}
.dh-alert > div {
flex: 1;
min-width: 0;
}
.dh-alert strong {
display: block;
font-weight: 500;
letter-spacing: -0.01em;
}
.dh-alert p {
margin: 0;
color: var(--dh-muted, #5c5c5c);
}
.dh-alert > a {
display: grid;
flex: none;
place-items: center;
height: 28px;
padding: 0 10px;
border-radius: 7px;
background: var(--dh-raised, #fff);
color: inherit;
font-weight: 500;
letter-spacing: -0.01em;
text-decoration: none;
box-shadow:
0 0 0 1px var(--line),
0 1px 2px color-mix(in srgb, var(--dh-fg, #171717) 10%, transparent);
transition: transform 150ms cubic-bezier(0.23, 1, 0.32, 1);
}
.dh-alert > button {
display: grid;
flex: none;
place-items: center;
width: 24px;
height: 24px;
padding: 0;
border: 0;
border-radius: 7px;
background: none;
color: var(--dh-faint, #8a8a8a);
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-alert svg {
width: 12px;
height: 12px;
fill: none;
stroke: currentColor;
stroke-width: 1.4;
stroke-linecap: round;
}
.dh-alert > a:active,
.dh-alert > button:active {
transform: scale(0.96);
}
.dh-alert :is(a, button):focus-visible {
outline: 2px solid var(--dh-faint, #8a8a8a);
outline-offset: 1px;
}
.dh-alert[data-leaving] {
animation: dh-alert-out 150ms cubic-bezier(0.23, 1, 0.32, 1) both;
}
@media (hover: hover) and (pointer: fine) {
.dh-alert > button:hover {
background: color-mix(in srgb, var(--dh-fg, #171717) 6%, transparent);
color: var(--dh-fg, #171717);
}
}
@keyframes dh-alert-in {
from {
opacity: 0;
filter: blur(4px);
transform: translateY(-4px);
}
}
@keyframes dh-alert-out {
to {
opacity: 0;
filter: blur(4px);
transform: scale(0.98);
}
}
@media (prefers-reduced-motion: reduce) {
.dh-alert,
.dh-alert[data-leaving] {
animation: none;
}
.dh-alert > a,
.dh-alert > button {
transition: none;
}
}/* The cross fades its alert out, then takes it off the page. */
document.querySelectorAll(".dh-alert > button").forEach((button) => {
button.addEventListener("click", () => {
const alert = button.closest(".dh-alert");
alert.setAttribute("data-leaving", "");
setTimeout(() => alert.remove(), 150);
});
});Alert is not in the Figma file yet.
Use the React or HTML code for now.