Application
Toast
Provider-free toast system: render <Toaster /> once and call toast() from anywhere.
Motionspring enter/exit + layout reflow via AnimatePresence
Install
Install Kineto and follow the theme setup once in your app.
bash
npm install kinetotsx
import { toast, dismissToast, Toaster } from "kineto/toast";Preview
tsx
"use client";
import * as React from "react";
import { AnimatePresence, motion } from "motion/react";
import {
CircleAlert,
CircleCheck,
Info,
TriangleAlert,
X,
type LucideIcon,
} from "lucide-react";
import { cn } from "@/lib/cn";
import { spring } from "@/lib/motion-presets";
export type ToastVariant = "default" | "brand" | "success" | "warning" | "destructive";
export interface ToastOptions {
id?: string;
title?: React.ReactNode;
description?: React.ReactNode;
variant?: ToastVariant;
/** Auto-dismiss after ms. 0 keeps it open. */
duration?: number;
}
interface ToastItem extends ToastOptions {
id: string;
}
/* Minimal motion-powered toast store — no provider required, just render <Toaster />. */
let items: ToastItem[] = [];
const listeners = new Set<(items: ToastItem[]) => void>();
function emit() {
for (const listener of listeners) listener(items);
}
export function toast(options: ToastOptions): string {
const id = options.id ?? Math.random().toString(36).slice(2);
const item: ToastItem = { duration: 4000, variant: "default", ...options, id };
items = [...items.filter((t) => t.id !== id), item];
emit();
if (item.duration && item.duration > 0) {
setTimeout(() => dismissToast(id), item.duration);
}
return id;
}
export function dismissToast(id: string) {
items = items.filter((t) => t.id !== id);
emit();
}
function useToasts(): ToastItem[] {
const [state, setState] = React.useState<ToastItem[]>(items);
React.useEffect(() => {
listeners.add(setState);
setState(items);
return () => {
listeners.delete(setState);
};
}, []);
return state;
}
const icons: Record<ToastVariant, LucideIcon> = {
default: Info,
brand: Info,
success: CircleCheck,
warning: TriangleAlert,
destructive: CircleAlert,
};
const iconColor: Record<ToastVariant, string> = {
default: "text-muted-foreground",
brand: "text-brand",
success: "text-success",
warning: "text-warning",
destructive: "text-destructive",
};
export interface ToasterProps {
position?: "bottom-right" | "top-right" | "bottom-center";
}
export function Toaster({ position = "bottom-right" }: ToasterProps) {
const toasts = useToasts();
return (
<div
aria-live="polite"
className={cn(
"pointer-events-none fixed z-[100] flex w-full max-w-sm flex-col gap-2 p-4",
position === "top-right" && "right-0 top-0",
position === "bottom-right" && "bottom-0 right-0",
position === "bottom-center" && "bottom-0 left-1/2 -translate-x-1/2"
)}
>
<AnimatePresence initial={false}>
{toasts.map((t) => {
const variant = t.variant ?? "default";
const Icon = icons[variant];
return (
<motion.div
key={t.id}
layout
initial={{ opacity: 0, x: 48, scale: 0.96 }}
animate={{ opacity: 1, x: 0, scale: 1 }}
exit={{ opacity: 0, x: 48, scale: 0.96 }}
transition={spring.snappy}
className="pointer-events-auto flex items-start gap-3 rounded-xl border border-border bg-popover p-4 shadow-lift"
>
<Icon className={cn("mt-0.5 size-5 shrink-0", iconColor[variant])} />
<div className="flex-1">
{t.title ? (
<p className="text-sm font-medium text-foreground">{t.title}</p>
) : null}
{t.description ? (
<p className="mt-0.5 text-sm text-muted-foreground">
{t.description}
</p>
) : null}
</div>
<button
type="button"
aria-label="Dismiss"
onClick={() => dismissToast(t.id)}
className="-mr-1 -mt-1 inline-flex size-7 shrink-0 items-center justify-center rounded-md text-muted-foreground transition-colors hover:bg-accent hover:text-foreground"
>
<X className="size-4" />
</button>
</motion.div>
);
})}
</AnimatePresence>
</div>
);
}src/components/ui/toast.tsx
Props
| Prop | Type | Default | Description |
|---|---|---|---|
| toast(options) | { title?; description?; variant?; duration? } | — | Imperative trigger. |
| <Toaster position /> | "bottom-right" | "top-right" | "bottom-center" | — | Mount once near the root. |