@vinctus/react-maplibre 0.2.17 → 0.2.18

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.
@@ -3,16 +3,17 @@ import maplibre from 'maplibre-gl';
3
3
  import { useMap } from './Map';
4
4
  export const GeolocateControl = ({ position = 'bottom-right', options = { showUserLocation: false }, }) => {
5
5
  const { map } = useMap();
6
+ const optionsString = JSON.stringify(options);
6
7
  useEffect(() => {
7
- const control = new maplibre.GeolocateControl(options);
8
+ const control = new maplibre.GeolocateControl(JSON.parse(optionsString));
8
9
  if (map && position) {
9
10
  map.addControl(control, position);
10
11
  }
11
12
  return () => {
12
- if (map && map.hasControl && map.hasControl(control)) {
13
+ if (map && map.hasControl(control)) {
13
14
  map.removeControl(control);
14
15
  }
15
16
  };
16
- }, [position, options, map]);
17
+ }, [position, optionsString, map]);
17
18
  return null;
18
19
  };
package/dist/Layer.js CHANGED
@@ -14,24 +14,14 @@ export const Layer = ({ layer, source }) => {
14
14
  map.addLayer(parsedLayer);
15
15
  }
16
16
  return () => {
17
- if (added && map) {
18
- try {
19
- // Check if layer exists before removing
20
- if (map.getLayer && map.getLayer(parsedLayer.id)) {
21
- map.removeLayer(parsedLayer.id); // when removing, the layer must be removed first, and then the source
22
- }
17
+ if (map) {
18
+ // Check if layer exists before removing
19
+ if (map.getLayer && map.getLayer(parsedLayer.id)) {
20
+ map.removeLayer(parsedLayer.id); // when removing, the layer must be removed first, and then the source
23
21
  }
24
- catch (_a) {
25
- // Layer might not exist or map might be destroyed
26
- }
27
- try {
28
- // Check if source exists before removing
29
- if (map.getSource && map.getSource(parsedLayer.id)) {
30
- map.removeSource(parsedLayer.id);
31
- }
32
- }
33
- catch (_b) {
34
- // Source might not exist or map might be destroyed
22
+ // Check if source exists before removing
23
+ if (map.getSource && map.getSource(parsedLayer.id)) {
24
+ map.removeSource(parsedLayer.id);
35
25
  }
36
26
  }
37
27
  };
package/dist/Map.js CHANGED
@@ -44,12 +44,14 @@ export const Map = React.forwardRef((_a, ref) => {
44
44
  const mapContainer = useRef(null);
45
45
  const mapInstanceRef = useRef(null);
46
46
  const { map, setMap, setMapLoaded } = useMap();
47
+ const optionsString = JSON.stringify(options);
47
48
  useImperativeHandle(ref, () => map, [map]);
48
49
  useEffect(() => {
49
50
  if (map)
50
51
  return; // initialize map only once
51
52
  if (mapContainer.current) {
52
- const m = new maplibre.Map(Object.assign({ container: mapContainer.current, style: mapStyle, center: [longitude, latitude], zoom }, options));
53
+ const parsedOptions = JSON.parse(optionsString);
54
+ const m = new maplibre.Map(Object.assign({ container: mapContainer.current, style: mapStyle, center: [longitude, latitude], zoom }, parsedOptions));
53
55
  mapInstanceRef.current = m;
54
56
  if (onDragEnd)
55
57
  addEventHandler(m, 'dragend', onDragEnd);
@@ -69,7 +71,7 @@ export const Map = React.forwardRef((_a, ref) => {
69
71
  mapInstanceRef.current = null;
70
72
  };
71
73
  }
72
- }, []); // Empty deps - only run once on mount
74
+ }, [map, mapStyle, longitude, latitude, zoom, optionsString, onDragEnd, onLoad, onMoveEnd, onZoomEnd, setMap, setMapLoaded]);
73
75
  const memoizedStyle = useMemo(() => (Object.assign({ position: 'relative', width: '100%', height: '100%' }, style)), [style]);
74
76
  return (_jsx(ErrorBoundary, { fallback: fallback, children: _jsx("div", { id: id, ref: mapContainer, style: memoizedStyle, children: children }) }));
75
77
  });
package/dist/Marker.js CHANGED
@@ -9,7 +9,7 @@ var __rest = (this && this.__rest) || function (s, e) {
9
9
  }
10
10
  return t;
11
11
  };
12
- import { useEffect, useState, Children, useRef } from 'react';
12
+ import { useEffect, useState, useRef } from 'react';
13
13
  import maplibre from 'maplibre-gl';
14
14
  import { useMap } from './Map';
15
15
  import ReactDOM from 'react-dom';
@@ -22,24 +22,21 @@ export const Marker = (_a) => {
22
22
  const popupRef = useRef(null);
23
23
  const cleanupRef = useRef(null);
24
24
  const optionsString = JSON.stringify(options);
25
+ const popupString = JSON.stringify(popup);
25
26
  useEffect(() => {
26
27
  if (!map)
27
28
  return;
28
- const isEmpty = Children.count(children) === 0;
29
29
  const element = document.createElement('div');
30
30
  const parsedOptions = JSON.parse(optionsString);
31
- if (!isEmpty) {
32
- setResult(ReactDOM.createPortal(children, element));
33
- }
31
+ setResult(ReactDOM.createPortal(children, element));
34
32
  // Create marker
35
- const marker = isEmpty
36
- ? new maplibre.Marker(Object.assign({}, parsedOptions))
37
- : new maplibre.Marker(Object.assign(Object.assign({}, parsedOptions), { element }));
33
+ const marker = new maplibre.Marker(Object.assign(Object.assign({}, parsedOptions), { element }));
38
34
  marker.setLngLat([longitude, latitude]).addTo(map);
39
35
  markerRef.current = marker;
40
36
  // Handle popup manually if provided
41
37
  if (popup) {
42
- const popupHTML = ReactDOMServer.renderToStaticMarkup(popup);
38
+ const parsedPopup = JSON.parse(popupString);
39
+ const popupHTML = ReactDOMServer.renderToStaticMarkup(parsedPopup);
43
40
  const handleMarkerClick = (e) => {
44
41
  e.stopPropagation();
45
42
  // Close existing popup if any
@@ -88,6 +85,6 @@ export const Marker = (_a) => {
88
85
  markerRef.current = null;
89
86
  }
90
87
  };
91
- }, [map, optionsString, children, longitude, latitude, popup, closeOnClick, closeButton]);
88
+ }, [map, optionsString, children, longitude, latitude, popupString, closeOnClick, closeButton, popupClassName]);
92
89
  return result;
93
90
  };
@@ -6,16 +6,17 @@ export const NavigationControl = ({ position = 'bottom-right', options = {
6
6
  showCompass: false,
7
7
  }, }) => {
8
8
  const { map } = useMap();
9
+ const optionsString = JSON.stringify(options);
9
10
  useEffect(() => {
10
- const control = new maplibre.NavigationControl(options);
11
+ const control = new maplibre.NavigationControl(JSON.parse(optionsString));
11
12
  if (map && position) {
12
13
  map.addControl(control, position);
13
14
  }
14
15
  return () => {
15
- if (map && map.hasControl && map.hasControl(control)) {
16
+ if (map && map.hasControl(control)) {
16
17
  map.removeControl(control);
17
18
  }
18
19
  };
19
- }, [position, options, map]);
20
+ }, [position, optionsString, map]);
20
21
  return null;
21
22
  };
@@ -1,18 +1,19 @@
1
1
  import { useEffect } from 'react';
2
2
  import maplibre from 'maplibre-gl';
3
3
  import { useMap } from './Map';
4
- export const ScaleControl = ({ position = 'bottom-right', options, }) => {
4
+ export const ScaleControl = ({ position = 'bottom-right', options }) => {
5
5
  const { map } = useMap();
6
+ const optionsString = JSON.stringify(options);
6
7
  useEffect(() => {
7
- const control = new maplibre.ScaleControl(options);
8
+ const control = new maplibre.ScaleControl(JSON.parse(optionsString));
8
9
  if (map && position) {
9
10
  map.addControl(control, position);
10
11
  }
11
12
  return () => {
12
- if (map && map.hasControl && map.hasControl(control)) {
13
+ if (map && map.hasControl(control)) {
13
14
  map.removeControl(control);
14
15
  }
15
16
  };
16
- }, [position, options, map]);
17
+ }, [position, optionsString, map]);
17
18
  return null;
18
19
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vinctus/react-maplibre",
3
- "version": "0.2.17",
3
+ "version": "0.2.18",
4
4
  "description": "Minimalist MapLibre GL JS React wrapper library",
5
5
  "type": "module",
6
6
  "types": "dist/index.d.ts",