/
index.tsx
334 lines (310 loc) · 8.21 KB
/
index.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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
import React, { useEffect, useState, useRef } from 'react';
import ReactDom from 'react-dom';
import cx from 'classnames';
import CloseIcon from './CloseIcon';
import { FocusTrap } from './FocusTrap';
import { modalManager, useModalManager } from './modalManager';
import { isBrowser, blockNoScroll, unblockNoScroll } from './utils';
const classes = {
overlay: 'react-responsive-modal-overlay',
modal: 'react-responsive-modal-modal',
modalCenter: 'react-responsive-modal-modalCenter',
closeButton: 'react-responsive-modal-closeButton',
animationIn: 'react-responsive-modal-fadeIn',
animationOut: 'react-responsive-modal-fadeOut',
};
export interface ModalProps {
/**
* Control if the modal is open or not.
*/
open: boolean;
/**
* Should the dialog be centered.
*
* Default to false.
*/
center?: boolean;
/**
* Is the modal closable when user press esc key.
*
* Default to true.
*/
closeOnEsc?: boolean;
/**
* Is the modal closable when user click on overlay.
*
* Default to true.
*/
closeOnOverlayClick?: boolean;
/**
* Whether to block scrolling when dialog is open.
*
* Default to true.
*/
blockScroll?: boolean;
/**
* Show the close icon.
*
* Default to true.
*/
showCloseIcon?: boolean;
/**
* id attribute for the close icon button.
*/
closeIconId?: string;
/**
* Custom icon to render (svg, img, etc...).
*/
closeIcon?: React.ReactNode;
/**
* When the modal is open, trap focus within it.
*
* Default to true.
*/
focusTrapped?: boolean;
/**
* You can specify a container prop which should be of type `Element`.
* The portal will be rendered inside that element.
* The default behavior will create a div node and render it at the at the end of document.body.
*/
container?: Element | null;
/**
* An object containing classNames to style the modal.
*/
classNames?: {
overlay?: string;
modal?: string;
closeButton?: string;
closeIcon?: string;
animationIn?: string;
animationOut?: string;
};
/**
* An object containing the styles objects to style the modal.
*/
styles?: {
overlay?: React.CSSProperties;
modal?: React.CSSProperties;
closeButton?: React.CSSProperties;
closeIcon?: React.CSSProperties;
};
/**
* Animation duration in milliseconds.
*
* Default to 500.
*/
animationDuration?: number;
/**
* ARIA role for modal
*
* Default to 'dialog'.
*/
role?: string;
/**
* ARIA label for modal
*/
ariaLabelledby?: string;
/**
* ARIA description for modal
*/
ariaDescribedby?: string;
/**
* id attribute for modal
*/
modalId?: string;
/**
* Callback fired when the Modal is requested to be closed by a click on the overlay or when user press esc key.
*/
onClose: () => void;
/**
* Callback fired when the escape key is pressed.
*/
onEscKeyDown?: (event: KeyboardEvent) => void;
/**
* Callback fired when the overlay is clicked.
*/
onOverlayClick?: (
event: React.MouseEvent<HTMLDivElement, MouseEvent>
) => void;
/**
* Callback fired when the Modal has exited and the animation is finished.
*/
onAnimationEnd?: () => void;
children?: React.ReactNode;
}
export const Modal = ({
open,
center,
blockScroll = true,
closeOnEsc = true,
closeOnOverlayClick = true,
container,
showCloseIcon = true,
closeIconId,
closeIcon,
focusTrapped = true,
animationDuration = 500,
classNames,
styles,
role = 'dialog',
ariaDescribedby,
ariaLabelledby,
modalId,
onClose,
onEscKeyDown,
onOverlayClick,
onAnimationEnd,
children,
}: ModalProps) => {
const refModal = useRef<HTMLDivElement>(null);
const refShouldClose = useRef<boolean | null>(null);
const refContainer = useRef<HTMLDivElement | null>(null);
// Lazily create the ref instance
// https://reactjs.org/docs/hooks-faq.html#how-to-create-expensive-objects-lazily
if (refContainer.current === null && isBrowser) {
refContainer.current = document.createElement('div');
}
// The value should be false for srr, that way when the component is hydrated client side,
// it will match the server rendered content
const [showPortal, setShowPortal] = useState(false);
// Hook used to manage multiple modals opened at the same time
useModalManager(refModal, open, blockScroll);
const handleOpen = () => {
if (blockScroll) {
blockNoScroll();
}
if (
refContainer.current &&
!container &&
!document.body.contains(refContainer.current)
) {
document.body.appendChild(refContainer.current);
}
document.addEventListener('keydown', handleKeydown);
};
const handleClose = () => {
// Restore the scroll only if there is no modal on the screen
// We filter the modals that are not affecting the scroll
if (
blockScroll &&
modalManager.modals().filter((modal) => modal.blockScroll).length === 0
) {
unblockNoScroll();
}
if (
refContainer.current &&
!container &&
document.body.contains(refContainer.current)
) {
document.body.removeChild(refContainer.current);
}
document.removeEventListener('keydown', handleKeydown);
};
const handleKeydown = (event: KeyboardEvent) => {
// Only the last modal need to be escaped when pressing the esc key
if (event.keyCode !== 27 || !modalManager.isTopModal(refModal)) {
return;
}
onEscKeyDown?.(event);
if (closeOnEsc) {
onClose();
}
};
useEffect(() => {
return () => {
// When the component is unmounted directly we want to unblock the scroll
if (showPortal) {
handleClose();
}
};
}, [showPortal]);
useEffect(() => {
// If the open prop is changing, we need to open the modal
// This is also called on the first render if the open prop is true when the modal is created
if (open && !showPortal) {
setShowPortal(true);
handleOpen();
}
}, [open]);
const handleClickOverlay = (
event: React.MouseEvent<HTMLDivElement, MouseEvent>
) => {
if (refShouldClose.current === null) {
refShouldClose.current = true;
}
if (!refShouldClose.current) {
refShouldClose.current = null;
return;
}
onOverlayClick?.(event);
if (closeOnOverlayClick) {
onClose();
}
refShouldClose.current = null;
};
const handleModalEvent = () => {
refShouldClose.current = false;
};
const handleClickCloseIcon = () => {
onClose();
};
const handleAnimationEnd = () => {
if (!open) {
setShowPortal(false);
}
onAnimationEnd?.();
};
const containerModal = container || refContainer.current;
return showPortal && containerModal
? ReactDom.createPortal(
<div
style={{
animation: `${
open
? classNames?.animationIn ?? classes.animationIn
: classNames?.animationOut ?? classes.animationOut
} ${animationDuration}ms`,
...styles?.overlay,
}}
className={cx(classes.overlay, classNames?.overlay)}
onClick={handleClickOverlay}
onAnimationEnd={handleAnimationEnd}
data-testid="overlay"
>
<div
ref={refModal}
className={cx(
classes.modal,
center && classes.modalCenter,
classNames?.modal
)}
style={styles?.modal}
onMouseDown={handleModalEvent}
onMouseUp={handleModalEvent}
onClick={handleModalEvent}
id={modalId}
role={role}
aria-modal="true"
aria-labelledby={ariaLabelledby}
aria-describedby={ariaDescribedby}
data-testid="modal"
>
{focusTrapped && <FocusTrap container={refModal} />}
{children}
{showCloseIcon && (
<CloseIcon
classes={classes}
classNames={classNames}
styles={styles}
closeIcon={closeIcon}
onClickCloseIcon={handleClickCloseIcon}
id={closeIconId}
/>
)}
</div>
</div>,
containerModal
)
: null;
};
export default Modal;