forked from toggle-corp/re-map
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MapBounds.tsx
55 lines (44 loc) · 1.22 KB
/
MapBounds.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
import { useContext, useEffect, useState } from 'react';
import { MapChildContext } from './context';
interface Props {
bounds?: [number, number, number, number];
padding: number | mapboxgl.PaddingOptions;
duration: number;
}
const MapBounds = (props: Props) => {
const { map, setBounds } = useContext(MapChildContext);
const {
padding,
duration,
bounds,
} = props;
const [initialDuration] = useState(duration);
// Handle change in bounds
useEffect(
() => {
if (!map || !bounds) {
return;
}
// NOTE: just to be safe here
if (bounds.length < 4) {
return;
}
const [fooLon, fooLat, barLon, barLat] = bounds;
setBounds(bounds, padding, initialDuration);
map.fitBounds(
[[fooLon, fooLat], [barLon, barLat]],
{
padding,
duration: initialDuration,
},
);
},
[map, bounds, padding, initialDuration, setBounds],
);
return null;
};
MapBounds.defaultProps = {
padding: 0,
duration: 200, // ms
};
export default MapBounds;