|
1
|
"use client" |
|
2
|
|
|
3
|
import { clx } from "@/utils/clx" |
|
4
|
import { |
|
5
|
CheckCircleSolid, |
|
6
|
ExclamationCircleSolid, |
|
7
|
InformationCircleSolid, |
|
8
|
XCircleSolid, |
|
9
|
XMarkMini, |
|
10
|
} from "@medusajs/icons" |
|
11
|
import * as React from "react" |
|
12
|
|
|
13
|
import { IconButton } from "@/components/icon-button" |
|
14
|
|
|
15
|
interface AlertProps extends React.ComponentPropsWithoutRef<"div"> { |
|
16
|
variant?: "error" | "success" | "warning" | "info" |
|
17
|
dismissible?: boolean |
|
18
|
} |
|
19
|
|
|
20
|
/** |
|
21
|
* This component is based on the div element and supports all of its props |
|
22
|
* |
|
23
|
* @excludeExternal |
|
24
|
*/ |
|
25
|
export const Alert = React.forwardRef<HTMLDivElement, AlertProps>( |
|
26
|
( |
|
27
|
{ |
|
28
|
/** |
|
29
|
* The variant of the alert |
|
30
|
*/ |
|
31
|
variant = "info", |
|
32
|
/** |
|
33
|
* Whether the alert is dismissible |
|
34
|
*/ |
|
35
|
dismissible = false, |
|
36
|
className, |
|
37
|
children, |
|
38
|
...props |
|
39
|
}: AlertProps, |
|
40
|
ref |
|
41
|
) => { |
|
42
|
const [dismissed, setDismissed] = React.useState(false) |
|
43
|
|
|
44
|
const Icon = { |
|
45
|
info: InformationCircleSolid, |
|
46
|
error: XCircleSolid, |
|
47
|
success: CheckCircleSolid, |
|
48
|
warning: ExclamationCircleSolid, |
|
49
|
}[variant] |
|
50
|
|
|
51
|
const handleDismiss = () => { |
|
52
|
setDismissed(true) |
|
53
|
} |
|
54
|
|
|
55
|
if (dismissed) { |
|
56
|
return null |
|
57
|
} |
|
58
|
|
|
59
|
return ( |
|
60
|
<div |
|
61
|
ref={ref} |
|
62
|
className={clx( |
|
63
|
"bg-ui-bg-subtle txt-compact-small grid items-start gap-x-2 text-pretty rounded-lg border p-3", |
|
64
|
{ |
|
65
|
"grid-cols-[20px_1fr]": !dismissible, |
|
66
|
"grid-cols-[20px_1fr_20px]": dismissible, |
|
67
|
}, |
|
68
|
className |
|
69
|
)} |
|
70
|
{...props} |
|
71
|
> |
|
72
|
<Icon |
|
73
|
className={clx({ |
|
74
|
"text-ui-tag-red-icon": variant === "error", |
|
75
|
"text-ui-tag-green-icon": variant === "success", |
|
76
|
"text-ui-tag-orange-icon": variant === "warning", |
|
77
|
"text-ui-tag-neutral-icon": variant === "info", |
|
78
|
})} |
|
79
|
/> |
|
80
|
<div className="w-full">{children}</div> |
|
81
|
{dismissible && ( |
|
82
|
<IconButton |
|
83
|
size="2xsmall" |
|
84
|
variant="transparent" |
|
85
|
type="button" |
|
86
|
onClick={handleDismiss} |
|
87
|
> |
|
88
|
<XMarkMini className="text-ui-fg-muted" /> |
|
89
|
</IconButton> |
|
90
|
)} |
|
91
|
</div> |
|
92
|
) |
|
93
|
} |
|
94
|
) |