react-tooltip 5.1.0-beta.0 → 5.1.0
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/README.md +6 -0
- package/dist/react-tooltip.cjs.js +230 -114
- package/dist/react-tooltip.cjs.min.js +2 -2
- package/dist/react-tooltip.d.ts +40 -5
- package/dist/react-tooltip.esm.js +230 -116
- package/dist/react-tooltip.esm.min.js +2 -2
- package/dist/react-tooltip.umd.js +230 -114
- package/dist/react-tooltip.umd.min.js +2 -2
- package/package.json +1 -1
- package/src/App.tsx +0 -79
- package/src/components/Tooltip/Tooltip.tsx +0 -191
- package/src/components/Tooltip/TooltipTypes.d.ts +0 -35
- package/src/components/Tooltip/index.ts +0 -1
- package/src/components/Tooltip/styles.module.css +0 -62
- package/src/components/TooltipContent/TooltipContent.tsx +0 -8
- package/src/components/TooltipContent/TooltipContentTypes.d.ts +0 -3
- package/src/components/TooltipContent/index.ts +0 -1
- package/src/components/TooltipController/TooltipController.tsx +0 -203
- package/src/components/TooltipController/TooltipControllerTypes.d.ts +0 -32
- package/src/components/TooltipController/constants.ts +0 -11
- package/src/components/TooltipController/index.ts +0 -1
- package/src/index-dev.tsx +0 -15
- package/src/index.tsx +0 -3
- package/src/styles.module.css +0 -5
- package/src/test/__snapshots__/index.spec.js.snap +0 -107
- package/src/test/index.spec.js +0 -142
- package/src/tokens.css +0 -8
- package/src/utils/compute-positions-types.d.ts +0 -8
- package/src/utils/compute-positions.ts +0 -66
- package/src/utils/debounce.ts +0 -32
|
@@ -1392,9 +1392,7 @@
|
|
|
1392
1392
|
|
|
1393
1393
|
var classNames = classnames.exports;
|
|
1394
1394
|
|
|
1395
|
-
/* eslint-disable
|
|
1396
|
-
/* eslint-disable prefer-rest-params */
|
|
1397
|
-
/* eslint-disable @typescript-eslint/no-this-alias */
|
|
1395
|
+
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
1398
1396
|
/**
|
|
1399
1397
|
* This function debounce the received function
|
|
1400
1398
|
* @param { function } func Function to be debounced
|
|
@@ -1402,19 +1400,17 @@
|
|
|
1402
1400
|
* @param { boolean } immediate Param to define if the function will be executed immediately
|
|
1403
1401
|
*/
|
|
1404
1402
|
const debounce = (func, wait, immediate) => {
|
|
1405
|
-
let timeout;
|
|
1406
|
-
return function () {
|
|
1407
|
-
// @ts-ignore
|
|
1408
|
-
const context = this;
|
|
1409
|
-
const args = arguments;
|
|
1403
|
+
let timeout = null;
|
|
1404
|
+
return function debounced(...args) {
|
|
1410
1405
|
const later = () => {
|
|
1411
1406
|
timeout = null;
|
|
1412
1407
|
if (!immediate) {
|
|
1413
|
-
func.apply(
|
|
1408
|
+
func.apply(this, args);
|
|
1414
1409
|
}
|
|
1415
1410
|
};
|
|
1416
|
-
|
|
1417
|
-
|
|
1411
|
+
if (timeout) {
|
|
1412
|
+
clearTimeout(timeout);
|
|
1413
|
+
}
|
|
1418
1414
|
timeout = setTimeout(later, wait);
|
|
1419
1415
|
};
|
|
1420
1416
|
};
|
|
@@ -1423,6 +1419,101 @@
|
|
|
1423
1419
|
return jsxRuntime.exports.jsx("span", { dangerouslySetInnerHTML: { __html: content } });
|
|
1424
1420
|
};
|
|
1425
1421
|
|
|
1422
|
+
const defaultContextData = {
|
|
1423
|
+
anchorRefs: new Set(),
|
|
1424
|
+
activeAnchor: { current: null },
|
|
1425
|
+
attach: () => {
|
|
1426
|
+
/* attach anchor element */
|
|
1427
|
+
},
|
|
1428
|
+
detach: () => {
|
|
1429
|
+
/* detach anchor element */
|
|
1430
|
+
},
|
|
1431
|
+
setActiveAnchor: () => {
|
|
1432
|
+
/* set active anchor */
|
|
1433
|
+
},
|
|
1434
|
+
};
|
|
1435
|
+
const defaultContextWrapper = Object.assign(() => defaultContextData, defaultContextData);
|
|
1436
|
+
const TooltipContext = require$$0.createContext(defaultContextWrapper);
|
|
1437
|
+
const TooltipProvider = ({ children }) => {
|
|
1438
|
+
const defaultTooltipId = require$$0.useId();
|
|
1439
|
+
const [anchorRefMap, setAnchorRefMap] = require$$0.useState({
|
|
1440
|
+
[defaultTooltipId]: new Set(),
|
|
1441
|
+
});
|
|
1442
|
+
const [activeAnchorMap, setActiveAnchorMap] = require$$0.useState({
|
|
1443
|
+
[defaultTooltipId]: { current: null },
|
|
1444
|
+
});
|
|
1445
|
+
const attach = (tooltipId, ...refs) => {
|
|
1446
|
+
setAnchorRefMap((oldMap) => {
|
|
1447
|
+
var _a;
|
|
1448
|
+
const tooltipRefs = (_a = oldMap[tooltipId]) !== null && _a !== void 0 ? _a : new Set();
|
|
1449
|
+
refs.forEach((ref) => tooltipRefs.add(ref));
|
|
1450
|
+
// create new object to trigger re-render
|
|
1451
|
+
return { ...oldMap, [tooltipId]: new Set(tooltipRefs) };
|
|
1452
|
+
});
|
|
1453
|
+
};
|
|
1454
|
+
const detach = (tooltipId, ...refs) => {
|
|
1455
|
+
setAnchorRefMap((oldMap) => {
|
|
1456
|
+
const tooltipRefs = oldMap[tooltipId];
|
|
1457
|
+
if (!tooltipRefs) {
|
|
1458
|
+
// tooltip not found
|
|
1459
|
+
// maybe thow error?
|
|
1460
|
+
return oldMap;
|
|
1461
|
+
}
|
|
1462
|
+
refs.forEach((ref) => tooltipRefs.delete(ref));
|
|
1463
|
+
// create new object to trigger re-render
|
|
1464
|
+
return { ...oldMap };
|
|
1465
|
+
});
|
|
1466
|
+
};
|
|
1467
|
+
const setActiveAnchor = (tooltipId, ref) => {
|
|
1468
|
+
setActiveAnchorMap((oldMap) => {
|
|
1469
|
+
var _a;
|
|
1470
|
+
if (((_a = oldMap[tooltipId]) === null || _a === void 0 ? void 0 : _a.current) === ref.current) {
|
|
1471
|
+
return oldMap;
|
|
1472
|
+
}
|
|
1473
|
+
// create new object to trigger re-render
|
|
1474
|
+
return { ...oldMap, [tooltipId]: ref };
|
|
1475
|
+
});
|
|
1476
|
+
};
|
|
1477
|
+
const getTooltipData = require$$0.useCallback((tooltipId) => {
|
|
1478
|
+
var _a, _b;
|
|
1479
|
+
return ({
|
|
1480
|
+
anchorRefs: (_a = anchorRefMap[tooltipId !== null && tooltipId !== void 0 ? tooltipId : defaultTooltipId]) !== null && _a !== void 0 ? _a : new Set(),
|
|
1481
|
+
activeAnchor: (_b = activeAnchorMap[tooltipId !== null && tooltipId !== void 0 ? tooltipId : defaultTooltipId]) !== null && _b !== void 0 ? _b : { current: null },
|
|
1482
|
+
attach: (...refs) => attach(tooltipId !== null && tooltipId !== void 0 ? tooltipId : defaultTooltipId, ...refs),
|
|
1483
|
+
detach: (...refs) => detach(tooltipId !== null && tooltipId !== void 0 ? tooltipId : defaultTooltipId, ...refs),
|
|
1484
|
+
setActiveAnchor: (ref) => setActiveAnchor(tooltipId !== null && tooltipId !== void 0 ? tooltipId : defaultTooltipId, ref),
|
|
1485
|
+
});
|
|
1486
|
+
}, [defaultTooltipId, anchorRefMap, activeAnchorMap, attach, detach]);
|
|
1487
|
+
const context = require$$0.useMemo(() => {
|
|
1488
|
+
const contextData = getTooltipData(defaultTooltipId);
|
|
1489
|
+
const contextWrapper = Object.assign((tooltipId) => getTooltipData(tooltipId), contextData);
|
|
1490
|
+
return contextWrapper;
|
|
1491
|
+
}, [getTooltipData]);
|
|
1492
|
+
return jsxRuntime.exports.jsx(TooltipContext.Provider, { value: context, children: children });
|
|
1493
|
+
};
|
|
1494
|
+
/*
|
|
1495
|
+
// this will use the "global" tooltip (same as `useTooltip()()`)
|
|
1496
|
+
const { anchorRefs, attach, detach } = useTooltip()
|
|
1497
|
+
|
|
1498
|
+
// this will use the tooltip with id `tooltip-id`
|
|
1499
|
+
const { anchorRefs, attach, detach } = useTooltip()('tooltip-id')
|
|
1500
|
+
*/
|
|
1501
|
+
function useTooltip() {
|
|
1502
|
+
return require$$0.useContext(TooltipContext);
|
|
1503
|
+
}
|
|
1504
|
+
|
|
1505
|
+
const TooltipWrapper = ({ tooltipId, children, place, content, html, variant, offset, wrapper, events, positionStrategy, delayShow, delayHide, }) => {
|
|
1506
|
+
const { attach, detach } = useTooltip()(tooltipId);
|
|
1507
|
+
const anchorRef = require$$0.useRef(null);
|
|
1508
|
+
require$$0.useEffect(() => {
|
|
1509
|
+
attach(anchorRef);
|
|
1510
|
+
return () => {
|
|
1511
|
+
detach(anchorRef);
|
|
1512
|
+
};
|
|
1513
|
+
}, []);
|
|
1514
|
+
return (jsxRuntime.exports.jsx("span", { ref: anchorRef, "data-tooltip-place": place, "data-tooltip-content": content, "data-tooltip-html": html, "data-tooltip-variant": variant, "data-tooltip-offset": offset, "data-tooltip-wrapper": wrapper, "data-tooltip-events": events, "data-tooltip-position-strategy": positionStrategy, "data-tooltip-delay-show": delayShow, "data-tooltip-delay-hide": delayHide, children: children }));
|
|
1515
|
+
};
|
|
1516
|
+
|
|
1426
1517
|
function getSide(placement) {
|
|
1427
1518
|
return placement.split('-')[0];
|
|
1428
1519
|
}
|
|
@@ -2608,7 +2699,7 @@
|
|
|
2608
2699
|
...options
|
|
2609
2700
|
});
|
|
2610
2701
|
|
|
2611
|
-
const
|
|
2702
|
+
const computeTooltipPosition = async ({ elementReference = null, tooltipReference = null, tooltipArrowReference = null, place = 'top', offset: offsetValue = 10, strategy = 'absolute', }) => {
|
|
2612
2703
|
if (!elementReference) {
|
|
2613
2704
|
// elementReference can be null or undefined and we will not compute the position
|
|
2614
2705
|
// eslint-disable-next-line no-console
|
|
@@ -2626,21 +2717,20 @@
|
|
|
2626
2717
|
strategy,
|
|
2627
2718
|
middleware,
|
|
2628
2719
|
}).then(({ x, y, placement, middlewareData }) => {
|
|
2720
|
+
var _a, _b;
|
|
2629
2721
|
const styles = { left: `${x}px`, top: `${y}px` };
|
|
2630
|
-
|
|
2631
|
-
const
|
|
2632
|
-
const staticSide = {
|
|
2722
|
+
const { x: arrowX, y: arrowY } = (_a = middlewareData.arrow) !== null && _a !== void 0 ? _a : { x: 0, y: 0 };
|
|
2723
|
+
const staticSide = (_b = {
|
|
2633
2724
|
top: 'bottom',
|
|
2634
2725
|
right: 'left',
|
|
2635
2726
|
bottom: 'top',
|
|
2636
2727
|
left: 'right',
|
|
2637
|
-
}[placement.split('-')[0]];
|
|
2728
|
+
}[placement.split('-')[0]]) !== null && _b !== void 0 ? _b : 'bottom';
|
|
2638
2729
|
const arrowStyle = {
|
|
2639
2730
|
left: arrowX != null ? `${arrowX}px` : '',
|
|
2640
2731
|
top: arrowY != null ? `${arrowY}px` : '',
|
|
2641
2732
|
right: '',
|
|
2642
2733
|
bottom: '',
|
|
2643
|
-
// @ts-ignore
|
|
2644
2734
|
[staticSide]: '-4px',
|
|
2645
2735
|
};
|
|
2646
2736
|
return { tooltipStyles: styles, tooltipArrowStyles: arrowStyle };
|
|
@@ -2660,16 +2750,19 @@
|
|
|
2660
2750
|
|
|
2661
2751
|
const Tooltip = ({
|
|
2662
2752
|
// props
|
|
2663
|
-
id
|
|
2753
|
+
id, className, classNameArrow, variant = 'dark', anchorId, place = 'top', offset = 10, events = ['hover'], positionStrategy = 'absolute', wrapper: WrapperElement = 'div', children = null, delayShow = 0, delayHide = 0, style: externalStyles,
|
|
2664
2754
|
// props handled by controller
|
|
2665
2755
|
isHtmlContent = false, content, isOpen, setIsOpen, }) => {
|
|
2666
|
-
const tooltipRef = require$$0.
|
|
2667
|
-
const tooltipArrowRef = require$$0.
|
|
2668
|
-
const tooltipShowDelayTimerRef = require$$0.useRef();
|
|
2669
|
-
const tooltipHideDelayTimerRef = require$$0.useRef();
|
|
2756
|
+
const tooltipRef = require$$0.useRef(null);
|
|
2757
|
+
const tooltipArrowRef = require$$0.useRef(null);
|
|
2758
|
+
const tooltipShowDelayTimerRef = require$$0.useRef(null);
|
|
2759
|
+
const tooltipHideDelayTimerRef = require$$0.useRef(null);
|
|
2670
2760
|
const [inlineStyles, setInlineStyles] = require$$0.useState({});
|
|
2671
2761
|
const [inlineArrowStyles, setInlineArrowStyles] = require$$0.useState({});
|
|
2672
2762
|
const [show, setShow] = require$$0.useState(false);
|
|
2763
|
+
const [calculatingPosition, setCalculatingPosition] = require$$0.useState(false);
|
|
2764
|
+
const { anchorRefs, setActiveAnchor: setProviderActiveAnchor } = useTooltip()(id);
|
|
2765
|
+
const [activeAnchor, setActiveAnchor] = require$$0.useState({ current: null });
|
|
2673
2766
|
const handleShow = (value) => {
|
|
2674
2767
|
if (setIsOpen) {
|
|
2675
2768
|
setIsOpen(value);
|
|
@@ -2694,13 +2787,18 @@
|
|
|
2694
2787
|
handleShow(false);
|
|
2695
2788
|
}, delayHide);
|
|
2696
2789
|
};
|
|
2697
|
-
const handleShowTooltip = () => {
|
|
2790
|
+
const handleShowTooltip = (event) => {
|
|
2791
|
+
if (!event) {
|
|
2792
|
+
return;
|
|
2793
|
+
}
|
|
2698
2794
|
if (delayShow) {
|
|
2699
2795
|
handleShowTooltipDelayed();
|
|
2700
2796
|
}
|
|
2701
2797
|
else {
|
|
2702
2798
|
handleShow(true);
|
|
2703
2799
|
}
|
|
2800
|
+
setActiveAnchor((anchor) => anchor.current === event.target ? anchor : { current: event.target });
|
|
2801
|
+
setProviderActiveAnchor({ current: event.target });
|
|
2704
2802
|
if (tooltipHideDelayTimerRef.current) {
|
|
2705
2803
|
clearTimeout(tooltipHideDelayTimerRef.current);
|
|
2706
2804
|
}
|
|
@@ -2712,16 +2810,9 @@
|
|
|
2712
2810
|
else {
|
|
2713
2811
|
handleShow(false);
|
|
2714
2812
|
}
|
|
2715
|
-
if (
|
|
2813
|
+
if (tooltipShowDelayTimerRef.current) {
|
|
2716
2814
|
clearTimeout(tooltipShowDelayTimerRef.current);
|
|
2717
2815
|
}
|
|
2718
|
-
else if (!show && tooltipShowDelayTimerRef.current) {
|
|
2719
|
-
// workaround to prevent tooltip being show forever
|
|
2720
|
-
// when we remove the mouse before show tooltip with `delayShow`
|
|
2721
|
-
tooltipHideDelayTimerRef.current = setTimeout(() => {
|
|
2722
|
-
handleShow(false);
|
|
2723
|
-
}, delayShow * 2);
|
|
2724
|
-
}
|
|
2725
2816
|
};
|
|
2726
2817
|
const handleClickTooltipAnchor = () => {
|
|
2727
2818
|
if (setIsOpen) {
|
|
@@ -2736,8 +2827,13 @@
|
|
|
2736
2827
|
const debouncedHandleShowTooltip = debounce(handleShowTooltip, 50);
|
|
2737
2828
|
const debouncedHandleHideTooltip = debounce(handleHideTooltip, 50);
|
|
2738
2829
|
require$$0.useEffect(() => {
|
|
2739
|
-
const
|
|
2740
|
-
|
|
2830
|
+
const elementRefs = new Set(anchorRefs);
|
|
2831
|
+
const anchorById = document.querySelector(`[id='${anchorId}']`);
|
|
2832
|
+
if (anchorById) {
|
|
2833
|
+
setActiveAnchor((anchor) => anchor.current === anchorById ? anchor : { current: anchorById });
|
|
2834
|
+
elementRefs.add({ current: anchorById });
|
|
2835
|
+
}
|
|
2836
|
+
if (!elementRefs.size) {
|
|
2741
2837
|
// eslint-disable-next-line @typescript-eslint/no-empty-function
|
|
2742
2838
|
return () => { };
|
|
2743
2839
|
}
|
|
@@ -2749,17 +2845,29 @@
|
|
|
2749
2845
|
enabledEvents.push({ event: 'mouseenter', listener: debouncedHandleShowTooltip }, { event: 'mouseleave', listener: debouncedHandleHideTooltip }, { event: 'focus', listener: debouncedHandleShowTooltip }, { event: 'blur', listener: debouncedHandleHideTooltip });
|
|
2750
2846
|
}
|
|
2751
2847
|
enabledEvents.forEach(({ event, listener }) => {
|
|
2752
|
-
|
|
2848
|
+
elementRefs.forEach((ref) => {
|
|
2849
|
+
var _a;
|
|
2850
|
+
(_a = ref.current) === null || _a === void 0 ? void 0 : _a.addEventListener(event, listener);
|
|
2851
|
+
});
|
|
2753
2852
|
});
|
|
2754
2853
|
return () => {
|
|
2755
2854
|
enabledEvents.forEach(({ event, listener }) => {
|
|
2756
|
-
|
|
2855
|
+
elementRefs.forEach((ref) => {
|
|
2856
|
+
var _a;
|
|
2857
|
+
(_a = ref.current) === null || _a === void 0 ? void 0 : _a.removeEventListener(event, listener);
|
|
2858
|
+
});
|
|
2757
2859
|
});
|
|
2758
2860
|
};
|
|
2759
|
-
}, [anchorId, events, delayHide, delayShow]);
|
|
2861
|
+
}, [anchorRefs, anchorId, events, delayHide, delayShow]);
|
|
2760
2862
|
require$$0.useEffect(() => {
|
|
2761
|
-
|
|
2762
|
-
|
|
2863
|
+
let elementReference = activeAnchor.current;
|
|
2864
|
+
if (anchorId) {
|
|
2865
|
+
// `anchorId` element takes precedence
|
|
2866
|
+
elementReference = document.querySelector(`[id='${anchorId}']`);
|
|
2867
|
+
}
|
|
2868
|
+
setCalculatingPosition(true);
|
|
2869
|
+
let mounted = true;
|
|
2870
|
+
computeTooltipPosition({
|
|
2763
2871
|
place,
|
|
2764
2872
|
offset,
|
|
2765
2873
|
elementReference,
|
|
@@ -2767,6 +2875,11 @@
|
|
|
2767
2875
|
tooltipArrowReference: tooltipArrowRef.current,
|
|
2768
2876
|
strategy: positionStrategy,
|
|
2769
2877
|
}).then((computedStylesData) => {
|
|
2878
|
+
if (!mounted) {
|
|
2879
|
+
// invalidate computed positions after remount
|
|
2880
|
+
return;
|
|
2881
|
+
}
|
|
2882
|
+
setCalculatingPosition(false);
|
|
2770
2883
|
if (Object.keys(computedStylesData.tooltipStyles).length) {
|
|
2771
2884
|
setInlineStyles(computedStylesData.tooltipStyles);
|
|
2772
2885
|
}
|
|
@@ -2775,29 +2888,26 @@
|
|
|
2775
2888
|
}
|
|
2776
2889
|
});
|
|
2777
2890
|
return () => {
|
|
2778
|
-
|
|
2779
|
-
tooltipHideDelayTimerRef.current = undefined;
|
|
2891
|
+
mounted = false;
|
|
2780
2892
|
};
|
|
2781
|
-
}, [show, isOpen, anchorId]);
|
|
2893
|
+
}, [show, isOpen, anchorId, activeAnchor, place, offset, positionStrategy]);
|
|
2894
|
+
require$$0.useEffect(() => {
|
|
2895
|
+
return () => {
|
|
2896
|
+
if (tooltipShowDelayTimerRef.current) {
|
|
2897
|
+
clearTimeout(tooltipShowDelayTimerRef.current);
|
|
2898
|
+
}
|
|
2899
|
+
if (tooltipHideDelayTimerRef.current) {
|
|
2900
|
+
clearTimeout(tooltipHideDelayTimerRef.current);
|
|
2901
|
+
}
|
|
2902
|
+
};
|
|
2903
|
+
}, []);
|
|
2782
2904
|
return (jsxRuntime.exports.jsxs(WrapperElement, { id: id, role: "tooltip", className: classNames(styles['tooltip'], styles[variant], className, {
|
|
2783
|
-
[styles['show']]: isOpen || show,
|
|
2905
|
+
[styles['show']]: !calculatingPosition && (isOpen || show),
|
|
2784
2906
|
[styles['fixed']]: positionStrategy === 'fixed',
|
|
2785
2907
|
}), style: { ...externalStyles, ...inlineStyles }, ref: tooltipRef, children: [children || (isHtmlContent ? jsxRuntime.exports.jsx(TooltipContent, { content: content }) : content), jsxRuntime.exports.jsx("div", { className: classNames(styles['arrow'], classNameArrow), style: inlineArrowStyles, ref: tooltipArrowRef })] }));
|
|
2786
2908
|
};
|
|
2787
2909
|
|
|
2788
|
-
const
|
|
2789
|
-
'place',
|
|
2790
|
-
'content',
|
|
2791
|
-
'html',
|
|
2792
|
-
'variant',
|
|
2793
|
-
'offset',
|
|
2794
|
-
'wrapper',
|
|
2795
|
-
'events',
|
|
2796
|
-
'delay-show',
|
|
2797
|
-
'delay-hide', // delay to hide tooltip
|
|
2798
|
-
];
|
|
2799
|
-
|
|
2800
|
-
const TooltipController = ({ id, anchorId, content, html, className, classNameArrow, variant = 'dark', place = 'top', offset = 10, wrapper = 'div', children = null, events = ['hover'], positionStrategy = 'absolute', delayShow = 0, delayHide = 0, style, getContent, isOpen, setIsOpen, }) => {
|
|
2910
|
+
const TooltipController = ({ id, anchorId, content, html, className, classNameArrow, variant = 'dark', place = 'top', offset = 10, wrapper = 'div', children = null, events = ['hover'], positionStrategy = 'absolute', delayShow = 0, delayHide = 0, style, isOpen, setIsOpen, }) => {
|
|
2801
2911
|
const [tooltipContent, setTooltipContent] = require$$0.useState(content || html);
|
|
2802
2912
|
const [tooltipPlace, setTooltipPlace] = require$$0.useState(place);
|
|
2803
2913
|
const [tooltipVariant, setTooltipVariant] = require$$0.useState(variant);
|
|
@@ -2808,112 +2918,116 @@
|
|
|
2808
2918
|
const [tooltipEvents, setTooltipEvents] = require$$0.useState(events);
|
|
2809
2919
|
const [tooltipPositionStrategy, setTooltipPositionStrategy] = require$$0.useState(positionStrategy);
|
|
2810
2920
|
const [isHtmlContent, setIsHtmlContent] = require$$0.useState(Boolean(html));
|
|
2921
|
+
const { anchorRefs, activeAnchor } = useTooltip()(id);
|
|
2811
2922
|
const getDataAttributesFromAnchorElement = (elementReference) => {
|
|
2812
2923
|
const dataAttributes = elementReference === null || elementReference === void 0 ? void 0 : elementReference.getAttributeNames().reduce((acc, name) => {
|
|
2813
|
-
|
|
2814
|
-
|
|
2924
|
+
var _a;
|
|
2925
|
+
if (name.startsWith('data-tooltip-')) {
|
|
2926
|
+
const parsedAttribute = name.replace(/^data-tooltip-/, '');
|
|
2927
|
+
acc[parsedAttribute] = (_a = elementReference === null || elementReference === void 0 ? void 0 : elementReference.getAttribute(name)) !== null && _a !== void 0 ? _a : null;
|
|
2815
2928
|
}
|
|
2816
2929
|
return acc;
|
|
2817
2930
|
}, {});
|
|
2818
2931
|
return dataAttributes;
|
|
2819
2932
|
};
|
|
2820
2933
|
const applyAllDataAttributesFromAnchorElement = (dataAttributes) => {
|
|
2821
|
-
const keys = Object.keys(dataAttributes);
|
|
2822
|
-
let formatedKey = null;
|
|
2823
2934
|
const handleDataAttributes = {
|
|
2824
2935
|
place: (value) => {
|
|
2825
|
-
|
|
2936
|
+
var _a;
|
|
2937
|
+
setTooltipPlace((_a = value) !== null && _a !== void 0 ? _a : place);
|
|
2826
2938
|
},
|
|
2827
2939
|
content: (value) => {
|
|
2828
|
-
setIsHtmlContent(
|
|
2829
|
-
|
|
2830
|
-
setTooltipContent(getContent(value));
|
|
2831
|
-
}
|
|
2832
|
-
else {
|
|
2833
|
-
setTooltipContent(value);
|
|
2834
|
-
}
|
|
2940
|
+
setIsHtmlContent(false);
|
|
2941
|
+
setTooltipContent(value !== null && value !== void 0 ? value : content);
|
|
2835
2942
|
},
|
|
2836
2943
|
html: (value) => {
|
|
2837
|
-
|
|
2838
|
-
|
|
2839
|
-
|
|
2840
|
-
}
|
|
2841
|
-
else {
|
|
2842
|
-
setTooltipContent(value);
|
|
2843
|
-
}
|
|
2944
|
+
var _a;
|
|
2945
|
+
setIsHtmlContent(!!value);
|
|
2946
|
+
setTooltipContent((_a = value !== null && value !== void 0 ? value : html) !== null && _a !== void 0 ? _a : content);
|
|
2844
2947
|
},
|
|
2845
2948
|
variant: (value) => {
|
|
2846
|
-
|
|
2949
|
+
var _a;
|
|
2950
|
+
setTooltipVariant((_a = value) !== null && _a !== void 0 ? _a : variant);
|
|
2847
2951
|
},
|
|
2848
2952
|
offset: (value) => {
|
|
2849
|
-
setTooltipOffset(value);
|
|
2953
|
+
setTooltipOffset(value === null ? offset : Number(value));
|
|
2850
2954
|
},
|
|
2851
2955
|
wrapper: (value) => {
|
|
2852
|
-
|
|
2956
|
+
var _a;
|
|
2957
|
+
setTooltipWrapper((_a = value) !== null && _a !== void 0 ? _a : 'div');
|
|
2853
2958
|
},
|
|
2854
2959
|
events: (value) => {
|
|
2855
|
-
const
|
|
2856
|
-
setTooltipEvents(
|
|
2960
|
+
const parsed = value === null || value === void 0 ? void 0 : value.split(' ');
|
|
2961
|
+
setTooltipEvents(parsed !== null && parsed !== void 0 ? parsed : events);
|
|
2857
2962
|
},
|
|
2858
|
-
|
|
2859
|
-
|
|
2963
|
+
'position-strategy': (value) => {
|
|
2964
|
+
var _a;
|
|
2965
|
+
setTooltipPositionStrategy((_a = value) !== null && _a !== void 0 ? _a : positionStrategy);
|
|
2860
2966
|
},
|
|
2861
2967
|
'delay-show': (value) => {
|
|
2862
|
-
setTooltipDelayShow(Number(value));
|
|
2968
|
+
setTooltipDelayShow(value === null ? delayShow : Number(value));
|
|
2863
2969
|
},
|
|
2864
2970
|
'delay-hide': (value) => {
|
|
2865
|
-
setTooltipDelayHide(Number(value));
|
|
2971
|
+
setTooltipDelayHide(value === null ? delayHide : Number(value));
|
|
2866
2972
|
},
|
|
2867
2973
|
};
|
|
2868
|
-
|
|
2869
|
-
|
|
2870
|
-
|
|
2871
|
-
|
|
2872
|
-
|
|
2873
|
-
|
|
2974
|
+
// reset unset data attributes to default values
|
|
2975
|
+
// without this, data attributes from the last active anchor will still be used
|
|
2976
|
+
Object.values(handleDataAttributes).forEach((handler) => handler(null));
|
|
2977
|
+
Object.entries(dataAttributes).forEach(([key, value]) => {
|
|
2978
|
+
var _a;
|
|
2979
|
+
(_a = handleDataAttributes[key]) === null || _a === void 0 ? void 0 : _a.call(handleDataAttributes, value);
|
|
2874
2980
|
});
|
|
2875
2981
|
};
|
|
2876
|
-
const getElementSpecificAttributeKeyAndValueParsed = ({ element, attributeName, }) => {
|
|
2877
|
-
return { [attributeName]: element.getAttribute(attributeName) };
|
|
2878
|
-
};
|
|
2879
2982
|
require$$0.useEffect(() => {
|
|
2880
|
-
if (
|
|
2881
|
-
|
|
2882
|
-
|
|
2983
|
+
if (content) {
|
|
2984
|
+
setTooltipContent(content);
|
|
2985
|
+
}
|
|
2986
|
+
if (html) {
|
|
2987
|
+
setTooltipContent(html);
|
|
2988
|
+
}
|
|
2989
|
+
}, [content, html]);
|
|
2990
|
+
require$$0.useEffect(() => {
|
|
2991
|
+
var _a;
|
|
2992
|
+
const elementRefs = new Set(anchorRefs);
|
|
2993
|
+
const anchorById = document.querySelector(`[id='${anchorId}']`);
|
|
2994
|
+
if (anchorById) {
|
|
2995
|
+
elementRefs.add({ current: anchorById });
|
|
2883
2996
|
}
|
|
2884
|
-
|
|
2885
|
-
if (!elementReference) {
|
|
2997
|
+
if (!elementRefs.size) {
|
|
2886
2998
|
// eslint-disable-next-line @typescript-eslint/no-empty-function
|
|
2887
2999
|
return () => { };
|
|
2888
3000
|
}
|
|
2889
|
-
if (content && getContent) {
|
|
2890
|
-
setTooltipContent(getContent(content));
|
|
2891
|
-
}
|
|
2892
|
-
// do not check for subtree and childrens, we only want to know attribute changes
|
|
2893
|
-
// to stay watching `data-attributes` from anchor element
|
|
2894
|
-
const observerConfig = { attributes: true, childList: false, subtree: false };
|
|
2895
3001
|
const observerCallback = (mutationList) => {
|
|
2896
3002
|
mutationList.forEach((mutation) => {
|
|
2897
|
-
|
|
2898
|
-
|
|
2899
|
-
|
|
2900
|
-
|
|
2901
|
-
|
|
2902
|
-
applyAllDataAttributesFromAnchorElement(attributeKeyAndValue);
|
|
3003
|
+
var _a;
|
|
3004
|
+
if (!activeAnchor.current ||
|
|
3005
|
+
mutation.type !== 'attributes' ||
|
|
3006
|
+
!((_a = mutation.attributeName) === null || _a === void 0 ? void 0 : _a.startsWith('data-tooltip-'))) {
|
|
3007
|
+
return;
|
|
2903
3008
|
}
|
|
3009
|
+
// make sure to get all set attributes, since all unset attributes are reset
|
|
3010
|
+
const dataAttributes = getDataAttributesFromAnchorElement(activeAnchor.current);
|
|
3011
|
+
applyAllDataAttributesFromAnchorElement(dataAttributes);
|
|
2904
3012
|
});
|
|
2905
3013
|
};
|
|
2906
3014
|
// Create an observer instance linked to the callback function
|
|
2907
3015
|
const observer = new MutationObserver(observerCallback);
|
|
2908
|
-
//
|
|
2909
|
-
|
|
2910
|
-
const
|
|
2911
|
-
|
|
3016
|
+
// do not check for subtree and childrens, we only want to know attribute changes
|
|
3017
|
+
// to stay watching `data-attributes-*` from anchor element
|
|
3018
|
+
const observerConfig = { attributes: true, childList: false, subtree: false };
|
|
3019
|
+
const element = (_a = activeAnchor.current) !== null && _a !== void 0 ? _a : anchorById;
|
|
3020
|
+
if (element) {
|
|
3021
|
+
const dataAttributes = getDataAttributesFromAnchorElement(element);
|
|
3022
|
+
applyAllDataAttributesFromAnchorElement(dataAttributes);
|
|
3023
|
+
// Start observing the target node for configured mutations
|
|
3024
|
+
observer.observe(element, observerConfig);
|
|
3025
|
+
}
|
|
2912
3026
|
return () => {
|
|
2913
3027
|
// Remove the observer when the tooltip is destroyed
|
|
2914
3028
|
observer.disconnect();
|
|
2915
3029
|
};
|
|
2916
|
-
}, [anchorId]);
|
|
3030
|
+
}, [anchorRefs, activeAnchor, anchorId]);
|
|
2917
3031
|
const props = {
|
|
2918
3032
|
id,
|
|
2919
3033
|
anchorId,
|
|
@@ -2937,6 +3051,8 @@
|
|
|
2937
3051
|
};
|
|
2938
3052
|
|
|
2939
3053
|
exports.Tooltip = TooltipController;
|
|
3054
|
+
exports.TooltipProvider = TooltipProvider;
|
|
3055
|
+
exports.TooltipWrapper = TooltipWrapper;
|
|
2940
3056
|
|
|
2941
3057
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
2942
3058
|
|