@vinctus/react-maplibre 0.2.13 → 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.
Files changed (2) hide show
  1. package/dist/Map.js +8 -21
  2. package/package.json +1 -1
package/dist/Map.js CHANGED
@@ -42,15 +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 mapInstanceRef = useRef(null);
45
46
  const { map, setMap, setMapLoaded } = useMap();
46
47
  useImperativeHandle(ref, () => map, [map]);
47
48
  useEffect(() => {
48
49
  if (map)
49
50
  return; // initialize map only once
50
- let createdMap = null;
51
51
  if (mapContainer.current) {
52
52
  const m = new maplibre.Map(Object.assign({ container: mapContainer.current, style: mapStyle, center: [longitude, latitude], zoom }, options));
53
- createdMap = m;
53
+ mapInstanceRef.current = m;
54
54
  if (onDragEnd)
55
55
  addEventHandler(m, 'dragend', onDragEnd);
56
56
  if (onLoad)
@@ -63,26 +63,13 @@ export const Map = React.forwardRef((_a, ref) => {
63
63
  if (onZoomEnd)
64
64
  addEventHandler(m, 'zoomend', onZoomEnd);
65
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
+ };
66
71
  }
67
- return () => {
68
- if (createdMap) {
69
- createdMap.remove(); // This destroys the map and cleans up all layers/sources
70
- setMap(null);
71
- setMapLoaded(false);
72
- }
73
- };
74
- }, [
75
- map,
76
- onDragEnd,
77
- onLoad,
78
- onMoveEnd,
79
- onZoomEnd,
80
- mapStyle,
81
- longitude,
82
- latitude,
83
- zoom,
84
- options,
85
- ]);
72
+ }, []); // Empty deps - only run once on mount
86
73
  const memoizedStyle = useMemo(() => (Object.assign({ position: 'relative', width: '100%', height: '100%' }, style)), [style]);
87
74
  return (_jsx(ErrorBoundary, { fallback: fallback, children: _jsx("div", { id: id, ref: mapContainer, style: memoizedStyle, children: children }) }));
88
75
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vinctus/react-maplibre",
3
- "version": "0.2.13",
3
+ "version": "0.2.15",
4
4
  "description": "Minimalist MapLibre GL JS React wrapper library",
5
5
  "type": "module",
6
6
  "types": "dist/index.d.ts",