forked from PinSpace-Org/GistPin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathToast.tsx
More file actions
80 lines (67 loc) · 2.16 KB
/
Copy pathToast.tsx
File metadata and controls
80 lines (67 loc) · 2.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
'use client';
import { useEffect, useState } from 'react';
type ToastType = 'success' | 'error' | 'warning' | 'info';
interface ToastProps {
message: string;
type?: ToastType;
duration?: number;
onClose?: () => void;
}
const typeClasses: Record<ToastType, string> = {
success: 'bg-green-50 border-green-400 text-green-800 dark:bg-green-900/30 dark:border-green-500 dark:text-green-300',
error: 'bg-red-50 border-red-400 text-red-800 dark:bg-red-900/30 dark:border-red-500 dark:text-red-300',
warning: 'bg-yellow-50 border-yellow-400 text-yellow-800 dark:bg-yellow-900/30 dark:border-yellow-500 dark:text-yellow-300',
info: 'bg-blue-50 border-blue-400 text-blue-800 dark:bg-blue-900/30 dark:border-blue-500 dark:text-blue-300',
};
const icons: Record<ToastType, string> = {
success: '✓',
error: '✕',
warning: '⚠',
info: 'ℹ',
};
export function Toast({ message, type = 'info', duration = 4000, onClose }: ToastProps) {
const [visible, setVisible] = useState(true);
useEffect(() => {
const timer = setTimeout(() => {
setVisible(false);
onClose?.();
}, duration);
return () => clearTimeout(timer);
}, [duration, onClose]);
if (!visible) return null;
return (
<div
role="alert"
className={`flex items-start gap-3 rounded-lg border px-4 py-3 text-sm shadow-md ${typeClasses[type]}`}
>
<span className="font-bold">{icons[type]}</span>
<span className="flex-1">{message}</span>
<button
onClick={() => { setVisible(false); onClose?.(); }}
className="ml-2 opacity-60 hover:opacity-100 transition-opacity"
aria-label="Dismiss"
>
✕
</button>
</div>
);
}
interface ToastItem {
id: string;
message: string;
type?: ToastType;
}
interface ToastContainerProps {
toasts: ToastItem[];
onRemove: (id: string) => void;
}
export function ToastContainer({ toasts, onRemove }: ToastContainerProps) {
return (
<div className="fixed bottom-4 right-4 z-50 flex flex-col gap-2 w-80">
{toasts.map((t) => (
<Toast key={t.id} message={t.message} type={t.type} onClose={() => onRemove(t.id)} />
))}
</div>
);
}
export default Toast;