-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathindex.js
59 lines (44 loc) · 1.23 KB
/
index.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
// @flow
import { PureComponent, createElement } from 'react';
import type MapboxMap from 'mapbox-gl/src/ui/map';
import type { CustomLayerInterface } from 'mapbox-gl/src/style/style_layer/custom_style_layer';
import MapContext from '../MapContext';
type Props = {
/** The id of an existing layer to insert the new layer before. */
before?: string,
/** Mapbox GL Custom Layer instance */
layer: CustomLayerInterface
};
/**
* Custom layers allow a user to render directly into the map's GL context
* using the map's camera.
*/
class CustomLayer extends PureComponent<Props> {
_id: string;
_map: MapboxMap;
static displayName = 'CustomLayer';
componentDidMount() {
const { layer, before } = this.props;
if (before && this._map.getLayer(before)) {
this._map.addLayer(layer, before);
} else {
this._map.addLayer(layer);
}
this._id = layer.id;
}
componentWillUnmount() {
if (!this._map || !this._map.getStyle() || !this._map.getLayer(this._id)) {
return;
}
this._map.removeLayer(this._id);
}
render() {
return createElement(MapContext.Consumer, {}, (map) => {
if (map) {
this._map = map;
}
return null;
});
}
}
export default CustomLayer;