-
Notifications
You must be signed in to change notification settings - Fork 9
/
Modal.tsx
264 lines (256 loc) · 7.46 KB
/
Modal.tsx
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
import { type ReactElement } from "react";
import classnames from "classnames";
import Heading from "../Heading/Heading";
import Box from "../Box/Box";
import FocusTrap from "./FocusTrap";
import StopScroll from "./StopScroll";
import Layer from "./Layer";
import styles from "./Modal.module.css";
import { useTheme } from "../ThemeProvider/ThemeProvider";
import IconButton from "../IconButton/IconButton";
function XIcon({ color = "#000" }: { color?: string }) {
return (
<svg xmlns="http://www.w3.org/2000/svg" width="12" height="12" fill={color}>
<path
fill="inherit"
d="M11.25.758a.83.83 0 0 0-1.175 0L6 4.825 1.925.75A.83.83 0 1 0 .75 1.925L4.825 6 .75 10.075a.83.83 0 1 0 1.175 1.175L6 7.175l4.075 4.075a.83.83 0 1 0 1.175-1.175L7.175 6l4.075-4.075a.835.835 0 0 0 0-1.167Z"
/>
</svg>
);
}
function XIconCambio({ className }: { className?: string }) {
return (
<svg
className={className}
fill="currentColor"
focusable="false"
aria-hidden="true"
viewBox="0 0 24 24"
>
<path d="M19 6.41 17.59 5 12 10.59 6.41 5 5 6.41 10.59 12 5 17.59 6.41 19 12 13.41 17.59 19 19 17.59 13.41 12z"></path>
</svg>
);
}
// Note: Only sm + lg size currently. design thinks there should only be two sizes.
// If there IS a md size at some point, we should use the "size" const.
const sizeWidth = {
sm: 400,
lg: 600,
} as const;
/**
* [Modal](https://cambly-syntax.vercel.app/?path=/docs/components-modal--docs) is a dialog that appears on top of the main content and locks user interaction within the modal.
*
```
const [showModal, setShowModal] = useState(false)
return (
<>
{showModal && <Modal
header="header text"
onDismiss={() => setShowModal(false)}
footer={
<>
<Button
text="Cancel"
color="secondary"
onClick={() => {}}
/>
<Button
text="Confirm"
onClick={() => {}}
/>
</>
}
>
<Typography>
Body text goes here!
</Typography>
</Modal> }
</>
)
```
*/
export default function Modal({
header,
children,
image,
onDismiss,
footer,
accessibilityCloseLabel = "close modal",
size = "sm",
zIndex = 1,
"data-testid": dataTestId,
}: {
/**
* The modal's main content. Should typically take in `Typography`'d text.
*/
children: JSX.Element;
/**
* The header inside a modal as a string.
*/
header: string;
/**
* What the X button (or clicking out of the modal area) does.
* Typically used for closing the Modal
*/
onDismiss: () => void;
/**
* The optional image rendered above the Modal.
* Image size should be 400w x 200h.
* If images aren't that sized, should ask design to give another image.
*/
image?: JSX.Element;
/**
* The footer for the bottom area of the Modal. Typically used for rendering buttons.
* * Use Syntax `Button` and pass it into footer.
* * If one button, just pass it in. If two, wrap in a React fragment (`<> </>`)
* * If two(2) buttons, put primary button _second_.
*
<>
<Button
text="Cancel"
color="secondary"
onClick={() => {}}
/>
<Button
text="Confirm"
onClick={() => {}}
/>
</>
*/
footer?: JSX.Element;
/**
* The accessibilty text on the close button.
* (Sets the aria-label of the button)
*
*
* @defaultValue close modal
*/
accessibilityCloseLabel?: string;
/**
* The size of the card
*
* Classic:
* * `sm`: 400px (Classic only)
* * `lg`: 600px
*
* Cambio:
* * `lg`: 600px
*
*
* @defaultValue sm
*/
size?: keyof typeof sizeWidth;
/**
* The z-index for the modal.
* Typically used if there are other things on the page with higher z-index and you need this overlayed on top.
*
* @defaultValue 0
*/
zIndex?: number;
/**
* Test id for the modal
*/
"data-testid"?: string;
}): ReactElement {
const { themeName } = useTheme();
return (
<Layer zIndex={zIndex}>
<StopScroll>
<FocusTrap>
<div
className={classnames(
styles.backdrop,
themeName === "classic"
? styles.backdropClassic
: styles.backdropCambio,
)}
role="presentation"
onClick={(e) => e.target === e.currentTarget && onDismiss()}
onKeyDown={(e) => e.key === "Escape" && onDismiss()}
>
<Box
data-testid={dataTestId}
backgroundColor="white"
rounding={themeName === "classic" ? "xl" : "md"}
display="flex"
direction="column"
marginStart={4}
marginEnd={4}
minWidth={240}
maxWidth={sizeWidth[themeName === "classic" ? size : "lg"]}
width="100%"
dangerouslySetInlineStyle={{ __style: { overflow: "hidden" } }}
>
<Box position="relative">
{themeName === "classic" ? (
<button
aria-label={accessibilityCloseLabel}
type="button"
className={classnames(
styles.closeButton,
styles.closeButtonClassic,
{
[styles.closeButtonWithImage]: !!image,
},
)}
onClick={onDismiss}
>
<XIcon color={image ? "#fff" : "#000"} />
</button>
) : (
<Box
position="absolute"
dangerouslySetInlineStyle={{
__style: { top: 4, right: 4 },
}}
>
<IconButton
accessibilityLabel={accessibilityCloseLabel}
color={image ? "primary" : "tertiary"}
on={image ? "darkBackground" : "lightBackground"}
size="sm"
icon={XIconCambio}
/>
</Box>
)}
</Box>
{image && <Box maxHeight={200}>{image}</Box>}
<Box
display="flex"
gap={themeName === "classic" ? 3 : 4}
direction="column"
padding={themeName === "classic" ? 9 : 6}
>
<Heading
as="h1"
size={themeName === "classic" ? 500 : 600}
fontStyle={themeName === "classic" ? "sans-serif" : "serif"}
>
{header}
</Heading>
<Box marginBottom={themeName === "classic" ? 4 : 0}>
{children}
</Box>
{footer && (
<Box
display="flex"
direction="column"
gap={3}
marginTop={themeName === "classic" ? 0 : 2}
smDirection="row"
smJustifyContent="end"
lgDirection="row"
lgJustifyContent="end"
>
{footer}
</Box>
)}
</Box>
</Box>
</div>
</FocusTrap>
</StopScroll>
</Layer>
);
}
Modal.displayName = "Modal";