|
1
|
import { |
|
2
|
CheckCircleSolid, |
|
3
|
ExclamationCircleSolid, |
|
4
|
InformationCircleSolid, |
|
5
|
Spinner, |
|
6
|
XCircleSolid, |
|
7
|
XMark, |
|
8
|
} from "@medusajs/icons" |
|
9
|
import * as React from "react" |
|
10
|
import { toast } from "sonner" |
|
11
|
|
|
12
|
import { IconButton } from "@/components/icon-button" |
|
13
|
import type { ToastAction, ToastVariant } from "@/types" |
|
14
|
import { clx } from "@/utils/clx" |
|
15
|
|
|
16
|
interface ToastComponentProps { |
|
17
|
id: string | number |
|
18
|
variant?: ToastVariant |
|
19
|
title: React.ReactNode |
|
20
|
description?: React.ReactNode |
|
21
|
action?: ToastAction |
|
22
|
icon?: React.ReactNode |
|
23
|
dismissable?: boolean |
|
24
|
} |
|
25
|
|
|
26
|
/** |
|
27
|
* This component is based on the [Sonner](https://sonner.emilkowal.ski/toast) toast library. |
|
28
|
*/ |
|
29
|
export const Toast = ({ |
|
30
|
/** |
|
31
|
* Optional ID of the toast. |
|
32
|
*/ |
|
33
|
id, |
|
34
|
/** |
|
35
|
* @ignore |
|
36
|
* |
|
37
|
* @privateRemarks |
|
38
|
* As the Toast component is created using |
|
39
|
* the `toast` utility functions, the variant is inferred |
|
40
|
* from the utility function. |
|
41
|
*/ |
|
42
|
variant = "info", |
|
43
|
/** |
|
44
|
* @ignore |
|
45
|
* |
|
46
|
* @privateRemarks |
|
47
|
* The `toast` utility functions accept this as a parameter. |
|
48
|
*/ |
|
49
|
title, |
|
50
|
/** |
|
51
|
* @ignore |
|
52
|
* |
|
53
|
* @privateRemarks |
|
54
|
* The `toast` utility functions accept this as a parameter. |
|
55
|
*/ |
|
56
|
icon: _icon, |
|
57
|
/** |
|
58
|
* The toast's text. |
|
59
|
*/ |
|
60
|
description, |
|
61
|
/** |
|
62
|
* The toast's action buttons. |
|
63
|
*/ |
|
64
|
action, |
|
65
|
/** |
|
66
|
* @ignore |
|
67
|
* |
|
68
|
* @privateRemarks |
|
69
|
* The `toast` utility functions accept this as a parameter. |
|
70
|
*/ |
|
71
|
dismissable = true, |
|
72
|
}: ToastComponentProps) => { |
|
73
|
let icon = _icon |
|
74
|
|
|
75
|
if (!_icon) { |
|
76
|
switch (variant) { |
|
77
|
case "success": |
|
78
|
icon = <CheckCircleSolid className="text-ui-tag-green-icon" /> |
|
79
|
break |
|
80
|
case "warning": |
|
81
|
icon = <ExclamationCircleSolid className="text-ui-tag-orange-icon" /> |
|
82
|
break |
|
83
|
case "error": |
|
84
|
icon = <XCircleSolid className="text-ui-tag-red-icon" /> |
|
85
|
break |
|
86
|
case "loading": |
|
87
|
icon = <Spinner className="text-ui-tag-blue-icon animate-spin" /> |
|
88
|
break |
|
89
|
case "info": |
|
90
|
icon = <InformationCircleSolid className="text-ui-fg-base" /> |
|
91
|
break |
|
92
|
default: |
|
93
|
break |
|
94
|
} |
|
95
|
} |
|
96
|
|
|
97
|
return ( |
|
98
|
<div className="shadow-elevation-flyout bg-ui-bg-component flex w-fit min-w-[360px] max-w-[440px] gap-x-3 overflow-hidden rounded-lg p-3"> |
|
99
|
<div |
|
100
|
className={clx("grid flex-1 items-center gap-x-2", { |
|
101
|
"grid-cols-[20px_1fr]": !!icon, |
|
102
|
"grid-cols-1": !icon, |
|
103
|
"items-start": !!description, |
|
104
|
})} |
|
105
|
> |
|
106
|
{!!icon && ( |
|
107
|
<span className="flex size-5 items-center justify-center" aria-hidden> |
|
108
|
{icon} |
|
109
|
</span> |
|
110
|
)} |
|
111
|
<div className="flex flex-col gap-y-3"> |
|
112
|
<div className="flex flex-col"> |
|
113
|
<span className="txt-compact-small-plus text-ui-fg-base"> |
|
114
|
{title} |
|
115
|
</span> |
|
116
|
{description && ( |
|
117
|
<span className="txt-small text-ui-fg-subtle text-pretty"> |
|
118
|
{description} |
|
119
|
</span> |
|
120
|
)} |
|
121
|
</div> |
|
122
|
{!!action && ( |
|
123
|
<button |
|
124
|
type="button" |
|
125
|
className={clx( |
|
126
|
"txt-compact-small-plus text-ui-fg-base bg-ui-bg-base flex w-fit items-center rounded-[4px] transition-colors", |
|
127
|
"focus-visible:shadow-borders-focus", |
|
128
|
"hover:text-ui-fg-subtle", |
|
129
|
"disabled:text-ui-fg-disabled", |
|
130
|
{ |
|
131
|
"text-ui-fg-error": action.variant === "destructive", |
|
132
|
} |
|
133
|
)} |
|
134
|
onClick={action.onClick} |
|
135
|
> |
|
136
|
{action.label} |
|
137
|
</button> |
|
138
|
)} |
|
139
|
</div> |
|
140
|
</div> |
|
141
|
{!!dismissable && ( |
|
142
|
<IconButton |
|
143
|
size="2xsmall" |
|
144
|
variant="transparent" |
|
145
|
type="button" |
|
146
|
onClick={() => toast.dismiss(id)} |
|
147
|
> |
|
148
|
<XMark /> |
|
149
|
</IconButton> |
|
150
|
)} |
|
151
|
</div> |
|
152
|
) |
|
153
|
} |