import React, { useEffect, useState } from 'react'; export type ToastType = 'success' | 'error' | 'info'; export interface ToastProps { message: string; type: ToastType; onClose: () => void; } export const Toast: React.FC = ({ message, type, onClose }) => { const [visible, setVisible] = useState(false); useEffect(() => { // Start entry animation requestAnimationFrame(() => setVisible(true)); // Auto dismiss const timer = setTimeout(() => { setVisible(false); // Allow exit animation to complete before unmounting setTimeout(onClose, 300); }, 3000); return () => clearTimeout(timer); }, [onClose]); const baseClasses = "fixed top-6 left-1/2 transform -translate-x-1/2 z-[100] flex items-center gap-3 px-5 py-3 rounded-lg shadow-xl text-white font-medium text-sm transition-all duration-300 ease-out"; const typeClasses = { success: "bg-emerald-600 ring-1 ring-emerald-500", error: "bg-rose-600 ring-1 ring-rose-500", info: "bg-indigo-600 ring-1 ring-indigo-500" }; // Animation states const opacityClass = visible ? "opacity-100 translate-y-0 scale-100" : "opacity-0 -translate-y-4 scale-95"; const icons = { success: ( ), error: ( ), info: ( ) }; return (
{icons[type]} {message}
); };