@vinctus/react-maplibre 1.0.2 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/Marker.js +48 -54
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +1 -1
- package/dist/types.d.ts +0 -1
- package/dist/types.js +0 -1
package/dist/Marker.js
CHANGED
|
@@ -1,69 +1,63 @@
|
|
|
1
|
-
import { useEffect, useState, useContext } from 'react';
|
|
1
|
+
import { useEffect, useMemo, useRef, useState, useContext, } from 'react';
|
|
2
2
|
import maplibre from 'maplibre-gl';
|
|
3
|
-
import
|
|
3
|
+
import { createPortal } from 'react-dom';
|
|
4
4
|
import ReactDOMServer from 'react-dom/server';
|
|
5
5
|
import { MapContext } from './Map';
|
|
6
6
|
export const Marker = ({ children, longitude, latitude, className, anchor, color, popup, popupClassName, closeOnClick = false, closeButton = false, }) => {
|
|
7
7
|
const { map, mapLoaded } = useContext(MapContext);
|
|
8
|
-
const [
|
|
8
|
+
const [marker, setMarker] = useState(null);
|
|
9
|
+
// The DOM element the `children` are rendered into. It is created once and reused for the
|
|
10
|
+
// lifetime of the component so that the marker's content is never torn down and rebuilt.
|
|
11
|
+
const element = useMemo(() => document.createElement('div'), []);
|
|
12
|
+
// Keeps the newest coordinates available to the creation effect without making it depend on
|
|
13
|
+
// them, so that a position change moves the existing marker instead of recreating it.
|
|
14
|
+
const coordinates = useRef([longitude, latitude]);
|
|
15
|
+
coordinates.current = [longitude, latitude];
|
|
9
16
|
useEffect(() => {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
let handleMarkerClick;
|
|
13
|
-
if (map && mapLoaded) {
|
|
14
|
-
// We create the DOM element that we will attach to the marker, and then
|
|
15
|
-
// render the `children` props into the DOM element.
|
|
16
|
-
const element = document.createElement('div');
|
|
17
|
-
setResult(ReactDOM.createPortal(children, element));
|
|
18
|
-
marker = new maplibre.Marker({ anchor, className, color, element })
|
|
19
|
-
.setLngLat([longitude, latitude])
|
|
20
|
-
.addTo(map);
|
|
21
|
-
if (popup) {
|
|
22
|
-
const popupHTML = ReactDOMServer.renderToStaticMarkup(popup);
|
|
23
|
-
handleMarkerClick = (e) => {
|
|
24
|
-
e.stopPropagation();
|
|
25
|
-
newPopup = new maplibre.Popup({
|
|
26
|
-
offset: [0, -15],
|
|
27
|
-
closeButton,
|
|
28
|
-
closeOnClick,
|
|
29
|
-
className: popupClassName,
|
|
30
|
-
})
|
|
31
|
-
.setLngLat([longitude, latitude])
|
|
32
|
-
.setHTML(popupHTML)
|
|
33
|
-
.addTo(map);
|
|
34
|
-
};
|
|
35
|
-
marker.getElement().addEventListener('click', handleMarkerClick);
|
|
36
|
-
}
|
|
17
|
+
if (!map || !mapLoaded) {
|
|
18
|
+
return;
|
|
37
19
|
}
|
|
20
|
+
const newMarker = new maplibre.Marker({ anchor, className, color, element })
|
|
21
|
+
.setLngLat(coordinates.current)
|
|
22
|
+
.addTo(map);
|
|
23
|
+
setMarker(newMarker);
|
|
38
24
|
return () => {
|
|
39
25
|
try {
|
|
40
|
-
|
|
41
|
-
marker.remove();
|
|
42
|
-
}
|
|
43
|
-
if (newPopup) {
|
|
44
|
-
newPopup.remove();
|
|
45
|
-
}
|
|
46
|
-
if (marker && handleMarkerClick) {
|
|
47
|
-
marker.getElement().removeEventListener('click', handleMarkerClick);
|
|
48
|
-
}
|
|
26
|
+
newMarker.remove();
|
|
49
27
|
}
|
|
50
28
|
catch (_a) {
|
|
51
29
|
console.warn('Error cleaning up Marker');
|
|
52
30
|
}
|
|
31
|
+
setMarker(null);
|
|
32
|
+
};
|
|
33
|
+
}, [anchor, className, color, element, map, mapLoaded]);
|
|
34
|
+
useEffect(() => {
|
|
35
|
+
if (marker) {
|
|
36
|
+
marker.setLngLat([longitude, latitude]);
|
|
37
|
+
}
|
|
38
|
+
}, [marker, latitude, longitude]);
|
|
39
|
+
useEffect(() => {
|
|
40
|
+
if (!marker || !popup) {
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
const newPopup = new maplibre.Popup({
|
|
44
|
+
offset: [0, -15],
|
|
45
|
+
closeButton,
|
|
46
|
+
closeOnClick,
|
|
47
|
+
className: popupClassName,
|
|
48
|
+
}).setHTML(ReactDOMServer.renderToStaticMarkup(popup));
|
|
49
|
+
// Attaching the popup to the marker lets MapLibre toggle it on click and keep it anchored to
|
|
50
|
+
// the marker as the marker moves.
|
|
51
|
+
marker.setPopup(newPopup);
|
|
52
|
+
return () => {
|
|
53
|
+
try {
|
|
54
|
+
newPopup.remove();
|
|
55
|
+
marker.setPopup(undefined);
|
|
56
|
+
}
|
|
57
|
+
catch (_a) {
|
|
58
|
+
console.warn('Error cleaning up Marker popup');
|
|
59
|
+
}
|
|
53
60
|
};
|
|
54
|
-
}, [
|
|
55
|
-
|
|
56
|
-
className,
|
|
57
|
-
children,
|
|
58
|
-
color,
|
|
59
|
-
latitude,
|
|
60
|
-
longitude,
|
|
61
|
-
map,
|
|
62
|
-
popup,
|
|
63
|
-
popupClassName,
|
|
64
|
-
closeOnClick,
|
|
65
|
-
closeButton,
|
|
66
|
-
mapLoaded,
|
|
67
|
-
]);
|
|
68
|
-
return result;
|
|
61
|
+
}, [marker, popup, popupClassName, closeButton, closeOnClick]);
|
|
62
|
+
return createPortal(children, element);
|
|
69
63
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"root":["../src/
|
|
1
|
+
{"root":["../src/errorboundary.tsx","../src/geolocatecontrol.tsx","../src/layer.tsx","../src/map.tsx","../src/marker.tsx","../src/navigationcontrol.tsx","../src/popup.tsx","../src/scalecontrol.tsx","../src/index.ts"],"version":"5.8.2"}
|
package/package.json
CHANGED
package/dist/types.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export type ControlPosition = 'top-right' | 'top-left' | 'bottom-right' | 'bottom-left';
|
package/dist/types.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|