-
Notifications
You must be signed in to change notification settings - Fork 784
/
Copy pathindex.tsx
80 lines (74 loc) · 2.15 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
"use client";
import {
Dispatch,
SetStateAction,
useCallback,
useEffect,
useRef,
} from "react";
import FocusTrap from "focus-trap-react";
import { AnimatePresence, motion } from "framer-motion";
import Leaflet from "./leaflet";
import useWindowSize from "@/lib/hooks/use-window-size";
export default function Modal({
children,
showModal,
setShowModal,
}: {
children: React.ReactNode;
showModal: boolean;
setShowModal: Dispatch<SetStateAction<boolean>>;
}) {
const desktopModalRef = 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]);
const { isMobile, isDesktop } = useWindowSize();
return (
<AnimatePresence>
{showModal && (
<>
{isMobile && <Leaflet setShow={setShowModal}>{children}</Leaflet>}
{isDesktop && (
<>
<FocusTrap focusTrapOptions={{ initialFocus: false }}>
<motion.div
ref={desktopModalRef}
key="desktop-modal"
className="fixed inset-0 z-40 hidden min-h-screen items-center justify-center md:flex"
initial={{ scale: 0.95 }}
animate={{ scale: 1 }}
exit={{ scale: 0.95 }}
onMouseDown={(e) => {
if (desktopModalRef.current === e.target) {
setShowModal(false);
}
}}
>
{children}
</motion.div>
</FocusTrap>
<motion.div
key="desktop-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>
);
}