-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathMapLibreMap.tsx
More file actions
141 lines (126 loc) · 3.59 KB
/
MapLibreMap.tsx
File metadata and controls
141 lines (126 loc) · 3.59 KB
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import React, { useRef, useEffect, useContext, FC, RefObject } from 'react';
import MapContext, { MapContextType } from '../../contexts/MapContext';
import MapLibreGlWrapper from './lib/MapLibreGlWrapper';
import { MapOptions as MapOptionsType, Map } from 'maplibre-gl';
import 'maplibre-gl/dist/maplibre-gl.css';
export type MapLibreMapProps = {
/**
* Id of the MapLibreGl(Wrapper) instance in mapContext
*/
mapId?: string;
/**
* Config object that is passed to the MapLibreGl constructor as first parameter.
* See https://maplibre.org/maplibre-gl-js-docs/api/map/ for a formal documentation of al
* available properties.
*/
options?: Partial<MapOptionsType>;
/**
* css style definition passed to the map container DOM element
*/
style?: object;
};
const defaultProps: MapLibreMapProps = {
mapId: undefined,
options: {
center: { lng: 8.607, lat: 53.1409349 },
zoom: 11,
container: '',
style: {
version: 8,
name: 'blank',
center: [0, 0],
zoom: 0,
sources: {},
sprite: 'https://wms.wheregroup.com/tileserver/sprites/osm-bright',
glyphs: 'https://wms.wheregroup.com/tileserver/fonts/{fontstack}/{range}.pbf',
layers: [
{
id: '_background',
type: 'background',
paint: {
'background-color': 'rgba(0,0,0,0)',
},
},
],
},
},
};
/**
* Creates a MapLibreGlWrapper instance and registers it in MapContext
* after the MapLibre-gl load event has fired.
*
* MapLibreMap returns the html node that will be used by MapLibre-gl to render the map.
* This Component must be kept unaware of any related components that interact with the MapLibre-gl
* instance.
*
* @category Map components
*/
const MapLibreMap: FC<MapLibreMapProps> = (props: MapLibreMapProps) => {
const mapRef = useRef<MapLibreGlWrapper>();
const mapContainer = useRef<HTMLDivElement>();
const mapContext = useContext<MapContextType>(MapContext);
const mapIdRef = useRef(props.mapId);
const initializedRef = useRef(false);
const currentStyle = useRef(props.options?.style);
useEffect(() => {
const mapId = mapIdRef.current;
return () => {
initializedRef.current = false;
mapContext.removeMap(mapId);
if (mapRef.current) {
mapRef.current.map?.remove?.();
mapRef.current.cancelled = true;
mapRef.current = undefined;
}
};
}, []);
useEffect(() => {
if (initializedRef.current) return;
if (mapContainer.current) {
initializedRef.current = true;
mapRef.current = new MapLibreGlWrapper({
mapOptions: {
style: '',
...props.options,
...(props?.options?.style ? {} : { style: defaultProps?.options?.style }),
container: mapContainer.current,
},
onReady: (map: Map, wrapper: MapLibreGlWrapper) => {
map.once('load', () => {
if (!wrapper?.cancelled) {
// add maplibre instance to window for debugging purposes
window['_map'] = map;
if (props.mapId) {
mapContext.registerMap(props.mapId, wrapper);
} else {
mapContext.setMap(wrapper);
}
} else {
map.remove();
}
});
},
});
}
}, [props.options, props.mapId]);
useEffect(() => {
if (
mapRef.current?.map &&
props?.options?.style &&
currentStyle.current !== props.options.style
) {
currentStyle.current = props.options.style;
mapRef.current.map.setStyle(props.options.style);
}
}, [props?.options?.style]);
return (
<div
ref={mapContainer as RefObject<HTMLDivElement>}
className="mapContainer"
style={props.style}
data-testid={props.mapId ? `map-${props.mapId}` : 'map'}
/>
);
};
MapLibreMap.defaultProps = defaultProps;
export default MapLibreMap;