'use client' import { CloseCircle, InfoCircle, TickCircle } from 'iconsax-react'; import React, { useState, useEffect } from 'react'; interface Toast { id: string; message: string; type?: 'success' | 'error' | 'info'; isExiting?: boolean; } let addToast: (toast: Toast) => void; const ToastContainer: React.FC = () => { const [toasts, setToasts] = useState([]); // ثبت toast جدید const showToast = (toast: Toast) => { setToasts((prev) => [...prev, toast]); // حذف خودکار بعد از ۳ ثانیه setTimeout(() => { setToasts((prev) => prev.map(t => t.id === toast.id ? { ...t, isExiting: true } : t )); // حذف از DOM بعد از اتمام انیمیشن setTimeout(() => { setToasts((prev) => prev.filter((t) => t.id !== toast.id)); }, 300); // مدت زمان انیمیشن خروج }, 3000); }; // تخصیص تابع نمایش toast به متغیر سراسری useEffect(() => { addToast = showToast; }, []); return (
{toasts.map((toast) => (
{ toast.type === 'success' ? : toast.type === 'error' ? : } {toast.message}
))}
); }; export const toast = (message?: string, type: 'success' | 'error' | 'info' = 'info') => { addToast({ id: Date.now().toString(), message: message || '', type }); }; export default ToastContainer;