-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathmap.tsx
More file actions
139 lines (120 loc) · 3.64 KB
/
map.tsx
File metadata and controls
139 lines (120 loc) · 3.64 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
import * as React from 'react';
import {useState, useRef, useEffect, useContext, useMemo, useImperativeHandle} from 'react';
import {MountedMapsContext} from './use-map';
import Mapbox, {MapboxProps} from '../mapbox/mapbox';
import createRef, {MapRef} from '../mapbox/create-ref';
import type {CSSProperties} from 'react';
import useIsomorphicLayoutEffect from '../utils/use-isomorphic-layout-effect';
import setGlobals, {GlobalSettings} from '../utils/set-globals';
import type {MapLib, MapOptions} from '../types/lib';
export type MapContextValue = {
mapLib: MapLib;
map: MapRef;
};
export const MapContext = React.createContext<MapContextValue>(null);
type MapInitOptions = Omit<
MapOptions,
'style' | 'container' | 'bounds' | 'fitBoundsOptions' | 'center'
>;
export type MapProps = MapInitOptions &
MapboxProps &
GlobalSettings & {
mapLib?: MapLib | Promise<MapLib>;
reuseMaps?: boolean;
/** Map container id */
id?: string;
/** Map container CSS style */
style?: CSSProperties;
/** Map child container CSS style */
childContainerStyle?: CSSProperties;
children?: any;
};
function _Map(props: MapProps, ref: React.Ref<MapRef>) {
const mountedMapsContext = useContext(MountedMapsContext);
const [mapInstance, setMapInstance] = useState<Mapbox>(null);
const containerRef = useRef();
const {current: contextValue} = useRef<MapContextValue>({mapLib: null, map: null});
useEffect(() => {
const mapLib = props.mapLib;
let isMounted = true;
let mapbox: Mapbox;
Promise.resolve(mapLib || import('mapbox-gl'))
.then((module: MapLib | {default: MapLib}) => {
if (!isMounted) {
return;
}
if (!module) {
throw new Error('Invalid mapLib');
}
const mapboxgl = 'Map' in module ? module : module.default;
if (!mapboxgl.Map) {
throw new Error('Invalid mapLib');
}
setGlobals(mapboxgl, props);
if (props.reuseMaps) {
mapbox = Mapbox.reuse(props, containerRef.current);
}
if (!mapbox) {
mapbox = new Mapbox(mapboxgl.Map, props, containerRef.current);
}
contextValue.map = createRef(mapbox);
contextValue.mapLib = mapboxgl;
setMapInstance(mapbox);
mountedMapsContext?.onMapMount(contextValue.map, props.id);
})
.catch(error => {
const {onError} = props;
if (onError) {
onError({
type: 'error',
target: null,
error
});
} else {
console.error(error); // eslint-disable-line
}
});
return () => {
isMounted = false;
if (mapbox) {
mountedMapsContext?.onMapUnmount(props.id);
if (props.reuseMaps) {
mapbox.recycle();
} else {
mapbox.destroy();
}
}
};
}, []);
useIsomorphicLayoutEffect(() => {
if (mapInstance) {
mapInstance.setProps(props);
}
});
useImperativeHandle(ref, () => contextValue.map, [mapInstance]);
const style: CSSProperties = useMemo(
() => ({
position: 'relative',
width: '100%',
height: '100%',
...props.style
}),
[props.style]
);
const CHILD_CONTAINER_STYLE = {
height: '100%',
...props.childContainerStyle
};
return (
<div id={props.id} ref={containerRef} style={style}>
{mapInstance && props.children && (
<MapContext.Provider value={contextValue}>
<div mapboxgl-children="" style={CHILD_CONTAINER_STYLE}>
{props.children}
</div>
</MapContext.Provider>
)}
</div>
);
}
export const Map = React.forwardRef(_Map);