@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 CHANGED
@@ -4,8 +4,8 @@ export type MarkerProps = {
4
4
  longitude: number;
5
5
  latitude: number;
6
6
  popup?: ReactElement;
7
+ popupClassName?: string;
7
8
  closeOnClick?: boolean;
8
- closeButton?: boolean;
9
9
  anchor?: PositionAnchor;
10
10
  color?: string;
11
11
  children?: ReactNode;
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, useRef } from 'react';
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, closeOnClick = false, closeButton = true } = _a, options = __rest(_a, ["children", "longitude", "latitude", "popup", "closeOnClick", "closeButton"]);
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
- if (!map)
27
- return;
23
+ let marker = null;
28
24
  const isEmpty = Children.count(children) === 0;
29
- const element = document.createElement('div');
30
- const parsedOptions = JSON.parse(optionsString);
31
- if (!isEmpty) {
32
- setResult(ReactDOM.createPortal(children, element));
33
- }
34
- // Create marker
35
- const marker = isEmpty
36
- ? new maplibre.Marker(Object.assign({}, parsedOptions))
37
- : new maplibre.Marker(Object.assign(Object.assign({}, parsedOptions), { element }));
38
- marker.setLngLat([longitude, latitude]).addTo(map);
39
- markerRef.current = marker;
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 (cleanupRef.current) {
82
- cleanupRef.current();
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
- }, [map, optionsString, children, longitude, latitude, popup, closeOnClick, closeButton]);
41
+ }, [closeOnClick, optionsString, setResult, children, latitude, longitude, map, popup]);
91
42
  return result;
92
43
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vinctus/react-maplibre",
3
- "version": "0.2.6",
3
+ "version": "0.2.7",
4
4
  "description": "Minimalist MapLibre GL JS React wrapper library",
5
5
  "type": "module",
6
6
  "types": "dist/index.d.ts",