forked from PinSpace-Org/GistPin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAlert.tsx
More file actions
84 lines (75 loc) · 2.13 KB
/
Copy pathAlert.tsx
File metadata and controls
84 lines (75 loc) · 2.13 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
81
82
83
84
'use client';
import { ReactNode, useState } from 'react';
type AlertType = 'info' | 'success' | 'warning' | 'error';
interface AlertProps {
type?: AlertType;
title?: string;
children: ReactNode;
dismissible?: boolean;
icon?: ReactNode;
action?: ReactNode;
onDismiss?: () => void;
}
const styles: Record<AlertType, { container: string; icon: string; defaultIcon: string }> = {
info: {
container: 'bg-blue-50 border-blue-200 text-blue-800 dark:bg-blue-900/20 dark:border-blue-800 dark:text-blue-300',
icon: 'text-blue-500',
defaultIcon: 'ℹ',
},
success: {
container: 'bg-green-50 border-green-200 text-green-800 dark:bg-green-900/20 dark:border-green-800 dark:text-green-300',
icon: 'text-green-500',
defaultIcon: '✓',
},
warning: {
container: 'bg-yellow-50 border-yellow-200 text-yellow-800 dark:bg-yellow-900/20 dark:border-yellow-800 dark:text-yellow-300',
icon: 'text-yellow-500',
defaultIcon: '⚠',
},
error: {
container: 'bg-red-50 border-red-200 text-red-800 dark:bg-red-900/20 dark:border-red-800 dark:text-red-300',
icon: 'text-red-500',
defaultIcon: '✕',
},
};
export default function Alert({
type = 'info',
title,
children,
dismissible = false,
icon,
action,
onDismiss,
}: AlertProps) {
const [dismissed, setDismissed] = useState(false);
if (dismissed) return null;
const s = styles[type];
function handleDismiss() {
setDismissed(true);
onDismiss?.();
}
return (
<div
role="alert"
className={`flex gap-3 rounded-lg border p-4 text-sm ${s.container}`}
>
<span className={`mt-0.5 shrink-0 font-bold ${s.icon}`}>
{icon ?? s.defaultIcon}
</span>
<div className="flex-1 min-w-0">
{title && <p className="font-semibold mb-0.5">{title}</p>}
<div>{children}</div>
{action && <div className="mt-2">{action}</div>}
</div>
{dismissible && (
<button
onClick={handleDismiss}
aria-label="Dismiss"
className="shrink-0 opacity-60 hover:opacity-100 transition-opacity"
>
✕
</button>
)}
</div>
);
}