@vinctus/react-maplibre 0.2.6 → 0.2.7
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.d.ts +1 -1
- package/dist/Marker.js +18 -67
- package/package.json +1 -1
package/dist/Marker.d.ts
CHANGED
package/dist/Marker.js
CHANGED
|
@@ -9,84 +9,35 @@ var __rest = (this && this.__rest) || function (s, e) {
|
|
|
9
9
|
}
|
|
10
10
|
return t;
|
|
11
11
|
};
|
|
12
|
-
import { useEffect, useState, Children
|
|
12
|
+
import { useEffect, useState, Children } from 'react';
|
|
13
13
|
import maplibre from 'maplibre-gl';
|
|
14
14
|
import { useMap } from './Map';
|
|
15
15
|
import ReactDOM from 'react-dom';
|
|
16
|
-
import ReactDOMServer from 'react-dom/server';
|
|
16
|
+
import ReactDOMServer from 'react-dom/server'; // eslint-disable-line import/no-webpack-loader-syntax
|
|
17
17
|
export const Marker = (_a) => {
|
|
18
|
-
var { children, longitude, latitude, popup,
|
|
18
|
+
var { children, longitude, latitude, popup, popupClassName, closeOnClick } = _a, options = __rest(_a, ["children", "longitude", "latitude", "popup", "popupClassName", "closeOnClick"]);
|
|
19
19
|
const { map } = useMap();
|
|
20
20
|
const [result, setResult] = useState(null);
|
|
21
|
-
const markerRef = useRef(null);
|
|
22
|
-
const popupRef = useRef(null);
|
|
23
|
-
const cleanupRef = useRef(null);
|
|
24
21
|
const optionsString = JSON.stringify(options);
|
|
25
22
|
useEffect(() => {
|
|
26
|
-
|
|
27
|
-
return;
|
|
23
|
+
let marker = null;
|
|
28
24
|
const isEmpty = Children.count(children) === 0;
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
// Handle popup manually if provided
|
|
41
|
-
if (popup) {
|
|
42
|
-
const popupHTML = ReactDOMServer.renderToStaticMarkup(popup);
|
|
43
|
-
const handleMarkerClick = (e) => {
|
|
44
|
-
e.stopPropagation();
|
|
45
|
-
// Close existing popup if any
|
|
46
|
-
if (popupRef.current) {
|
|
47
|
-
popupRef.current.remove();
|
|
48
|
-
popupRef.current = null;
|
|
49
|
-
return; // Toggle behavior - close if already open
|
|
50
|
-
}
|
|
51
|
-
// Create new popup directly on the map
|
|
52
|
-
const newPopup = new maplibre.Popup({
|
|
53
|
-
offset: [0, -15],
|
|
54
|
-
closeOnClick,
|
|
55
|
-
closeButton,
|
|
56
|
-
})
|
|
57
|
-
.setLngLat([longitude, latitude])
|
|
58
|
-
.setHTML(popupHTML)
|
|
59
|
-
.addTo(map);
|
|
60
|
-
popupRef.current = newPopup;
|
|
61
|
-
// Listen for popup close to clean up reference
|
|
62
|
-
newPopup.on('close', () => {
|
|
63
|
-
popupRef.current = null;
|
|
64
|
-
});
|
|
65
|
-
};
|
|
66
|
-
// Add click listener to marker element
|
|
67
|
-
const markerElement = marker.getElement();
|
|
68
|
-
markerElement.style.cursor = 'pointer';
|
|
69
|
-
markerElement.addEventListener('click', handleMarkerClick);
|
|
70
|
-
// Cleanup function
|
|
71
|
-
const cleanup = () => {
|
|
72
|
-
markerElement.removeEventListener('click', handleMarkerClick);
|
|
73
|
-
if (popupRef.current) {
|
|
74
|
-
popupRef.current.remove();
|
|
75
|
-
popupRef.current = null;
|
|
76
|
-
}
|
|
77
|
-
};
|
|
78
|
-
cleanupRef.current = cleanup;
|
|
25
|
+
if (map) {
|
|
26
|
+
const element = document.createElement('div');
|
|
27
|
+
const parsedOptions = JSON.parse(optionsString);
|
|
28
|
+
if (!isEmpty)
|
|
29
|
+
setResult(ReactDOM.createPortal(children, element));
|
|
30
|
+
marker = isEmpty
|
|
31
|
+
? new maplibre.Marker(Object.assign({}, parsedOptions))
|
|
32
|
+
: new maplibre.Marker(Object.assign(Object.assign({}, parsedOptions), { element }));
|
|
33
|
+
if (popup)
|
|
34
|
+
marker.setPopup(new maplibre.Popup({ offset: [0, -15], closeOnClick, className: popupClassName }).setHTML(ReactDOMServer.renderToStaticMarkup(popup)));
|
|
35
|
+
marker = marker.setLngLat([longitude, latitude]).addTo(map);
|
|
79
36
|
}
|
|
80
37
|
return () => {
|
|
81
|
-
if (
|
|
82
|
-
|
|
83
|
-
cleanupRef.current = null;
|
|
84
|
-
}
|
|
85
|
-
if (markerRef.current) {
|
|
86
|
-
markerRef.current.remove();
|
|
87
|
-
markerRef.current = null;
|
|
88
|
-
}
|
|
38
|
+
if (marker)
|
|
39
|
+
marker.remove();
|
|
89
40
|
};
|
|
90
|
-
}, [
|
|
41
|
+
}, [closeOnClick, optionsString, setResult, children, latitude, longitude, map, popup]);
|
|
91
42
|
return result;
|
|
92
43
|
};
|