-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathmodal.tsx
66 lines (62 loc) · 1.56 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
import {
Dispatch,
SetStateAction,
useCallback,
useEffect,
useRef,
} from "react";
import { AnimatePresence, motion } from "framer-motion";
export default function Modal({
children,
showModal,
setShowModal,
}: {
children: React.ReactNode;
showModal: boolean;
setShowModal: Dispatch<SetStateAction<boolean>>;
}) {
const modalRef = useRef(null);
const onKeyDown = useCallback(
(e: KeyboardEvent) => {
if (e.key === "Escape") {
setShowModal(false);
}
},
[setShowModal]
);
useEffect(() => {
document.addEventListener("keydown", onKeyDown);
return () => document.removeEventListener("keydown", onKeyDown);
}, [onKeyDown]);
return (
<AnimatePresence>
{showModal && (
<>
<motion.div
ref={modalRef}
key="modal"
className="fixed inset-0 z-40 min-h-screen flex items-center justify-center"
initial={{ scale: 0.95 }}
animate={{ scale: 1 }}
exit={{ scale: 0.95 }}
onMouseDown={(e) => {
if (modalRef.current === e.target) {
setShowModal(false);
}
}}
>
{children}
</motion.div>
<motion.div
key="backdrop"
className="fixed inset-0 z-30 bg-gray-100 bg-opacity-10 backdrop-blur"
initial={{ opacity: 0 }}
animate={{ opacity: 1 }}
exit={{ opacity: 0 }}
onClick={() => setShowModal(false)}
/>
</>
)}
</AnimatePresence>
);
}