-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathindex.js
148 lines (118 loc) · 3.05 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
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
142
143
144
145
146
147
148
// @flow
import { createPortal } from 'react-dom';
import { PureComponent, createElement } from 'react';
import type MapboxMap from 'mapbox-gl/src/ui/map';
import type MapboxPopup from 'mapbox-gl/src/ui/popup';
import type MapboxLngLatBoundsLike from 'mapbox-gl/src/geo/lng_lat_bounds';
import MapContext from '../MapContext';
import mapboxgl from '../../utils/mapbox-gl';
type Props = {
/** Popup content. */
children: React$Node,
/** The longitude of the center of the popup. */
longitude: number,
/** The latitude of the center of the popup. */
latitude: number,
/*
* If true, a close button will appear
* in the top right corner of the popup.
*/
closeButton?: boolean,
/** If true, the popup will closed when the map is clicked. */
closeOnClick?: boolean,
/** The onClose callback is fired when the popup closed. */
onClose?: Function,
/*
* A string indicating the part of the Popup
* that should be positioned closest to the coordinate.
* */
anchor?:
| 'top'
| 'bottom'
| 'left'
| 'right'
| 'top-left'
| 'top-right'
| 'bottom-left'
| 'bottom-right',
/**
* The offset in pixels as a `PointLike` object to apply
* relative to the element's center. Negatives indicate left and up.
*/
offset?: MapboxLngLatBoundsLike,
/** The className of the popup */
className?: string,
/** A string that sets the CSS property of the popup's maximum width. */
maxWidth?: string
};
class Popup extends PureComponent<Props> {
_map: MapboxMap;
_el: HTMLDivElement;
_popup: MapboxPopup;
static displayName = 'Popup';
static defaultProps = {
closeButton: true,
closeOnClick: true,
onClose: undefined,
anchor: undefined,
offset: undefined,
className: undefined,
maxWidth: '240px'
};
constructor(props: Props) {
super(props);
this._el = document.createElement('div');
}
componentDidMount() {
const {
longitude,
latitude,
offset,
closeButton,
closeOnClick,
onClose,
anchor,
className,
maxWidth
} = this.props;
this._popup = new mapboxgl.Popup({
offset,
closeButton,
closeOnClick,
anchor,
className,
maxWidth
});
this._popup.setDOMContent(this._el);
this._popup.setLngLat([longitude, latitude]).addTo(this._map);
if (onClose) {
this._popup.on('close', onClose);
}
}
componentDidUpdate(prevProps: Props) {
const positionChanged =
prevProps.latitude !== this.props.latitude ||
prevProps.longitude !== this.props.longitude;
if (positionChanged) {
this._popup.setLngLat([this.props.longitude, this.props.latitude]);
}
}
componentWillUnmount() {
if (!this._map || !this._map.getStyle()) {
return;
}
this._popup.remove();
}
getPopup() {
return this._popup;
}
render() {
return createElement(MapContext.Consumer, {}, (map) => {
if (map) {
this._map = map;
}
return createPortal(this.props.children, this._el);
});
}
}
export default Popup;