-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathModal.js
53 lines (44 loc) · 1.27 KB
/
Modal.js
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
import React, { useRef, useState, useEffect, memo } from "react";
import { createPortal } from "react-dom";
import "./Modal.css";
const Modal = ({ isOpen, onClose, height, children }) => {
if (!isOpen) return null;
const [_isOpen, setIsOpen] = useState(true);
const modalEl = useRef(null);
const handleCoverClick = e => {
if (e.target.hasAttribute("modal")) {
setIsOpen(false);
}
};
useEffect(() => {
const handleAnimationEnd = event => {
if (!_isOpen) {
onClose();
}
};
modalEl.current.addEventListener("animationend", handleAnimationEnd);
return () =>
modalEl.current.removeEventListener("animationend", handleAnimationEnd);
}, [_isOpen]);
return createPortal(
<>
{console.log("modalEl.current ", modalEl.current)}
<div
className={`ModalCover ${isOpen ? "show" : "hide"}`}
onClick={handleCoverClick}
modal="true"
/>
<div
className={`ModalContainer ${_isOpen ? "slide-up" : "slide-down"}`}
ref={modalEl}
>
{children}
</div>
</>,
document.body
);
};
const shouldMemo = (prevProps, nextProps) =>
prevProps.isOpen === nextProps.isOpen &&
prevProps.height === nextProps.height;
export default memo(Modal, shouldMemo);