57 lines
2.2 KiB
TypeScript
57 lines
2.2 KiB
TypeScript
import { useEffect, useState } from "react";
|
|
|
|
interface ToastProps {
|
|
message: string;
|
|
type: "success" | "error";
|
|
onClose: () => void;
|
|
}
|
|
|
|
export default function Toast({ message, type, onClose }: ToastProps) {
|
|
const [isVisible, setIsVisible] = useState(false);
|
|
|
|
useEffect(() => {
|
|
setIsVisible(true);
|
|
const timer = setTimeout(() => {
|
|
setIsVisible(false);
|
|
setTimeout(onClose, 300); // Wait for animation to complete
|
|
}, 3000);
|
|
|
|
return () => clearTimeout(timer);
|
|
}, [onClose]);
|
|
|
|
const bgColor = type === "success" ? "bg-green-500" : "bg-red-500";
|
|
const icon = type === "success" ? (
|
|
<svg className="w-5 h-5 text-white" fill="currentColor" viewBox="0 0 20 20">
|
|
<path fillRule="evenodd" d="M16.707 5.293a1 1 0 010 1.414l-8 8a1 1 0 01-1.414 0l-4-4a1 1 0 011.414-1.414L8 12.586l7.293-7.293a1 1 0 011.414 0z" clipRule="evenodd" />
|
|
</svg>
|
|
) : (
|
|
<svg className="w-5 h-5 text-white" fill="currentColor" viewBox="0 0 20 20">
|
|
<path fillRule="evenodd" d="M4.293 4.293a1 1 0 011.414 0L10 8.586l4.293-4.293a1 1 0 111.414 1.414L11.414 10l4.293 4.293a1 1 0 01-1.414 1.414L10 11.414l-4.293 4.293a1 1 0 01-1.414-1.414L8.586 10 4.293 5.707a1 1 0 010-1.414z" clipRule="evenodd" />
|
|
</svg>
|
|
);
|
|
|
|
return (
|
|
<div className="fixed top-4 right-4 z-50">
|
|
<div
|
|
className={`${bgColor} text-white px-6 py-4 rounded-lg shadow-lg flex items-center space-x-3 transform transition-all duration-300 ${
|
|
isVisible ? "translate-x-0 opacity-100" : "translate-x-full opacity-0"
|
|
}`}
|
|
>
|
|
{icon}
|
|
<span className="font-medium">{message}</span>
|
|
<button
|
|
onClick={() => {
|
|
setIsVisible(false);
|
|
setTimeout(onClose, 300);
|
|
}}
|
|
className="ml-4 text-white hover:text-gray-200"
|
|
>
|
|
<svg className="w-4 h-4" fill="currentColor" viewBox="0 0 20 20">
|
|
<path fillRule="evenodd" d="M4.293 4.293a1 1 0 011.414 0L10 8.586l4.293-4.293a1 1 0 111.414 1.414L11.414 10l4.293 4.293a1 1 0 01-1.414 1.414L10 11.414l-4.293 4.293a1 1 0 01-1.414-1.414L8.586 10 4.293 5.707a1 1 0 010-1.414z" clipRule="evenodd" />
|
|
</svg>
|
|
</button>
|
|
</div>
|
|
</div>
|
|
);
|
|
}
|