File size: 1,654 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 { Tooltip as LeafletTooltip } from 'leaflet';
import { useEffect } from 'react';
export const Tooltip = createOverlayComponent(function createTooltip(props, context) {
const tooltip = new LeafletTooltip(props, context.overlayContainer);
return createElementObject(tooltip, context);
}, function useTooltipLifecycle(element, context, { position }, setOpen) {
useEffect(function addTooltip() {
const container = context.overlayContainer;
if (container == null) {
return;
}
const { instance } = element;
const onTooltipOpen = (event)=>{
if (event.tooltip === instance) {
if (position != null) {
instance.setLatLng(position);
}
instance.update();
setOpen(true);
}
};
const onTooltipClose = (event)=>{
if (event.tooltip === instance) {
setOpen(false);
}
};
container.on({
tooltipopen: onTooltipOpen,
tooltipclose: onTooltipClose
});
container.bindTooltip(instance);
return function removeTooltip() {
container.off({
tooltipopen: onTooltipOpen,
tooltipclose: onTooltipClose
});
// @ts-ignore protected property
if (container._map != null) {
container.unbindTooltip();
}
};
}, [
element,
context,
setOpen,
position
]);
});
|