-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathindex.js
73 lines (57 loc) · 1.79 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// @flow
import { PureComponent, createElement } from 'react';
import type MapboxMap from 'mapbox-gl/src/ui/map';
import type MapboxAttributionControl from 'mapbox-gl/src/ui/control/attribution_control';
import MapContext from '../MapContext';
import mapboxgl from '../../utils/mapbox-gl';
type Props = {
/**
* If `true` force a compact attribution that shows the full
* attribution on mouse hover, or if false force the full attribution
* control. The default is a responsive attribution that collapses when
* the map is less than 640 pixels wide.
*/
compact: boolean,
/* String or strings to show in addition to any other attributions. */
customAttribution: string | Array<string>,
/* A string representing the position of the control on the map. */
position?: 'top-left' | 'top-right' | 'bottom-left' | 'bottom-right'
};
/**
* An `AttributionControl` control presents the map's attribution information.
*/
class AttributionControl extends PureComponent<Props> {
_map: MapboxMap;
_control: MapboxAttributionControl;
static defaultProps = {
position: 'bottom-right'
};
componentDidMount() {
const map: MapboxMap = this._map;
const { compact, customAttribution, position } = this.props;
const control: MapboxAttributionControl = new mapboxgl.AttributionControl({
compact,
customAttribution
});
map.addControl(control, position);
this._control = control;
}
componentWillUnmount() {
if (!this._map || !this._map.getStyle()) {
return;
}
this._map.removeControl(this._control);
}
getControl() {
return this._control;
}
render() {
return createElement(MapContext.Consumer, {}, (map) => {
if (map) {
this._map = map;
}
return null;
});
}
}
export default AttributionControl;