# Stacked Toasts

> Notifications that pile up politely.

- Category: Overlays
- Tier: Free (MIT)
- Page: https://bowls.dev/components/stacked-toasts
- Requires: React 18+, Tailwind CSS 4, motion

## Install

Copy the source below into `components/bowls/stacked-toasts.tsx`, then install its dependencies:

```bash
npm i motion
```

## Props

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `toasts` | `Toast[]` | — | From the useToasts() hook. |
| `onDismiss` | `(id: number) => void` | — | Remove a toast. |
| `visible` | `number` | `3` | How many stay in the stack. |

## Usage

```tsx
"use client";

import { useEffect } from "react";
import { ToastStack, useToasts } from "@/components/bowls/stacked-toasts";

const MESSAGES = [
  { title: "Deployed", body: "bowls.dev is live · 2s" },
  { title: "Copied", body: "magnetic-dock.tsx" },
  { title: "Added to bowl", body: "Spring Toggle" },
];

export default function Demo() {
  const { toasts, push, dismiss } = useToasts(6000);

  useEffect(() => {
    MESSAGES.forEach((m, i) => setTimeout(() => push(m), 250 + i * 350));
  }, [push]);

  return (
    <div className="flex h-full w-full flex-col items-center justify-center gap-5">
      <ToastStack toasts={toasts} onDismiss={dismiss} />
      <button
        type="button"
        onClick={() => push(MESSAGES[Math.floor(Math.random() * MESSAGES.length)])}
        className="min-h-10 rounded-full border-[1.5px] border-[#0a0a0a] dark:border-[#f5f5f5] px-4 text-[13px] font-semibold hover:bg-[#0a0a0a] dark:hover:bg-[#f5f5f5] hover:text-white dark:hover:text-black"
      >
        Toast me
      </button>
    </div>
  );
}
```

## Source

`components/bowls/stacked-toasts.tsx`

```tsx
"use client";

import { AnimatePresence, motion } from "motion/react";
import { useCallback, useEffect, useRef, useState } from "react";

export type Toast = { id: number; title: string; body?: string };

/** Keeps a list of toasts and removes each after `duration` ms. */
export function useToasts(duration = 4000) {
  const [toasts, setToasts] = useState<Toast[]>([]);
  const next = useRef(1);
  const timers = useRef(new Map<number, ReturnType<typeof setTimeout>>());

  const dismiss = useCallback((id: number) => {
    setToasts((t) => t.filter((x) => x.id !== id));
    clearTimeout(timers.current.get(id));
    timers.current.delete(id);
  }, []);

  const push = useCallback(
    (toast: Omit<Toast, "id">) => {
      const id = next.current++;
      setToasts((t) => [...t, { ...toast, id }]);
      timers.current.set(id, setTimeout(() => dismiss(id), duration));
    },
    [dismiss, duration],
  );

  useEffect(() => {
    const map = timers.current;
    return () => map.forEach(clearTimeout);
  }, []);

  return { toasts, push, dismiss };
}

export type ToastStackProps = { toasts: Toast[]; onDismiss: (id: number) => void; visible?: number };

/** Newest toast in front, older ones tucked behind it. */
export function ToastStack({ toasts, onDismiss, visible = 3 }: ToastStackProps) {
  const shown = toasts.slice(-visible);
  return (
    <ol aria-live="polite" className="relative h-[86px] w-[260px] list-none p-0">
      <AnimatePresence initial={false}>
        {shown.map((t, i) => {
          const depth = shown.length - 1 - i;
          return (
            <motion.li
              key={t.id}
              layout
              initial={{ opacity: 0, y: 30, scale: 0.9 }}
              animate={{ opacity: 1 - depth * 0.2, y: -depth * 12, scale: 1 - depth * 0.06 }}
              exit={{ opacity: 0, x: 60 }}
              transition={{ type: "spring", stiffness: 380, damping: 30 }}
              style={{ zIndex: 10 - depth }}
              className="absolute inset-x-0 bottom-0 flex items-center gap-2.5 rounded-[14px] bg-[#0a0a0a] dark:bg-[#f5f5f5] px-3.5 py-3 text-[13px] text-white dark:text-black shadow-[0_14px_30px_rgba(10,10,10,.25)]"
            >
              <span className="size-6 shrink-0 rounded-lg bg-white dark:bg-black" />
              <span className="min-w-0 flex-1">
                <b className="block font-semibold">{t.title}</b>
                {t.body && <span className="block truncate text-[#9a9a9a] dark:text-[#656565]">{t.body}</span>}
              </span>
              <button type="button" onClick={() => onDismiss(t.id)} aria-label="Dismiss" className="-mr-1 flex size-8 items-center justify-center rounded-full text-[#9a9a9a] dark:text-[#656565] hover:bg-white/10 dark:hover:bg-black/10 hover:text-white dark:hover:text-black">
                ×
              </button>
            </motion.li>
          );
        })}
      </AnimatePresence>
    </ol>
  );
}
```
