-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathindex.tsx
More file actions
72 lines (60 loc) · 1.74 KB
/
Copy pathindex.tsx
File metadata and controls
72 lines (60 loc) · 1.74 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
import { useEffect } from 'react';
import { createPortal } from 'react-dom';
import { useWindowWidth } from '~/hooks/utility';
import Icon from '../Icon';
import { StyledBackdrop, StyledCloseButton, StyledModal } from './styles';
interface KeyboardEvent {
key: string;
}
interface IModalProps {
handleClose: () => void | React.ReactEventHandler | React.ChangeEventHandler;
children: React.ReactNode;
customWidth?: string;
flexLayout?: boolean;
}
const modalRoot = document.getElementById('modal-root') as Element;
const Modal: React.FC<IModalProps> = ({
handleClose,
children,
customWidth,
flexLayout,
}) => {
const { isMobile } = useWindowWidth();
// Disabling page scroll when modal is open
useEffect(() => {
document.body.classList.add('no-scroll');
return () => document.body.classList.remove('no-scroll');
}, []);
const handleBackdropClick: React.ReactEventHandler = (event) => {
if (event.target !== event.currentTarget) {
return;
}
handleClose();
};
useEffect(() => {
const handleCloseOnEsc = (event: KeyboardEvent) => {
if (event.key !== 'Escape') {
return;
}
handleClose();
};
window.addEventListener('keydown', handleCloseOnEsc);
return () => {
window.removeEventListener('keydown', handleCloseOnEsc);
};
}, [handleClose]);
return createPortal(
<StyledBackdrop onMouseDown={handleBackdropClick}>
<StyledModal $customWidth={customWidth} $flexLayout={flexLayout}>
{isMobile && (
<StyledCloseButton onClick={handleClose}>
<Icon name="CloseIcon" size="24px" />
</StyledCloseButton>
)}
{children}
</StyledModal>
</StyledBackdrop>,
modalRoot,
);
};
export default Modal;