@vinctus/react-maplibre 0.2.3 → 0.2.6

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.
@@ -1,7 +1,8 @@
1
1
  import { FC } from 'react';
2
+ import { GeolocateControlOptions } from 'maplibre-gl';
2
3
  import { ControlPosition } from './types';
3
4
  export type GeolocateControlProps = {
4
5
  position?: ControlPosition;
5
- showUserLocation?: boolean;
6
+ options?: GeolocateControlOptions;
6
7
  };
7
8
  export declare const GeolocateControl: FC<GeolocateControlProps>;
@@ -1,29 +1,16 @@
1
- var __rest = (this && this.__rest) || function (s, e) {
2
- var t = {};
3
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
4
- t[p] = s[p];
5
- if (s != null && typeof Object.getOwnPropertySymbols === "function")
6
- for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
7
- if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
8
- t[p[i]] = s[p[i]];
9
- }
10
- return t;
11
- };
12
1
  import { useEffect } from 'react';
13
2
  import maplibre from 'maplibre-gl';
14
3
  import { useMap } from './Map';
15
- export const GeolocateControl = (_a) => {
16
- var { position = 'top-right' } = _a, options = __rest(_a, ["position"]);
4
+ export const GeolocateControl = ({ position = 'bottom-right', options = { showUserLocation: false }, }) => {
17
5
  const { map } = useMap();
18
- const optionsString = JSON.stringify(options);
19
6
  useEffect(() => {
20
- const control = new maplibre.GeolocateControl(Object.assign({}, JSON.parse(optionsString)));
21
- if (map) {
7
+ const control = new maplibre.GeolocateControl(options);
8
+ if (map && position) {
22
9
  map.addControl(control, position);
23
10
  }
24
11
  return () => {
25
12
  map === null || map === void 0 ? void 0 : map.removeControl(control);
26
13
  };
27
- }, [position, optionsString, map]);
14
+ }, [position, options, map]);
28
15
  return null;
29
16
  };
package/dist/Marker.d.ts CHANGED
@@ -5,6 +5,7 @@ export type MarkerProps = {
5
5
  latitude: number;
6
6
  popup?: ReactElement;
7
7
  closeOnClick?: boolean;
8
+ closeButton?: boolean;
8
9
  anchor?: PositionAnchor;
9
10
  color?: string;
10
11
  children?: ReactNode;
package/dist/Marker.js CHANGED
@@ -9,35 +9,84 @@ var __rest = (this && this.__rest) || function (s, e) {
9
9
  }
10
10
  return t;
11
11
  };
12
- import { useEffect, useState, Children } from 'react';
12
+ import { useEffect, useState, Children, useRef } 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'; // eslint-disable-line import/no-webpack-loader-syntax
16
+ import ReactDOMServer from 'react-dom/server';
17
17
  export const Marker = (_a) => {
18
- var { children, longitude, latitude, popup, closeOnClick } = _a, options = __rest(_a, ["children", "longitude", "latitude", "popup", "closeOnClick"]);
18
+ var { children, longitude, latitude, popup, closeOnClick = false, closeButton = true } = _a, options = __rest(_a, ["children", "longitude", "latitude", "popup", "closeOnClick", "closeButton"]);
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);
21
24
  const optionsString = JSON.stringify(options);
22
25
  useEffect(() => {
23
- let marker = null;
26
+ if (!map)
27
+ return;
24
28
  const isEmpty = Children.count(children) === 0;
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 }).setHTML(ReactDOMServer.renderToStaticMarkup(popup)));
35
- marker = marker.setLngLat([longitude, latitude]).addTo(map);
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;
36
79
  }
37
80
  return () => {
38
- if (marker)
39
- marker.remove();
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
+ }
40
89
  };
41
- }, [closeOnClick, optionsString, setResult, children, latitude, longitude, map, popup]);
90
+ }, [map, optionsString, children, longitude, latitude, popup, closeOnClick, closeButton]);
42
91
  return result;
43
92
  };
@@ -0,0 +1,8 @@
1
+ import { FC } from 'react';
2
+ import { NavigationControlOptions } from 'maplibre-gl';
3
+ import { ControlPosition } from './types';
4
+ export type NavigationControlProps = {
5
+ position?: ControlPosition;
6
+ options?: NavigationControlOptions;
7
+ };
8
+ export declare const NavigationControl: FC<NavigationControlProps>;
@@ -0,0 +1,19 @@
1
+ import { useEffect } from 'react';
2
+ import maplibre from 'maplibre-gl';
3
+ import { useMap } from './Map';
4
+ export const NavigationControl = ({ position = 'bottom-right', options = {
5
+ showZoom: true,
6
+ showCompass: false,
7
+ }, }) => {
8
+ const { map } = useMap();
9
+ useEffect(() => {
10
+ const control = new maplibre.NavigationControl(options);
11
+ if (map && position) {
12
+ map.addControl(control, position);
13
+ }
14
+ return () => {
15
+ map === null || map === void 0 ? void 0 : map.removeControl(control);
16
+ };
17
+ }, [position, options, map]);
18
+ return null;
19
+ };
@@ -1,6 +1,8 @@
1
1
  import { FC } from 'react';
2
+ import { ScaleControlOptions } from 'maplibre-gl';
2
3
  import { ControlPosition } from './types';
3
4
  export type ScaleControlProps = {
4
5
  position?: ControlPosition;
6
+ options?: ScaleControlOptions;
5
7
  };
6
8
  export declare const ScaleControl: FC<ScaleControlProps>;
@@ -1,29 +1,16 @@
1
- var __rest = (this && this.__rest) || function (s, e) {
2
- var t = {};
3
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
4
- t[p] = s[p];
5
- if (s != null && typeof Object.getOwnPropertySymbols === "function")
6
- for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
7
- if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
8
- t[p[i]] = s[p[i]];
9
- }
10
- return t;
11
- };
12
1
  import { useEffect } from 'react';
13
2
  import maplibre from 'maplibre-gl';
14
3
  import { useMap } from './Map';
15
- export const ScaleControl = (_a) => {
16
- var { position = 'top-right' } = _a, options = __rest(_a, ["position"]);
4
+ export const ScaleControl = ({ position = 'bottom-right', options, }) => {
17
5
  const { map } = useMap();
18
- const optionsString = JSON.stringify(options);
19
6
  useEffect(() => {
20
- const control = new maplibre.ScaleControl();
21
- if (map) {
7
+ const control = new maplibre.ScaleControl(options);
8
+ if (map && position) {
22
9
  map.addControl(control, position);
23
10
  }
24
11
  return () => {
25
12
  map === null || map === void 0 ? void 0 : map.removeControl(control);
26
13
  };
27
- }, [position, optionsString, map]);
14
+ }, [position, options, map]);
28
15
  return null;
29
16
  };
package/dist/index.d.ts CHANGED
@@ -5,3 +5,4 @@ export { Popup } from './Popup';
5
5
  export { Layer } from './Layer';
6
6
  export { GeolocateControl } from './GeolocateControl';
7
7
  export { ScaleControl } from './ScaleControl';
8
+ export { NavigationControl } from './NavigationControl';
package/dist/index.js CHANGED
@@ -4,3 +4,4 @@ export { Popup } from './Popup';
4
4
  export { Layer } from './Layer';
5
5
  export { GeolocateControl } from './GeolocateControl';
6
6
  export { ScaleControl } from './ScaleControl';
7
+ export { NavigationControl } from './NavigationControl';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vinctus/react-maplibre",
3
- "version": "0.2.3",
3
+ "version": "0.2.6",
4
4
  "description": "Minimalist MapLibre GL JS React wrapper library",
5
5
  "type": "module",
6
6
  "types": "dist/index.d.ts",