react-tooltip 5.22.0-beta.1108.0 → 5.22.0-beta.1109.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.
@@ -333,9 +333,10 @@ var styles = {"tooltip":"styles-module_tooltip__mnnfp","arrow":"styles-module_ar
333
333
 
334
334
  const Tooltip = ({
335
335
  // props
336
- id, className, classNameArrow, variant = 'dark', anchorId, anchorSelect, place = 'top', offset = 10, events = ['hover'], openOnClick = false, positionStrategy = 'absolute', middlewares, wrapper: WrapperElement, delayShow = 0, delayHide = 0, float = false, hidden = false, noArrow = false, clickable = false, closeOnEsc = false, closeOnScroll = false, closeOnResize = false, openEvents, closeEvents, globalCloseEvents, style: externalStyles, position, afterShow, afterHide,
336
+ forwardRef, id, className, classNameArrow, variant = 'dark', anchorId, anchorSelect, place = 'top', offset = 10, events = ['hover'], openOnClick = false, positionStrategy = 'absolute', middlewares, wrapper: WrapperElement, delayShow = 0, delayHide = 0, float = false, hidden = false, noArrow = false, clickable = false, closeOnEsc = false, closeOnScroll = false, closeOnResize = false, style: externalStyles, position, afterShow, afterHide,
337
337
  // props handled by controller
338
338
  content, contentWrapperRef, isOpen, setIsOpen, activeAnchor, setActiveAnchor, border, opacity, arrowColor, }) => {
339
+ var _a;
339
340
  const tooltipRef = React.useRef(null);
340
341
  const tooltipArrowRef = React.useRef(null);
341
342
  const tooltipShowDelayTimerRef = React.useRef(null);
@@ -345,6 +346,7 @@ content, contentWrapperRef, isOpen, setIsOpen, activeAnchor, setActiveAnchor, bo
345
346
  const [inlineArrowStyles, setInlineArrowStyles] = React.useState({});
346
347
  const [show, setShow] = React.useState(false);
347
348
  const [rendered, setRendered] = React.useState(false);
349
+ const [imperativeOptions, setImperativeOptions] = React.useState(null);
348
350
  const wasShowing = React.useRef(false);
349
351
  const lastFloatPosition = React.useRef(null);
350
352
  /**
@@ -354,48 +356,7 @@ content, contentWrapperRef, isOpen, setIsOpen, activeAnchor, setActiveAnchor, bo
354
356
  const hoveringTooltip = React.useRef(false);
355
357
  const [anchorsBySelect, setAnchorsBySelect] = React.useState([]);
356
358
  const mounted = React.useRef(false);
357
- /**
358
- * @todo Update when deprecated stuff gets removed.
359
- */
360
359
  const shouldOpenOnClick = openOnClick || events.includes('click');
361
- const hasClickEvent = shouldOpenOnClick || (openEvents === null || openEvents === void 0 ? void 0 : openEvents.click) || (openEvents === null || openEvents === void 0 ? void 0 : openEvents.dblclick) || (openEvents === null || openEvents === void 0 ? void 0 : openEvents.mousedown);
362
- const actualOpenEvents = openEvents
363
- ? { ...openEvents }
364
- : {
365
- mouseenter: true,
366
- focus: true,
367
- click: false,
368
- dblclick: false,
369
- mousedown: false,
370
- };
371
- if (!openEvents && shouldOpenOnClick) {
372
- Object.assign(actualOpenEvents, {
373
- mouseenter: false,
374
- focus: false,
375
- click: true,
376
- });
377
- }
378
- const actualCloseEvents = closeEvents
379
- ? { ...closeEvents }
380
- : {
381
- mouseleave: true,
382
- blur: true,
383
- click: false,
384
- };
385
- if (!closeEvents && shouldOpenOnClick) {
386
- Object.assign(actualCloseEvents, {
387
- mouseleave: false,
388
- blur: false,
389
- });
390
- }
391
- const actualGlobalCloseEvents = globalCloseEvents
392
- ? { ...globalCloseEvents }
393
- : {
394
- escape: closeOnEsc || false,
395
- scroll: closeOnScroll || false,
396
- resize: closeOnResize || false,
397
- clickOutsideAnchor: hasClickEvent || false,
398
- };
399
360
  /**
400
361
  * useLayoutEffect runs before useEffect,
401
362
  * but should be used carefully because of caveats
@@ -472,6 +433,7 @@ content, contentWrapperRef, isOpen, setIsOpen, activeAnchor, setActiveAnchor, bo
472
433
  afterShow === null || afterShow === void 0 ? void 0 : afterShow();
473
434
  }
474
435
  else {
436
+ setImperativeOptions(null);
475
437
  afterHide === null || afterHide === void 0 ? void 0 : afterHide();
476
438
  }
477
439
  }, [show]);
@@ -582,8 +544,17 @@ content, contentWrapperRef, isOpen, setIsOpen, activeAnchor, setActiveAnchor, bo
582
544
  handleTooltipPosition(mousePosition);
583
545
  lastFloatPosition.current = mousePosition;
584
546
  };
547
+ const handleClickTooltipAnchor = (event) => {
548
+ handleShowTooltip(event);
549
+ if (delayHide) {
550
+ handleHideTooltipDelayed();
551
+ }
552
+ };
585
553
  const handleClickOutsideAnchors = (event) => {
586
554
  var _a;
555
+ if (!show) {
556
+ return;
557
+ }
587
558
  const anchorById = document.querySelector(`[id='${anchorId}']`);
588
559
  const anchors = [anchorById, ...anchorsBySelect];
589
560
  if (anchors.some((anchor) => anchor === null || anchor === void 0 ? void 0 : anchor.contains(event.target))) {
@@ -602,9 +573,11 @@ content, contentWrapperRef, isOpen, setIsOpen, activeAnchor, setActiveAnchor, bo
602
573
  const debouncedHandleShowTooltip = debounce(handleShowTooltip, 50, true);
603
574
  const debouncedHandleHideTooltip = debounce(handleHideTooltip, 50, true);
604
575
  const updateTooltipPosition = React.useCallback(() => {
605
- if (position) {
576
+ var _a;
577
+ const actualPosition = (_a = imperativeOptions === null || imperativeOptions === void 0 ? void 0 : imperativeOptions.position) !== null && _a !== void 0 ? _a : position;
578
+ if (actualPosition) {
606
579
  // if `position` is set, override regular and `float` positioning
607
- handleTooltipPosition(position);
580
+ handleTooltipPosition(actualPosition);
608
581
  return;
609
582
  }
610
583
  if (float) {
@@ -655,6 +628,7 @@ content, contentWrapperRef, isOpen, setIsOpen, activeAnchor, setActiveAnchor, bo
655
628
  offset,
656
629
  positionStrategy,
657
630
  position,
631
+ imperativeOptions === null || imperativeOptions === void 0 ? void 0 : imperativeOptions.position,
658
632
  float,
659
633
  ]);
660
634
  React.useEffect(() => {
@@ -672,13 +646,13 @@ content, contentWrapperRef, isOpen, setIsOpen, activeAnchor, setActiveAnchor, bo
672
646
  };
673
647
  const anchorScrollParent = getScrollParent(activeAnchor);
674
648
  const tooltipScrollParent = getScrollParent(tooltipRef.current);
675
- if (actualGlobalCloseEvents.scroll) {
649
+ if (closeOnScroll) {
676
650
  window.addEventListener('scroll', handleScrollResize);
677
651
  anchorScrollParent === null || anchorScrollParent === void 0 ? void 0 : anchorScrollParent.addEventListener('scroll', handleScrollResize);
678
652
  tooltipScrollParent === null || tooltipScrollParent === void 0 ? void 0 : tooltipScrollParent.addEventListener('scroll', handleScrollResize);
679
653
  }
680
654
  let updateTooltipCleanup = null;
681
- if (actualGlobalCloseEvents.resize) {
655
+ if (closeOnResize) {
682
656
  window.addEventListener('resize', handleScrollResize);
683
657
  }
684
658
  else if (activeAnchor && tooltipRef.current) {
@@ -694,56 +668,22 @@ content, contentWrapperRef, isOpen, setIsOpen, activeAnchor, setActiveAnchor, bo
694
668
  }
695
669
  handleShow(false);
696
670
  };
697
- if (actualGlobalCloseEvents.escape) {
671
+ if (closeOnEsc) {
698
672
  window.addEventListener('keydown', handleEsc);
699
673
  }
700
- if (actualGlobalCloseEvents.clickOutsideAnchor) {
674
+ const enabledEvents = [];
675
+ if (shouldOpenOnClick) {
701
676
  window.addEventListener('click', handleClickOutsideAnchors);
677
+ enabledEvents.push({ event: 'click', listener: handleClickTooltipAnchor });
702
678
  }
703
- const enabledEvents = [];
704
- const handleClickOpenTooltipAnchor = (event) => {
705
- if (show) {
706
- return;
707
- }
708
- handleShowTooltip(event);
709
- };
710
- const handleClickCloseTooltipAnchor = () => {
711
- if (!show) {
712
- return;
713
- }
714
- handleHideTooltip();
715
- };
716
- const regularEvents = ['mouseenter', 'mouseleave', 'focus', 'blur'];
717
- const clickEvents = ['click', 'dblclick', 'mousedown', 'mouseup'];
718
- Object.entries(actualOpenEvents).forEach(([event, enabled]) => {
719
- if (!enabled) {
720
- return;
721
- }
722
- if (regularEvents.includes(event)) {
723
- enabledEvents.push({ event, listener: debouncedHandleShowTooltip });
724
- }
725
- else if (clickEvents.includes(event)) {
726
- enabledEvents.push({ event, listener: handleClickOpenTooltipAnchor });
727
- }
728
- else ;
729
- });
730
- Object.entries(actualCloseEvents).forEach(([event, enabled]) => {
731
- if (!enabled) {
732
- return;
733
- }
734
- if (regularEvents.includes(event)) {
735
- enabledEvents.push({ event, listener: debouncedHandleHideTooltip });
736
- }
737
- else if (clickEvents.includes(event)) {
738
- enabledEvents.push({ event, listener: handleClickCloseTooltipAnchor });
679
+ else {
680
+ enabledEvents.push({ event: 'mouseenter', listener: debouncedHandleShowTooltip }, { event: 'mouseleave', listener: debouncedHandleHideTooltip }, { event: 'focus', listener: debouncedHandleShowTooltip }, { event: 'blur', listener: debouncedHandleHideTooltip });
681
+ if (float) {
682
+ enabledEvents.push({
683
+ event: 'mousemove',
684
+ listener: handleMouseMove,
685
+ });
739
686
  }
740
- else ;
741
- });
742
- if (float) {
743
- enabledEvents.push({
744
- event: 'mousemove',
745
- listener: handleMouseMove,
746
- });
747
687
  }
748
688
  const handleMouseEnterTooltip = () => {
749
689
  hoveringTooltip.current = true;
@@ -752,9 +692,7 @@ content, contentWrapperRef, isOpen, setIsOpen, activeAnchor, setActiveAnchor, bo
752
692
  hoveringTooltip.current = false;
753
693
  handleHideTooltip();
754
694
  };
755
- if (clickable && !hasClickEvent) {
756
- // used to keep the tooltip open when hovering content.
757
- // not needed if using click events.
695
+ if (clickable && !shouldOpenOnClick) {
758
696
  (_a = tooltipRef.current) === null || _a === void 0 ? void 0 : _a.addEventListener('mouseenter', handleMouseEnterTooltip);
759
697
  (_b = tooltipRef.current) === null || _b === void 0 ? void 0 : _b.addEventListener('mouseleave', handleMouseLeaveTooltip);
760
698
  }
@@ -766,24 +704,24 @@ content, contentWrapperRef, isOpen, setIsOpen, activeAnchor, setActiveAnchor, bo
766
704
  });
767
705
  return () => {
768
706
  var _a, _b;
769
- if (actualGlobalCloseEvents.scroll) {
707
+ if (closeOnScroll) {
770
708
  window.removeEventListener('scroll', handleScrollResize);
771
709
  anchorScrollParent === null || anchorScrollParent === void 0 ? void 0 : anchorScrollParent.removeEventListener('scroll', handleScrollResize);
772
710
  tooltipScrollParent === null || tooltipScrollParent === void 0 ? void 0 : tooltipScrollParent.removeEventListener('scroll', handleScrollResize);
773
711
  }
774
- if (actualGlobalCloseEvents.resize) {
712
+ if (closeOnResize) {
775
713
  window.removeEventListener('resize', handleScrollResize);
776
714
  }
777
715
  else {
778
716
  updateTooltipCleanup === null || updateTooltipCleanup === void 0 ? void 0 : updateTooltipCleanup();
779
717
  }
780
- if (actualGlobalCloseEvents.clickOutsideAnchor) {
718
+ if (shouldOpenOnClick) {
781
719
  window.removeEventListener('click', handleClickOutsideAnchors);
782
720
  }
783
- if (actualGlobalCloseEvents.escape) {
721
+ if (closeOnEsc) {
784
722
  window.removeEventListener('keydown', handleEsc);
785
723
  }
786
- if (clickable && !hasClickEvent) {
724
+ if (clickable && !shouldOpenOnClick) {
787
725
  (_a = tooltipRef.current) === null || _a === void 0 ? void 0 : _a.removeEventListener('mouseenter', handleMouseEnterTooltip);
788
726
  (_b = tooltipRef.current) === null || _b === void 0 ? void 0 : _b.removeEventListener('mouseleave', handleMouseLeaveTooltip);
789
727
  }
@@ -804,14 +742,12 @@ content, contentWrapperRef, isOpen, setIsOpen, activeAnchor, setActiveAnchor, bo
804
742
  rendered,
805
743
  anchorRefs,
806
744
  anchorsBySelect,
807
- // the effect uses the `actual*Events` objects, but this should work
808
- openEvents,
809
- closeEvents,
810
- globalCloseEvents,
811
- shouldOpenOnClick,
745
+ closeOnEsc,
746
+ events,
812
747
  ]);
813
748
  React.useEffect(() => {
814
- let selector = anchorSelect !== null && anchorSelect !== void 0 ? anchorSelect : '';
749
+ var _a, _b;
750
+ let selector = (_b = (_a = imperativeOptions === null || imperativeOptions === void 0 ? void 0 : imperativeOptions.anchorSelect) !== null && _a !== void 0 ? _a : anchorSelect) !== null && _b !== void 0 ? _b : '';
815
751
  if (!selector && id) {
816
752
  selector = `[data-tooltip-id='${id}']`;
817
753
  }
@@ -900,7 +836,7 @@ content, contentWrapperRef, isOpen, setIsOpen, activeAnchor, setActiveAnchor, bo
900
836
  return () => {
901
837
  documentObserver.disconnect();
902
838
  };
903
- }, [id, anchorSelect, activeAnchor]);
839
+ }, [id, anchorSelect, imperativeOptions === null || imperativeOptions === void 0 ? void 0 : imperativeOptions.anchorSelect, activeAnchor]);
904
840
  React.useEffect(() => {
905
841
  updateTooltipPosition();
906
842
  }, [updateTooltipPosition]);
@@ -940,7 +876,8 @@ content, contentWrapperRef, isOpen, setIsOpen, activeAnchor, setActiveAnchor, bo
940
876
  };
941
877
  }, []);
942
878
  React.useEffect(() => {
943
- let selector = anchorSelect;
879
+ var _a;
880
+ let selector = (_a = imperativeOptions === null || imperativeOptions === void 0 ? void 0 : imperativeOptions.anchorSelect) !== null && _a !== void 0 ? _a : anchorSelect;
944
881
  if (!selector && id) {
945
882
  selector = `[data-tooltip-id='${id}']`;
946
883
  }
@@ -951,12 +888,37 @@ content, contentWrapperRef, isOpen, setIsOpen, activeAnchor, setActiveAnchor, bo
951
888
  const anchors = Array.from(document.querySelectorAll(selector));
952
889
  setAnchorsBySelect(anchors);
953
890
  }
954
- catch (_a) {
891
+ catch (_b) {
955
892
  // warning was already issued in the controller
956
893
  setAnchorsBySelect([]);
957
894
  }
958
- }, [id, anchorSelect]);
959
- const canShow = !hidden && content && show && Object.keys(inlineStyles).length > 0;
895
+ }, [id, anchorSelect, imperativeOptions === null || imperativeOptions === void 0 ? void 0 : imperativeOptions.anchorSelect]);
896
+ const actualContent = (_a = imperativeOptions === null || imperativeOptions === void 0 ? void 0 : imperativeOptions.content) !== null && _a !== void 0 ? _a : content;
897
+ const canShow = Boolean(!hidden && actualContent && show && Object.keys(inlineStyles).length > 0);
898
+ React.useImperativeHandle(forwardRef, () => ({
899
+ open: (options) => {
900
+ if (options === null || options === void 0 ? void 0 : options.anchorSelect) {
901
+ try {
902
+ document.querySelector(options.anchorSelect);
903
+ }
904
+ catch (_a) {
905
+ {
906
+ // eslint-disable-next-line no-console
907
+ console.warn(`[react-tooltip] "${options.anchorSelect}" is not a valid CSS selector`);
908
+ }
909
+ return;
910
+ }
911
+ }
912
+ setImperativeOptions(options !== null && options !== void 0 ? options : null);
913
+ handleShow(true);
914
+ },
915
+ close: () => {
916
+ handleShow(false);
917
+ },
918
+ activeAnchor,
919
+ place: actualPlacement,
920
+ isOpen: rendered && canShow,
921
+ }));
960
922
  return rendered ? (React__default["default"].createElement(WrapperElement, { id: id, role: "tooltip", className: classNames__default["default"]('react-tooltip', coreStyles['tooltip'], styles['tooltip'], styles[variant], className, `react-tooltip__place-${actualPlacement}`, {
961
923
  'react-tooltip__show': canShow,
962
924
  [coreStyles['show']]: canShow,
@@ -967,7 +929,7 @@ content, contentWrapperRef, isOpen, setIsOpen, activeAnchor, setActiveAnchor, bo
967
929
  ...inlineStyles,
968
930
  opacity: opacity !== undefined && canShow ? opacity : undefined,
969
931
  }, ref: tooltipRef },
970
- content,
932
+ actualContent,
971
933
  React__default["default"].createElement(WrapperElement, { className: classNames__default["default"]('react-tooltip-arrow', coreStyles['arrow'], styles['arrow'], classNameArrow, {
972
934
  /**
973
935
  * changed from dash `no-arrow` to camelcase because of:
@@ -987,7 +949,7 @@ const TooltipContent = ({ content }) => {
987
949
  return React__default["default"].createElement("span", { dangerouslySetInnerHTML: { __html: content } });
988
950
  };
989
951
 
990
- const TooltipController = ({ id, anchorId, anchorSelect, content, html, render, className, classNameArrow, variant = 'dark', place = 'top', offset = 10, wrapper = 'div', children = null, events = ['hover'], openOnClick = false, positionStrategy = 'absolute', middlewares, delayShow = 0, delayHide = 0, float = false, hidden = false, noArrow = false, clickable = false, closeOnEsc = false, closeOnScroll = false, closeOnResize = false, openEvents, closeEvents, globalCloseEvents, style, position, isOpen, disableStyleInjection = false, border, opacity, arrowColor, setIsOpen, afterShow, afterHide, }) => {
952
+ const TooltipController = React__default["default"].forwardRef(({ id, anchorId, anchorSelect, content, html, render, className, classNameArrow, variant = 'dark', place = 'top', offset = 10, wrapper = 'div', children = null, events = ['hover'], openOnClick = false, positionStrategy = 'absolute', middlewares, delayShow = 0, delayHide = 0, float = false, hidden = false, noArrow = false, clickable = false, closeOnEsc = false, closeOnScroll = false, closeOnResize = false, style, position, isOpen, disableStyleInjection = false, border, opacity, arrowColor, setIsOpen, afterShow, afterHide, }, ref) => {
991
953
  const [tooltipContent, setTooltipContent] = React.useState(content);
992
954
  const [tooltipHtml, setTooltipHtml] = React.useState(html);
993
955
  const [tooltipPlace, setTooltipPlace] = React.useState(place);
@@ -1211,6 +1173,7 @@ const TooltipController = ({ id, anchorId, anchorSelect, content, html, render,
1211
1173
  renderedContent = React__default["default"].createElement(TooltipContent, { content: tooltipHtml });
1212
1174
  }
1213
1175
  const props = {
1176
+ forwardRef: ref,
1214
1177
  id,
1215
1178
  anchorId,
1216
1179
  anchorSelect,
@@ -1235,9 +1198,6 @@ const TooltipController = ({ id, anchorId, anchorSelect, content, html, render,
1235
1198
  closeOnEsc,
1236
1199
  closeOnScroll,
1237
1200
  closeOnResize,
1238
- openEvents,
1239
- closeEvents,
1240
- globalCloseEvents,
1241
1201
  style,
1242
1202
  position,
1243
1203
  isOpen,
@@ -1251,7 +1211,7 @@ const TooltipController = ({ id, anchorId, anchorSelect, content, html, render,
1251
1211
  setActiveAnchor: (anchor) => setActiveAnchor(anchor),
1252
1212
  };
1253
1213
  return React__default["default"].createElement(Tooltip, { ...props });
1254
- };
1214
+ });
1255
1215
 
1256
1216
  // those content will be replaced in build time with the `react-tooltip.css` builded content
1257
1217
  const TooltipCoreStyles = `:root {