@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.
- package/dist/GeolocateControl.d.ts +2 -1
- package/dist/GeolocateControl.js +4 -17
- package/dist/Marker.d.ts +1 -0
- package/dist/Marker.js +67 -18
- package/dist/NavigationControl.d.ts +8 -0
- package/dist/NavigationControl.js +19 -0
- package/dist/ScaleControl.d.ts +2 -0
- package/dist/ScaleControl.js +4 -17
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/package.json +1 -1
|
@@ -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
|
-
|
|
6
|
+
options?: GeolocateControlOptions;
|
|
6
7
|
};
|
|
7
8
|
export declare const GeolocateControl: FC<GeolocateControlProps>;
|
package/dist/GeolocateControl.js
CHANGED
|
@@ -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 = (
|
|
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(
|
|
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,
|
|
14
|
+
}, [position, options, map]);
|
|
28
15
|
return null;
|
|
29
16
|
};
|
package/dist/Marker.d.ts
CHANGED
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';
|
|
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
|
-
|
|
26
|
+
if (!map)
|
|
27
|
+
return;
|
|
24
28
|
const isEmpty = Children.count(children) === 0;
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
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 (
|
|
39
|
-
|
|
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
|
-
}, [
|
|
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
|
+
};
|
package/dist/ScaleControl.d.ts
CHANGED
|
@@ -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>;
|
package/dist/ScaleControl.js
CHANGED
|
@@ -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 = (
|
|
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,
|
|
14
|
+
}, [position, options, map]);
|
|
28
15
|
return null;
|
|
29
16
|
};
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED