gen / src /components /Toast.tsx
AnesKAM's picture
Upload 71 files
fff5af9 verified
Raw History Blame Contribute Delete
1.97 kB
"use client";
import {
createContext,
useCallback,
useContext,
useRef,
useState,
type ReactNode,
} from "react";
import Icon from "@/components/Icon";
import { cn } from "@/lib/utils";
type ToastType = "success" | "error" | "info";
interface Toast {
id: number;
message: string;
type: ToastType;
}
const ToastContext = createContext<{ toast: (message: string, type?: ToastType) => void }>({
toast: () => undefined,
});
export function useToast() {
return useContext(ToastContext);
}
export function ToastProvider({ children }: { children: ReactNode }) {
const [toasts, setToasts] = useState<Toast[]>([]);
const nextId = useRef(1);
const toast = useCallback((message: string, type: ToastType = "success") => {
const id = nextId.current++;
setToasts((prev) => [...prev.slice(-3), { id, message, type }]);
setTimeout(() => {
setToasts((prev) => prev.filter((t) => t.id !== id));
}, 3400);
}, []);
return (
<ToastContext.Provider value={{ toast }}>
{children}
<div className="pointer-events-none fixed bottom-5 right-5 z-[100] flex w-[calc(100vw-2.5rem)] max-w-sm flex-col gap-2">
{toasts.map((t) => (
<div
key={t.id}
className={cn(
"pointer-events-auto flex animate-toast-in items-center gap-2.5 rounded-xl border px-4 py-3 text-sm font-medium shadow-lg",
t.type === "error"
? "border-red-200 bg-red-50 text-red-800"
: t.type === "info"
? "border-slate-200 bg-slate-900 text-white"
: "border-emerald-200 bg-emerald-50 text-emerald-800",
)}
>
<Icon
name={t.type === "error" ? "alert" : t.type === "info" ? "sparkles" : "check"}
className="h-4 w-4"
/>
<span className="flex-1">{t.message}</span>
</div>
))}
</div>
</ToastContext.Provider>
);
}