@vinctus/react-maplibre 0.2.14 → 0.2.15
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/Map.js +9 -24
- package/package.json +1 -1
package/dist/Map.js
CHANGED
|
@@ -42,17 +42,15 @@ function addEventHandler(map, type, handler) {
|
|
|
42
42
|
export const Map = React.forwardRef((_a, ref) => {
|
|
43
43
|
var { children, style, id, longitude, latitude, maxPitch = DISABLE_PITCH, mapStyle = DEFAULT_STYLE, zoom, onDragEnd, onLoad, onMoveEnd, onZoomEnd, fallback } = _a, options = __rest(_a, ["children", "style", "id", "longitude", "latitude", "maxPitch", "mapStyle", "zoom", "onDragEnd", "onLoad", "onMoveEnd", "onZoomEnd", "fallback"]);
|
|
44
44
|
const mapContainer = useRef(null);
|
|
45
|
-
const
|
|
45
|
+
const mapInstanceRef = useRef(null);
|
|
46
46
|
const { map, setMap, setMapLoaded } = useMap();
|
|
47
47
|
useImperativeHandle(ref, () => map, [map]);
|
|
48
48
|
useEffect(() => {
|
|
49
|
-
if (
|
|
49
|
+
if (map)
|
|
50
50
|
return; // initialize map only once
|
|
51
|
-
let createdMap = null;
|
|
52
51
|
if (mapContainer.current) {
|
|
53
52
|
const m = new maplibre.Map(Object.assign({ container: mapContainer.current, style: mapStyle, center: [longitude, latitude], zoom }, options));
|
|
54
|
-
|
|
55
|
-
initializationRef.current = true;
|
|
53
|
+
mapInstanceRef.current = m;
|
|
56
54
|
if (onDragEnd)
|
|
57
55
|
addEventHandler(m, 'dragend', onDragEnd);
|
|
58
56
|
if (onLoad)
|
|
@@ -65,26 +63,13 @@ export const Map = React.forwardRef((_a, ref) => {
|
|
|
65
63
|
if (onZoomEnd)
|
|
66
64
|
addEventHandler(m, 'zoomend', onZoomEnd);
|
|
67
65
|
setMap(m);
|
|
66
|
+
// Cleanup when component unmounts
|
|
67
|
+
return () => {
|
|
68
|
+
m.remove(); // This destroys the map and cleans up all layers/sources
|
|
69
|
+
mapInstanceRef.current = null;
|
|
70
|
+
};
|
|
68
71
|
}
|
|
69
|
-
|
|
70
|
-
if (createdMap) {
|
|
71
|
-
createdMap.remove(); // This destroys the map and cleans up all layers/sources
|
|
72
|
-
setMap(null);
|
|
73
|
-
setMapLoaded(false);
|
|
74
|
-
initializationRef.current = false;
|
|
75
|
-
}
|
|
76
|
-
};
|
|
77
|
-
}, [
|
|
78
|
-
onDragEnd,
|
|
79
|
-
onLoad,
|
|
80
|
-
onMoveEnd,
|
|
81
|
-
onZoomEnd,
|
|
82
|
-
mapStyle,
|
|
83
|
-
longitude,
|
|
84
|
-
latitude,
|
|
85
|
-
zoom,
|
|
86
|
-
options,
|
|
87
|
-
]);
|
|
72
|
+
}, []); // Empty deps - only run once on mount
|
|
88
73
|
const memoizedStyle = useMemo(() => (Object.assign({ position: 'relative', width: '100%', height: '100%' }, style)), [style]);
|
|
89
74
|
return (_jsx(ErrorBoundary, { fallback: fallback, children: _jsx("div", { id: id, ref: mapContainer, style: memoizedStyle, children: children }) }));
|
|
90
75
|
});
|