A black pill that morphs between states, from a quiet dot to a notification, a music player or a live timer, springing to each new size.
Stacking notifications with swipe-to-dismiss, hover-to-pause and variants.
Customize
Usage
import { ToastProvider, useToast } from "@/components/motionkit/toast";
// 1. Wrap your app once (e.g. in app/layout.tsx)
<ToastProvider>{children}</ToastProvider>
// 2. Call it from any client component inside
const toast = useToast();
toast({
title: "Changes saved",
description: "Your project is up to date.",
variant: "success",
});Motion
The exact curves this component runs, plotted with Motion's own maths.
Enter
A toast appears
Save confirmations, undo prompts, errors and background job updates in any web app.
A provider holds the list of toasts and a hook adds them from anywhere. Each toast uses Motion's layout animation, so the stack reflows smoothly as toasts come and go. You can swipe one away on the x axis, and hovering or tabbing into a toast pauses its timer. They sit in a polite live region, so screen readers announce them without interrupting.
Works in the App Router as is. Requires Tailwind CSS v4.
One command adds the file and installs its dependencies. Needs a components.json (run npx shadcn@latest init once if you don't have one).
npx shadcn@latest add https://www.motionkitui.com/r/toast.jsonnpm install motionSave as components/motionkit/toast.tsx.
"use client";
import { createContext, useCallback, useContext, useEffect, useRef, useState, type ReactNode } from "react";
import { AnimatePresence, motion, useReducedMotion } from "motion/react";
type ToastVariant = "success" | "error" | "info";
type ToastOptions = {
title: string;
description?: string;
variant?: ToastVariant;
/** Milliseconds before auto-dismiss. Use 0 to keep it open. */
duration?: number;
};
type ToastItem = Required<Omit<ToastOptions, "description">> & { id: number; description?: string };
const ToastContext = createContext<((options: ToastOptions) => void) | null>(null);
const accents: Record<ToastVariant, string> = {
success: "bg-[#7D9E9D]",
error: "bg-[#8C3216]",
info: "bg-[#0E7C86]",
};
export function ToastProvider({ children }: { children: ReactNode }) {
const [toasts, setToasts] = useState<ToastItem[]>([]);
const nextId = useRef(0);
const dismiss = useCallback((id: number) => {
setToasts((current) => current.filter((toast) => toast.id !== id));
}, []);
const toast = useCallback((options: ToastOptions) => {
const id = nextId.current++;
setToasts((current) => [
...current.slice(-3),
{ variant: "info", duration: 4000, ...options, id },
]);
}, []);
return (
<ToastContext.Provider value={toast}>
{children}
<ol
aria-live="polite"
className="pointer-events-none fixed bottom-4 right-4 z-50 flex w-[calc(100%-2rem)] max-w-sm flex-col gap-2"
>
<AnimatePresence initial={false}>
{toasts.map((item) => (
<ToastCard key={item.id} item={item} onDismiss={dismiss} />
))}
</AnimatePresence>
</ol>
</ToastContext.Provider>
);
}
function ToastCard({ item, onDismiss }: { item: ToastItem; onDismiss: (id: number) => void }) {
const [paused, setPaused] = useState(false);
const reduceMotion = useReducedMotion();
const close = () => onDismiss(item.id);
useEffect(() => {
if (!item.duration || paused) return;
const timer = window.setTimeout(() => onDismiss(item.id), item.duration);
return () => window.clearTimeout(timer);
}, [item.id, item.duration, paused, onDismiss]);
return (
<motion.li
layout
role="status"
initial={reduceMotion ? { opacity: 0 } : { opacity: 0, y: 24, scale: 0.95 }}
animate={{ opacity: 1, y: 0, scale: 1 }}
exit={reduceMotion ? { opacity: 0 } : { opacity: 0, x: 40, scale: 0.95, transition: { duration: 0.18 } }}
transition={{ type: "spring", bounce: 0.25, duration: 0.4 }}
drag="x"
dragConstraints={{ left: 0, right: 0 }}
dragElastic={{ left: 0, right: 0.6 }}
onDragEnd={(_, info) => info.offset.x > 80 && close()}
// hovering or tabbing into a toast pauses its timer, so nobody loses it mid-read
onPointerEnter={() => setPaused(true)}
onPointerLeave={() => setPaused(false)}
onFocus={() => setPaused(true)}
onBlur={() => setPaused(false)}
className="pointer-events-auto relative flex gap-3 overflow-hidden rounded-xl border border-white/10 bg-[#141B1B]/95 p-4 pr-10 text-sm text-[#F6F1E8] shadow-2xl shadow-black/40 backdrop-blur"
>
<span className={`mt-1.5 size-2 shrink-0 rounded-full ${accents[item.variant]}`} />
<div>
<p className="font-medium">{item.title}</p>
{item.description && <p className="mt-1 text-[#94A9A7]">{item.description}</p>}
</div>
<button
onClick={close}
aria-label="Dismiss notification"
className="absolute right-3 top-3 rounded-md px-1.5 text-[#94A9A7] transition-colors hover:text-[#F6F1E8] focus-visible:outline-2 focus-visible:outline-[#5FAEB4]"
>
×
</button>
</motion.li>
);
}
export function useToast() {
const toast = useContext(ToastContext);
if (!toast) throw new Error("useToast must be used inside <ToastProvider>");
return toast;
}
| Prop | Type | Default | Description |
|---|---|---|---|
| title | string | — | Main message. |
| description | string | — | Optional secondary text. |
| variant | "success" | "error" | "info" | "info" | Accent color. |
| duration | number | 4000 | Milliseconds before auto-dismiss; 0 keeps it open. |
How we audit: the WCAG 2.2 animation checklist and respecting reduced motion in React.
Yes. The file starts with "use client", so you can import it straight into a Server Component page or layout. It works in Vite as is, and in Astro as a React island.
Only the motion package, beside React and Tailwind CSS v4. There is no MotionKit package and no Tailwind config to add.
Yes. Run npx shadcn@latest add https://www.motionkitui.com/r/toast.json in any project with a components.json. It saves the file to components/motionkit and installs its dependencies.
Toasts fade only, with no slide or scale, for reduced-motion users.
A black pill that morphs between states, from a quiet dot to a notification, a music player or a live timer, springing to each new size.
Progress through steps: the line fills, and each finished step draws its own tick.
A live status indicator for services and deploys: the dot blends between colours and pulses softly only when something needs attention.