Skip to content

Commit dbce21f

Browse files
committed
accessibility fixup
make focus go back to map when info box closes previously keyboard-only users would get stuck after opening infobox
1 parent c64a411 commit dbce21f

File tree

2 files changed

+46
-18
lines changed

2 files changed

+46
-18
lines changed

src/components/InfoBox.js

Lines changed: 41 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import "./InfoBox.scss";
1212
const TRANSITION_SPEED = 500;
1313

1414
export default function InfoBox({ desc, displayMode, title, url }) {
15-
const [hidden, setHidden] = useState(true);
15+
const [closed, setClosed] = useState(true);
1616

1717
const ref = useRef(null);
1818

@@ -22,45 +22,70 @@ export default function InfoBox({ desc, displayMode, title, url }) {
2222

2323
if (!height || !intro) return;
2424

25-
if (hidden) {
25+
if (closed) {
2626
intro.style.transition = "margin 300ms";
2727
intro.style.marginBottom = "80px"; // .leaflet-bottom .leaflet-control in App.scss
2828
} else {
2929
intro.style.transition = "margin 300ms 200ms";
3030
intro.style.marginBottom = `${height + 64}px`;
3131
}
32-
}, [hidden]);
32+
}, [closed]);
33+
34+
const setKeyboardUser = (e) => {
35+
const body = document.body;
36+
37+
if (e.keyCode === 9) {
38+
body.classList.add("using-keyboard");
39+
}
40+
};
41+
42+
useEffect(() => {
43+
const map = document.getElementsByClassName("leaflet-container")[0];
44+
45+
map.addEventListener("keyup", setKeyboardUser);
46+
47+
return () => {
48+
map.removeEventListener("keyup", setKeyboardUser);
49+
};
50+
}, []);
3351

3452
useEffect(() => {
3553
const body = document.body;
3654
body.classList.add("overflow-hidden");
3755

3856
const map = document.getElementsByClassName("leaflet-container")[0];
3957

40-
const handleClick = () => {
41-
setHidden(!hidden);
58+
const handleMapClick = () => {
59+
setClosed(!closed);
4260
};
4361

44-
if (hidden) {
45-
map.removeEventListener("click", handleClick);
46-
map.removeEventListener("touchstart", handleClick);
47-
map.removeEventListener("touchmove", handleClick);
48-
return;
62+
if (closed) {
63+
document.querySelector(".leaflet-container").focus();
64+
map.removeEventListener("click", handleMapClick);
65+
map.removeEventListener("touchstart", handleMapClick);
66+
map.removeEventListener("touchmove", handleMapClick);
67+
return undefined;
4968
}
5069

5170
window.setTimeout(() => {
52-
map.addEventListener("click", handleClick);
53-
map.addEventListener("touchstart", handleClick);
54-
map.addEventListener("touchmove", handleClick);
71+
map.addEventListener("click", handleMapClick);
72+
map.addEventListener("touchstart", handleMapClick);
73+
map.addEventListener("touchmove", handleMapClick);
5574
}, 100);
56-
}, [hidden]);
75+
76+
return () => {
77+
map.removeEventListener("click", handleMapClick);
78+
map.removeEventListener("touchstart", handleMapClick);
79+
map.removeEventListener("touchmove", handleMapClick);
80+
};
81+
}, [closed]);
5782

5883
return (
59-
<CSSTransition in={!hidden} timeout={TRANSITION_SPEED}>
84+
<CSSTransition in={!closed} timeout={TRANSITION_SPEED}>
6085
<div>
6186
<InfoButton
6287
displayMode={displayMode}
63-
handleClick={() => setHidden(!hidden)}
88+
handleClick={() => setClosed(!closed)}
6489
/>
6590
<div ref={ref} className={classNames("info-wrapper", displayMode)}>
6691
<div className="info-title">

src/components/InfoButton.scss

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
border: 0;
77
outline: 0;
88
width: 340px;
9-
position: absolute;
9+
position: fixed;
1010
left: 0;
1111
right: 0;
1212
bottom: -20px; // this svg has extra padding in the image
@@ -15,13 +15,16 @@
1515
text-align: center;
1616
z-index: $z-index-map;
1717

18+
1819
@media (min-width: $mobile_breakpoint) {
1920
bottom: -40px; // this svg has extra padding in the image
2021

2122
}
2223
}
2324

24-
25+
.using-keyboard .info-button:focus {
26+
border: 1px solid blue;
27+
}
2528

2629
.info-button:hover {
2730
cursor: pointer;

0 commit comments

Comments
 (0)