forked from codesandbox/codesandbox-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathToast.tsx
More file actions
188 lines (175 loc) · 5.45 KB
/
Toast.tsx
File metadata and controls
188 lines (175 loc) · 5.45 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import * as React from 'react';
import { NotificationToast } from './Toasts';
import { NotificationStatus } from '../state';
import { ErrorIcon } from './icons/ErrorIcon';
import { SuccessIcon } from './icons/SuccessIcon';
import { WarningIcon } from './icons/WarningIcon';
import { InfoIcon } from './icons/InfoIcon';
import { StyledCrossIcon } from './elements';
const getColor = (colors: IColors, status: NotificationStatus) =>
colors[status];
const getIcon = (status: NotificationStatus) => {
switch (status) {
case NotificationStatus.ERROR:
return ErrorIcon;
case NotificationStatus.WARNING:
return WarningIcon;
case NotificationStatus.SUCCESS:
return SuccessIcon;
case NotificationStatus.NOTICE:
return InfoIcon;
default:
return InfoIcon;
}
};
export interface IColors {
[NotificationStatus.ERROR]: string;
[NotificationStatus.WARNING]: string;
[NotificationStatus.SUCCESS]: string;
[NotificationStatus.NOTICE]: string;
}
export type IButtonType = React.ComponentType<{
small?: boolean;
secondary?: boolean;
style?: React.CSSProperties;
onClick: (e: React.MouseEvent<HTMLButtonElement>) => void;
}>;
export type Props = {
toast: NotificationToast;
removeToast: (id: string) => void;
getRef?: React.LegacyRef<HTMLDivElement>;
colors: IColors;
Button: IButtonType;
};
export function Toast({ toast, removeToast, getRef, colors, Button }: Props) {
const Icon = getIcon(toast.notification.status);
return (
<div
ref={getRef}
style={{
display: 'flex',
backgroundColor: '#141618',
color: 'white',
borderRadius: 3,
position: 'relative',
width: 450,
overflow: 'hidden',
marginBottom: '0.5rem',
}}
>
<div
style={{
position: 'absolute',
left: 0,
top: 0,
bottom: 0,
backgroundColor: getColor(colors, toast.notification.status),
width: 4,
}}
/>
<div style={{ display: 'flex', padding: '.75rem 1rem', width: '100%' }}>
<div
style={{
color: getColor(colors, toast.notification.status),
width: 32,
fontSize: '1.25rem',
lineHeight:
toast.notification.message && toast.notification.title
? 1.4
: undefined,
verticalAlign: 'middle',
}}
>
<Icon />
</div>
<div style={{ width: '100%' }}>
<div style={{ display: 'flex', width: '100%' }}>
<div style={{ width: '100%' }}>
<div
style={{
fontSize: '.875rem',
fontWeight: 500,
color: 'white',
fontFamily: 'Poppins, sans-serif',
marginBottom:
toast.notification.title && toast.notification.message
? '.5rem'
: 0,
lineHeight: 1.6,
}}
>
{toast.notification.title
? toast.notification.title
: toast.notification.message}
</div>
{toast.notification.title && (
<div
style={{
color: 'rgba(255, 255, 255, 0.7)',
fontSize: '.875rem',
}}
>
{toast.notification.message}
</div>
)}
</div>
<div style={{ width: 24 }}>
<StyledCrossIcon onClick={() => removeToast(toast.id)} />
</div>
</div>
<div>
{toast.notification.actions && (
<div
style={{
display: 'flex',
fontSize: '.875rem',
}}
>
{toast.notification.actions.primary &&
toast.notification.actions.primary.map(primary => (
<Button
small
onClick={() => {
// By default we hide the notification on clicking primary buttons
if (primary.hideOnClick !== false) {
removeToast(toast.id);
}
primary.run();
}}
style={{
marginTop: '1rem',
marginRight: '0.75rem',
lineHeight: 1,
}}
>
{primary.label}
</Button>
))}
{toast.notification.actions.secondary &&
toast.notification.actions.secondary.map(secondary => (
<Button
secondary
small
onClick={() => {
if (secondary.hideOnClick) {
removeToast(toast.id);
}
secondary.run();
}}
style={{
marginTop: '1rem',
marginRight: '0.75rem',
lineHeight: 1,
}}
>
{secondary.label}
</Button>
))}
</div>
)}
</div>
</div>
</div>
</div>
);
}