import React, { useEffect, useState } from 'react'; import { CheckCircle2, AlertCircle, X } from 'lucide-react'; export type ToastType = 'success' | 'error'; interface ToastProps { message: string; type: ToastType; isVisible: boolean; onClose: () => void; duration?: number; } const Toast: React.FC = ({ message, type, isVisible, onClose, duration = 2500 }) => { const [shouldRender, setShouldRender] = useState(false); const [isAnimating, setIsAnimating] = useState(false); useEffect(() => { if (isVisible) { setShouldRender(true); // 触发动画 const animateTimer = setTimeout(() => setIsAnimating(true), 10); const autoCloseTimer = setTimeout(() => { setIsAnimating(false); // 等待动画完成后再移除DOM setTimeout(() => { setShouldRender(false); onClose(); }, 300); }, duration); return () => { clearTimeout(animateTimer); clearTimeout(autoCloseTimer); }; } else { // 当isVisible变为false时,先触发淡出动画 setIsAnimating(false); const hideTimer = setTimeout(() => { setShouldRender(false); }, 300); return () => clearTimeout(hideTimer); } }, [isVisible, duration, onClose]); if (!shouldRender) return null; const icon = type === 'success' ? ( ) : ( ); const bgColor = type === 'success' ? 'bg-green-50 border-green-200' : 'bg-red-50 border-red-200'; const textColor = type === 'success' ? 'text-green-700' : 'text-red-700'; return (
{icon} {message}
); }; export default Toast;