Files
factoryOps-v2/dashboard/components/Toast.tsx

50 lines
1.2 KiB
TypeScript
Raw Normal View History

'use client';
import React, { useEffect, useState } from 'react';
export type ToastType = 'success' | 'error' | 'info';
export interface ToastProps {
id: string;
message: string;
type: ToastType;
duration?: number;
onClose: (id: string) => void;
}
export const Toast: React.FC<ToastProps> = ({ id, message, type, duration = 3000, onClose }) => {
const [isExiting, setIsExiting] = useState(false);
useEffect(() => {
if (duration > 0) {
const timer = setTimeout(() => {
handleClose();
}, duration);
return () => clearTimeout(timer);
}
}, [duration]);
const handleClose = () => {
setIsExiting(true);
setTimeout(() => {
onClose(id);
}, 300);
};
const iconMap: Record<ToastType, string> = {
success: 'check_circle',
error: 'error',
info: 'info',
};
return (
<div className={`toast toast-${type} ${isExiting ? 'toast-exit' : 'toast-enter'}`}>
<span className="material-symbols-outlined toast-icon">{iconMap[type]}</span>
<span className="toast-message">{message}</span>
<button className="toast-close" onClick={handleClose}>
<span className="material-symbols-outlined">close</span>
</button>
</div>
);
};