'use client' import { useState, useEffect } from 'react' import { createPortal } from 'react-dom' export interface ToastProps { message: string type: 'success' | 'error' | 'warning' | 'info' duration?: number onClose: () => void } const TOAST_ICONS = { success: '✅', error: '❌', warning: '⚠️', info: 'ℹ️' } as const const TOAST_STYLES = { success: 'bg-green-50 border-green-200 text-green-800', error: 'bg-red-50 border-red-200 text-red-800', warning: 'bg-yellow-50 border-yellow-200 text-yellow-800', info: 'bg-blue-50 border-blue-200 text-blue-800' } as const export function Toast({ message, type, duration = 3000, onClose }: ToastProps) { const [isVisible, setIsVisible] = useState(false) const [mounted, setMounted] = useState(false) useEffect(() => { setMounted(true) // 出現動畫 const showTimer = setTimeout(() => setIsVisible(true), 50) // 自動消失 const hideTimer = setTimeout(() => { setIsVisible(false) // 等待動畫完成後關閉 setTimeout(onClose, 300) }, duration) return () => { clearTimeout(showTimer) clearTimeout(hideTimer) } }, [duration, onClose]) if (!mounted) return null return createPortal(
{message}