File size: 1,649 Bytes
d2efaca |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
import { createElementObject, createOverlayComponent } from '@react-leaflet/core';
import { Popup as LeafletPopup } from 'leaflet';
import { useEffect } from 'react';
export const Popup = createOverlayComponent(function createPopup(props, context) {
const popup = new LeafletPopup(props, context.overlayContainer);
return createElementObject(popup, context);
}, function usePopupLifecycle(element, context, { position }, setOpen) {
useEffect(function addPopup() {
const { instance } = element;
function onPopupOpen(event) {
if (event.popup === instance) {
instance.update();
setOpen(true);
}
}
function onPopupClose(event) {
if (event.popup === instance) {
setOpen(false);
}
}
context.map.on({
popupopen: onPopupOpen,
popupclose: onPopupClose
});
if (context.overlayContainer == null) {
// Attach to a Map
if (position != null) {
instance.setLatLng(position);
}
instance.openOn(context.map);
} else {
// Attach to container component
context.overlayContainer.bindPopup(instance);
}
return function removePopup() {
context.map.off({
popupopen: onPopupOpen,
popupclose: onPopupClose
});
context.overlayContainer?.unbindPopup();
context.map.removeLayer(instance);
};
}, [
element,
context,
setOpen,
position
]);
});
|