-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathInfoBox.js
114 lines (91 loc) · 2.96 KB
/
InfoBox.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
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
import React, { useEffect, useRef, useState } from "react";
import classNames from "classnames";
import PropTypes from "prop-types";
import { CSSTransition } from "react-transition-group";
import InfoButton from "./InfoButton";
import ShareButtons from "./ShareButtons";
import "./InfoBox.scss";
const TRANSITION_SPEED = 500;
export default function InfoBox({ desc, displayMode, title, url }) {
const [closed, setClosed] = useState(true);
const [spaceKeyPressed, setSpaceKeyPressed] = useState(false);
const ref = useRef(null);
const handleKeyUp = (e) => {
const body = document.body;
if (e.keyCode === 9) {
body.classList.add("using-keyboard");
}
if (e.keyCode === 32) {
if (document.activeElement.classList.contains("leaflet-container")) {
setSpaceKeyPressed(true);
}
}
};
useEffect(() => {
if (spaceKeyPressed) {
setClosed(!closed);
}
}, [spaceKeyPressed]);
useEffect(() => {
const map = document.getElementsByClassName("leaflet-container")[0];
map.addEventListener("keyup", handleKeyUp);
return () => {
map.removeEventListener("keyup", handleKeyUp);
};
}, []);
useEffect(() => {
setSpaceKeyPressed(false);
const intro = document.querySelector(".leaflet-control-zoom");
const height = ref.current.clientHeight;
if (!height || !intro) return;
if (closed) {
intro.style.transition = "margin 300ms";
intro.style.marginBottom = "32px"; // .leaflet-bottom .leaflet-control in App.scss
} else {
intro.style.transition = "margin 300ms 200ms";
intro.style.marginBottom = `${height + 24}px`;
}
}, [closed]);
const handleMapClick = () => {
setClosed(true);
};
useEffect(() => {
const body = document.body;
body.classList.add("overflow-hidden");
const map = document.getElementsByClassName("leaflet-container")[0];
if (closed) {
document.querySelector(".leaflet-container").focus();
} else {
map.addEventListener("click", handleMapClick);
map.addEventListener("touchstart", handleMapClick);
map.addEventListener("touchmove", handleMapClick);
}
return () => {
map.removeEventListener("click", handleMapClick);
map.removeEventListener("touchstart", handleMapClick);
map.removeEventListener("touchmove", handleMapClick);
};
}, [closed]);
return (
<CSSTransition in={!closed} timeout={TRANSITION_SPEED}>
<div>
<InfoButton
displayMode={displayMode}
handleClick={() => setClosed(!closed)}
/>
<div ref={ref} className={classNames("info-wrapper", displayMode)}>
<div className="info-title">
<h1>{title}</h1>
</div>
<div className="info-description">{desc}</div>
<ShareButtons url={url} />
</div>
</div>
</CSSTransition>
);
}
InfoBox.propTypes = {
title: PropTypes.string.isRequired,
desc: PropTypes.node.isRequired,
url: PropTypes.string.isRequired,
};