Stacked toasts
Notifications that pile up into a neat stack, fan out when you hover, pause their timers, and can be swiped away or undone.
"use client";
import {
useEffect,
useRef,
useState,
type CSSProperties,
type PointerEvent as ReactPointerEvent,
} from "react";
import "./toast-stack.css";
type Kind = "success" | "info" | "error";
type Toast = {
id: number;
kind: Kind;
title: string;
body: string;
action?: string;
};
const SAMPLES: Omit<Toast, "id">[] = [
{
kind: "success",
title: "Invoice sent",
body: "Northwind will get it in a minute.",
action: "Undo",
},
{
kind: "info",
title: "Maya joined the project",
body: "She can edit pages and comments.",
},
{
kind: "error",
title: "Upload failed",
body: "hero-video.mp4 is over 200 MB.",
action: "Retry",
},
{
kind: "success",
title: "Changes published",
body: "Your site is live at acme.studio.",
},
];
const LIFETIME = 5000;
/** One toast. Owns its timer (paused while the stack is expanded) and its swipe gesture. */
function ToastItem({
t,
index,
expanded,
onClose,
}: {
t: Toast;
index: number;
expanded: boolean;
onClose: () => void;
}) {
const [dx, setDx] = useState(0);
const [leaving, setLeaving] = useState(false);
const [dragging, setDragging] = useState(false);
const start = useRef<number | null>(null);
const left = useRef(LIFETIME);
const closeRef = useRef(onClose);
useEffect(() => {
closeRef.current = onClose;
});
const dismiss = () => {
setLeaving(true);
setTimeout(() => closeRef.current(), 250);
};
// Count down only while collapsed, so hovering the stack "pauses" every toast.
useEffect(() => {
if (expanded) return;
const since = performance.now();
const timer = setTimeout(() => {
setLeaving(true);
setTimeout(() => closeRef.current(), 250);
}, left.current);
return () => {
clearTimeout(timer);
left.current -= performance.now() - since;
};
}, [expanded]);
const down = (e: ReactPointerEvent<HTMLLIElement>) => {
if ((e.target as HTMLElement).closest("button")) return;
start.current = e.clientX;
e.currentTarget.setPointerCapture(e.pointerId);
};
const move = (e: ReactPointerEvent<HTMLLIElement>) => {
if (start.current === null) return;
setDragging(true);
setDx(e.clientX - start.current);
};
const up = () => {
if (start.current === null) return;
start.current = null;
setDragging(false);
if (Math.abs(dx) > 90) dismiss();
else setDx(0);
};
return (
<li
className="gu-toast"
data-kind={t.kind}
data-leaving={leaving ? "" : undefined}
data-dragging={dragging ? "" : undefined}
style={{ "--i": index, "--dx": `${dx}px` } as CSSProperties}
role={t.kind === "error" ? "alert" : "status"}
onPointerDown={down}
onPointerMove={move}
onPointerUp={up}
onPointerCancel={up}
>
<span className="gu-toast__icon" aria-hidden="true">
{t.kind === "success" ? "✓" : t.kind === "error" ? "!" : "i"}
</span>
<span className="gu-toast__text">
<b>{t.title}</b>
<small>{t.body}</small>
</span>
{t.action && (
<button type="button" className="gu-toast__action" onClick={dismiss}>
{t.action}
</button>
)}
<button
type="button"
className="gu-toast__close"
onClick={dismiss}
aria-label={`Dismiss: ${t.title}`}
>
×
</button>
<span
className="gu-toast__timer"
data-paused={expanded ? "" : undefined}
style={{ "--life": `${LIFETIME}ms` } as CSSProperties}
aria-hidden="true"
/>
</li>
);
}
/**
* ToastStack
* - Newest toast on top. Collapsed, older toasts peek out behind it (scaled down, offset up).
* - Hover or focus inside expands the stack into a list and pauses all timers.
*/
export function ToastStack({ theme = "light" }: { theme?: "light" | "dark" }) {
const [toasts, setToasts] = useState<Toast[]>([]);
const [expanded, setExpanded] = useState(false);
const next = useRef(0);
const push = () => {
const id = next.current++;
const s = SAMPLES[id % SAMPLES.length];
setToasts((list) => [{ ...s, id }, ...list].slice(0, 5));
};
// One toast on load so the demo isn't empty.
useEffect(() => {
const t = setTimeout(() => {
const id = next.current++;
setToasts((list) => [{ ...SAMPLES[0], id }, ...list]);
}, 400);
return () => clearTimeout(t);
}, []);
return (
<div className="gu-toasts" data-theme={theme}>
<button type="button" className="gu-toasts__trigger" onClick={push}>
Show a toast
</button>
<ol
className="gu-toasts__stack"
aria-label="Notifications"
data-expanded={expanded ? "" : undefined}
style={{ "--count": Math.min(toasts.length, 3) } as CSSProperties}
onPointerEnter={(e) => e.pointerType === "mouse" && setExpanded(true)}
onPointerLeave={(e) => e.pointerType === "mouse" && setExpanded(false)}
onClick={() => setExpanded(true)}
onFocus={() => setExpanded(true)}
onBlur={(e) =>
!e.currentTarget.contains(e.relatedTarget) && setExpanded(false)
}
>
{toasts.map((t, i) => (
<ToastItem
key={t.id}
t={t}
index={i}
expanded={expanded}
onClose={() =>
setToasts((list) => list.filter((x) => x.id !== t.id))
}
/>
))}
</ol>
</div>
);
}
Paste this into ChatGPT, Claude, Cursor, v0, Lovable or Bolt. It describes the layout, motion, colors and accessibility and includes the code, so the AI can rebuild it in any framework.
# Build this UI piece: Stacked toasts
You are a senior front-end engineer. Recreate the piece below in my project. If I don't say which framework I use, ask once, then build it in that framework (React, Vue, Svelte, Angular, Astro, plain HTML/CSS/JS or anything else). Keep the behavior, motion, colors, spacing and accessibility exactly as described. Keep the code clean and commented for beginners.
## What it is
A toast notification system: new toasts slide up from the bottom and the older ones tuck behind in a neat stack. Hovering (or tapping, or focusing) expands the stack into a list and pauses every timer. Toasts can be swiped away, closed or acted on ("Undo", "Retry").
## Layout and look
- Toasts: 76px+, radius 18px, card background, hairline border, soft shadow. A 32px colored icon tile (green ✓, blue i, red !), bold title, one-line muted body, optional outline action pill, a round × button, and a 3px orange lifetime bar along the bottom that shrinks.
- Collapsed stack: toast i is moved up 12px × i, scaled 1 − 0.05 × i, and faded; only three are visible.
- Expanded: each toast sits 86px above the previous one.
- Demo trigger: orange pill "Show a toast".
## Behavior and motion
1. Newest first, max 5. Each lives 5s; the remaining time is saved when the stack expands and resumes when it collapses (the bar pauses with animation-play-state).
2. Enter: slide up from 110% with fade (500ms cubic-bezier(0.16, 1, 0.3, 1)). Leave: slide right 40px and fade (250ms).
3. Swipe horizontally past 90px to dismiss; otherwise spring back.
## Accessibility
- Success/info toasts are role="status", errors role="alert". Close buttons say "Dismiss: {title}". Focus inside expands the stack.
- prefers-reduced-motion: short fades only.
## Props
theme. Edit SAMPLES and LIFETIME; call push() from your app.
## Reference implementation
The original is written in React with one plain CSS file. Dependencies: none (plain React and CSS). Translate it faithfully; do not add new libraries unless my framework needs them.
### ToastStack.tsx
```tsx
"use client";
import {
useEffect,
useRef,
useState,
type CSSProperties,
type PointerEvent as ReactPointerEvent,
} from "react";
import "./toast-stack.css";
type Kind = "success" | "info" | "error";
type Toast = {
id: number;
kind: Kind;
title: string;
body: string;
action?: string;
};
const SAMPLES: Omit<Toast, "id">[] = [
{
kind: "success",
title: "Invoice sent",
body: "Northwind will get it in a minute.",
action: "Undo",
},
{
kind: "info",
title: "Maya joined the project",
body: "She can edit pages and comments.",
},
{
kind: "error",
title: "Upload failed",
body: "hero-video.mp4 is over 200 MB.",
action: "Retry",
},
{
kind: "success",
title: "Changes published",
body: "Your site is live at acme.studio.",
},
];
const LIFETIME = 5000;
/** One toast. Owns its timer (paused while the stack is expanded) and its swipe gesture. */
function ToastItem({
t,
index,
expanded,
onClose,
}: {
t: Toast;
index: number;
expanded: boolean;
onClose: () => void;
}) {
const [dx, setDx] = useState(0);
const [leaving, setLeaving] = useState(false);
const [dragging, setDragging] = useState(false);
const start = useRef<number | null>(null);
const left = useRef(LIFETIME);
const closeRef = useRef(onClose);
useEffect(() => {
closeRef.current = onClose;
});
const dismiss = () => {
setLeaving(true);
setTimeout(() => closeRef.current(), 250);
};
// Count down only while collapsed, so hovering the stack "pauses" every toast.
useEffect(() => {
if (expanded) return;
const since = performance.now();
const timer = setTimeout(() => {
setLeaving(true);
setTimeout(() => closeRef.current(), 250);
}, left.current);
return () => {
clearTimeout(timer);
left.current -= performance.now() - since;
};
}, [expanded]);
const down = (e: ReactPointerEvent<HTMLLIElement>) => {
if ((e.target as HTMLElement).closest("button")) return;
start.current = e.clientX;
e.currentTarget.setPointerCapture(e.pointerId);
};
const move = (e: ReactPointerEvent<HTMLLIElement>) => {
if (start.current === null) return;
setDragging(true);
setDx(e.clientX - start.current);
};
const up = () => {
if (start.current === null) return;
start.current = null;
setDragging(false);
if (Math.abs(dx) > 90) dismiss();
else setDx(0);
};
return (
<li
className="gu-toast"
data-kind={t.kind}
data-leaving={leaving ? "" : undefined}
data-dragging={dragging ? "" : undefined}
style={{ "--i": index, "--dx": `${dx}px` } as CSSProperties}
role={t.kind === "error" ? "alert" : "status"}
onPointerDown={down}
onPointerMove={move}
onPointerUp={up}
onPointerCancel={up}
>
<span className="gu-toast__icon" aria-hidden="true">
{t.kind === "success" ? "✓" : t.kind === "error" ? "!" : "i"}
</span>
<span className="gu-toast__text">
<b>{t.title}</b>
<small>{t.body}</small>
</span>
{t.action && (
<button type="button" className="gu-toast__action" onClick={dismiss}>
{t.action}
</button>
)}
<button
type="button"
className="gu-toast__close"
onClick={dismiss}
aria-label={`Dismiss: ${t.title}`}
>
×
</button>
<span
className="gu-toast__timer"
data-paused={expanded ? "" : undefined}
style={{ "--life": `${LIFETIME}ms` } as CSSProperties}
aria-hidden="true"
/>
</li>
);
}
/**
* ToastStack
* - Newest toast on top. Collapsed, older toasts peek out behind it (scaled down, offset up).
* - Hover or focus inside expands the stack into a list and pauses all timers.
*/
export function ToastStack({ theme = "light" }: { theme?: "light" | "dark" }) {
const [toasts, setToasts] = useState<Toast[]>([]);
const [expanded, setExpanded] = useState(false);
const next = useRef(0);
const push = () => {
const id = next.current++;
const s = SAMPLES[id % SAMPLES.length];
setToasts((list) => [{ ...s, id }, ...list].slice(0, 5));
};
// One toast on load so the demo isn't empty.
useEffect(() => {
const t = setTimeout(() => {
const id = next.current++;
setToasts((list) => [{ ...SAMPLES[0], id }, ...list]);
}, 400);
return () => clearTimeout(t);
}, []);
return (
<div className="gu-toasts" data-theme={theme}>
<button type="button" className="gu-toasts__trigger" onClick={push}>
Show a toast
</button>
<ol
className="gu-toasts__stack"
aria-label="Notifications"
data-expanded={expanded ? "" : undefined}
style={{ "--count": Math.min(toasts.length, 3) } as CSSProperties}
onPointerEnter={(e) => e.pointerType === "mouse" && setExpanded(true)}
onPointerLeave={(e) => e.pointerType === "mouse" && setExpanded(false)}
onClick={() => setExpanded(true)}
onFocus={() => setExpanded(true)}
onBlur={(e) =>
!e.currentTarget.contains(e.relatedTarget) && setExpanded(false)
}
>
{toasts.map((t, i) => (
<ToastItem
key={t.id}
t={t}
index={i}
expanded={expanded}
onClose={() =>
setToasts((list) => list.filter((x) => x.id !== t.id))
}
/>
))}
</ol>
</div>
);
}
```
### toast-stack.css
```css
/* ToastStack: change these colors to match your brand. */
.gu-toasts {
--toast: #fbf8f2;
--text: #0e0e10;
--muted: #57534b;
--line: rgb(14 14 16 / 0.12);
--success: #16a34a;
--info: #2b35ff;
--error: #dc2626;
--accent: #ff5a1f;
--focus: #2b35ff;
--ease: cubic-bezier(0.16, 1, 0.3, 1);
position: relative;
display: grid;
justify-items: center;
width: min(100%, 400px);
min-height: 380px;
color: var(--text);
font-family: system-ui, sans-serif;
}
.gu-toasts[data-theme="dark"] {
--toast: #1f1f23;
--text: #f3eee3;
--muted: #aea89c;
--line: rgb(243 238 227 / 0.12);
--success: #4ade80;
--info: #8b93ff;
--error: #f87171;
--focus: #d7ff3a;
}
.gu-toasts button {
font: inherit;
cursor: pointer;
}
.gu-toasts :focus-visible {
outline: 3px solid var(--focus);
outline-offset: 2px;
}
.gu-toasts__trigger {
min-height: 50px;
padding: 0 22px;
border: 0;
border-radius: 999px;
background: var(--accent);
color: #0e0e10;
font-weight: 800 !important;
}
.gu-toasts__stack {
position: absolute;
left: 0;
right: 0;
bottom: 0;
height: calc(80px + 26px);
margin: 0;
padding: 0;
list-style: none;
transition: height 0.4s var(--ease);
}
.gu-toasts__stack[data-expanded] {
height: calc(var(--count) * 86px);
}
.gu-toast {
position: absolute;
left: 0;
right: 0;
bottom: 0;
z-index: calc(10 - var(--i));
display: grid;
grid-template-columns: 32px 1fr auto auto;
align-items: center;
gap: 12px;
min-height: 76px;
padding: 12px 10px 12px 14px;
overflow: hidden;
border: 1px solid var(--line);
border-radius: 18px;
background: var(--toast);
box-shadow: 0 16px 36px -18px rgb(0 0 0 / 0.4);
touch-action: pan-y;
/* Collapsed: older toasts tuck behind, smaller and higher */
transform: translateX(var(--dx)) translateY(calc(var(--i) * -12px))
scale(calc(1 - var(--i) * 0.05));
opacity: calc(1 - var(--i) * 0.25 - min(1, max(0, (var(--i) - 2))) * 0.5);
transition:
transform 0.45s var(--ease),
opacity 0.3s;
animation: gu-toast-in 0.5s var(--ease);
}
@keyframes gu-toast-in {
from {
transform: translateY(110%);
opacity: 0;
}
}
.gu-toasts__stack[data-expanded] .gu-toast {
opacity: 1;
transform: translateX(var(--dx)) translateY(calc(var(--i) * -86px));
}
.gu-toast:nth-child(n + 4) {
visibility: hidden;
}
.gu-toasts__stack[data-expanded] .gu-toast:nth-child(n + 4) {
visibility: visible;
}
.gu-toast[data-dragging] {
transition: none;
}
.gu-toast[data-leaving] {
opacity: 0;
transform: translateX(calc(var(--dx) + 40px))
translateY(calc(var(--i) * -12px)) scale(0.95);
transition:
transform 0.25s,
opacity 0.25s;
}
.gu-toast__icon {
display: grid;
place-items: center;
width: 32px;
height: 32px;
border-radius: 10px;
color: #fff;
font-weight: 900;
}
.gu-toast[data-kind="success"] .gu-toast__icon {
background: var(--success);
}
.gu-toast[data-kind="info"] .gu-toast__icon {
background: var(--info);
font-family: Georgia, serif;
}
.gu-toast[data-kind="error"] .gu-toast__icon {
background: var(--error);
}
.gu-toasts[data-theme="dark"] .gu-toast__icon {
color: #0e0e10;
}
.gu-toast__text {
display: grid;
min-width: 0;
}
.gu-toast__text b {
font-size: 0.95rem;
}
.gu-toast__text small {
overflow: hidden;
color: var(--muted);
text-overflow: ellipsis;
white-space: nowrap;
}
.gu-toast__action {
min-height: 36px;
padding: 0 12px;
border: 1px solid var(--line);
border-radius: 999px;
background: none;
color: inherit;
font-size: 0.85rem !important;
font-weight: 800 !important;
}
.gu-toast__close {
width: 32px;
height: 32px;
border: 0;
border-radius: 50%;
background: none;
color: var(--muted);
font-size: 1.3rem !important;
line-height: 1;
}
.gu-toast__close:hover {
background: var(--line);
color: var(--text);
}
/* Lifetime bar along the bottom edge */
.gu-toast__timer {
position: absolute;
left: 0;
right: 0;
bottom: 0;
height: 3px;
background: var(--accent);
transform-origin: left;
animation: gu-toast-life var(--life) linear forwards;
}
.gu-toast__timer[data-paused] {
animation-play-state: paused;
}
@keyframes gu-toast-life {
to {
transform: scaleX(0);
}
}
@media (prefers-reduced-motion: reduce) {
.gu-toast,
.gu-toasts__stack {
transition: opacity 0.15s;
}
.gu-toast {
animation: gu-toast-fade 0.15s;
}
@keyframes gu-toast-fade {
from {
opacity: 0;
}
}
}
```
You might also want
Next piece: Star ratingCart drawer
Components, advanced
Activity rings
Components, intermediate
Draggable bottom sheet
Components, advanced