UNPKG

1.43 kBJavaScriptView Raw
1import { createOverlayComponent } from '@react-leaflet/core';
2import { Tooltip as LeafletTooltip } from 'leaflet';
3import { useEffect } from 'react';
4export const Tooltip = createOverlayComponent(function createTooltip(props, context) {
5 return {
6 instance: new LeafletTooltip(props, context.overlayContainer),
7 context
8 };
9}, function useTooltipLifecycle(element, context, props, setOpen) {
10 const {
11 onClose,
12 onOpen
13 } = props;
14 useEffect(function addTooltip() {
15 const container = context.overlayContainer;
16
17 if (container == null) {
18 return;
19 }
20
21 const {
22 instance
23 } = element;
24
25 const onTooltipOpen = event => {
26 if (event.tooltip === instance) {
27 instance.update();
28 setOpen(true);
29 onOpen == null ? void 0 : onOpen();
30 }
31 };
32
33 const onTooltipClose = event => {
34 if (event.tooltip === instance) {
35 setOpen(false);
36 onClose == null ? void 0 : onClose();
37 }
38 };
39
40 container.on({
41 tooltipopen: onTooltipOpen,
42 tooltipclose: onTooltipClose
43 });
44 container.bindTooltip(instance);
45 return function removeTooltip() {
46 container.off({
47 tooltipopen: onTooltipOpen,
48 tooltipclose: onTooltipClose
49 }); // @ts-ignore protected property
50
51 if (container._map != null) {
52 container.unbindTooltip();
53 }
54 };
55 }, [element, context, setOpen, onClose, onOpen]);
56});
\No newline at end of file