react-tooltip 5.22.0 → 5.23.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, openEvents, closeEvents, globalCloseEvents, imperativeModeOnly, 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
  /**
@@ -381,6 +383,8 @@ content, contentWrapperRef, isOpen, setIsOpen, activeAnchor, setActiveAnchor, bo
381
383
  mouseleave: true,
382
384
  blur: true,
383
385
  click: false,
386
+ dblclick: false,
387
+ mouseup: false,
384
388
  };
385
389
  if (!closeEvents && shouldOpenOnClick) {
386
390
  Object.assign(actualCloseEvents, {
@@ -396,6 +400,28 @@ content, contentWrapperRef, isOpen, setIsOpen, activeAnchor, setActiveAnchor, bo
396
400
  resize: closeOnResize || false,
397
401
  clickOutsideAnchor: hasClickEvent || false,
398
402
  };
403
+ if (imperativeModeOnly) {
404
+ Object.assign(actualOpenEvents, {
405
+ mouseenter: false,
406
+ focus: false,
407
+ click: false,
408
+ dblclick: false,
409
+ mousedown: false,
410
+ });
411
+ Object.assign(actualCloseEvents, {
412
+ mouseleave: false,
413
+ blur: false,
414
+ click: false,
415
+ dblclick: false,
416
+ mouseup: false,
417
+ });
418
+ Object.assign(actualGlobalCloseEvents, {
419
+ escape: false,
420
+ scroll: false,
421
+ resize: false,
422
+ clickOutsideAnchor: false,
423
+ });
424
+ }
399
425
  /**
400
426
  * useLayoutEffect runs before useEffect,
401
427
  * but should be used carefully because of caveats
@@ -454,17 +480,14 @@ content, contentWrapperRef, isOpen, setIsOpen, activeAnchor, setActiveAnchor, bo
454
480
  if (show) {
455
481
  afterShow === null || afterShow === void 0 ? void 0 : afterShow();
456
482
  }
457
- else {
458
- afterHide === null || afterHide === void 0 ? void 0 : afterHide();
459
- }
460
483
  }, [show]);
461
- const handleShowTooltipDelayed = () => {
484
+ const handleShowTooltipDelayed = (delay = delayShow) => {
462
485
  if (tooltipShowDelayTimerRef.current) {
463
486
  clearTimeout(tooltipShowDelayTimerRef.current);
464
487
  }
465
488
  tooltipShowDelayTimerRef.current = setTimeout(() => {
466
489
  handleShow(true);
467
- }, delayShow);
490
+ }, delay);
468
491
  };
469
492
  const handleHideTooltipDelayed = (delay = delayHide) => {
470
493
  if (tooltipHideDelayTimerRef.current) {
@@ -520,6 +543,7 @@ content, contentWrapperRef, isOpen, setIsOpen, activeAnchor, setActiveAnchor, bo
520
543
  }
521
544
  };
522
545
  const handleTooltipPosition = ({ x, y }) => {
546
+ var _a;
523
547
  const virtualElement = {
524
548
  getBoundingClientRect() {
525
549
  return {
@@ -535,7 +559,7 @@ content, contentWrapperRef, isOpen, setIsOpen, activeAnchor, setActiveAnchor, bo
535
559
  },
536
560
  };
537
561
  computeTooltipPosition({
538
- place,
562
+ place: (_a = imperativeOptions === null || imperativeOptions === void 0 ? void 0 : imperativeOptions.place) !== null && _a !== void 0 ? _a : place,
539
563
  offset,
540
564
  elementReference: virtualElement,
541
565
  tooltipReference: tooltipRef.current,
@@ -567,12 +591,16 @@ content, contentWrapperRef, isOpen, setIsOpen, activeAnchor, setActiveAnchor, bo
567
591
  };
568
592
  const handleClickOutsideAnchors = (event) => {
569
593
  var _a;
570
- const anchorById = document.querySelector(`[id='${anchorId}']`);
571
- const anchors = [anchorById, ...anchorsBySelect];
572
- if (anchors.some((anchor) => anchor === null || anchor === void 0 ? void 0 : anchor.contains(event.target))) {
594
+ if (!show) {
595
+ return;
596
+ }
597
+ const target = event.target;
598
+ if ((_a = tooltipRef.current) === null || _a === void 0 ? void 0 : _a.contains(target)) {
573
599
  return;
574
600
  }
575
- if ((_a = tooltipRef.current) === null || _a === void 0 ? void 0 : _a.contains(event.target)) {
601
+ const anchorById = document.querySelector(`[id='${anchorId}']`);
602
+ const anchors = [anchorById, ...anchorsBySelect];
603
+ if (anchors.some((anchor) => anchor === null || anchor === void 0 ? void 0 : anchor.contains(target))) {
576
604
  return;
577
605
  }
578
606
  handleShow(false);
@@ -585,9 +613,11 @@ content, contentWrapperRef, isOpen, setIsOpen, activeAnchor, setActiveAnchor, bo
585
613
  const debouncedHandleShowTooltip = debounce(handleShowTooltip, 50, true);
586
614
  const debouncedHandleHideTooltip = debounce(handleHideTooltip, 50, true);
587
615
  const updateTooltipPosition = React.useCallback(() => {
588
- if (position) {
616
+ var _a, _b;
617
+ const actualPosition = (_a = imperativeOptions === null || imperativeOptions === void 0 ? void 0 : imperativeOptions.position) !== null && _a !== void 0 ? _a : position;
618
+ if (actualPosition) {
589
619
  // if `position` is set, override regular and `float` positioning
590
- handleTooltipPosition(position);
620
+ handleTooltipPosition(actualPosition);
591
621
  return;
592
622
  }
593
623
  if (float) {
@@ -608,7 +638,7 @@ content, contentWrapperRef, isOpen, setIsOpen, activeAnchor, setActiveAnchor, bo
608
638
  return;
609
639
  }
610
640
  computeTooltipPosition({
611
- place,
641
+ place: (_b = imperativeOptions === null || imperativeOptions === void 0 ? void 0 : imperativeOptions.place) !== null && _b !== void 0 ? _b : place,
612
642
  offset,
613
643
  elementReference: activeAnchor,
614
644
  tooltipReference: tooltipRef.current,
@@ -635,9 +665,11 @@ content, contentWrapperRef, isOpen, setIsOpen, activeAnchor, setActiveAnchor, bo
635
665
  content,
636
666
  externalStyles,
637
667
  place,
668
+ imperativeOptions === null || imperativeOptions === void 0 ? void 0 : imperativeOptions.place,
638
669
  offset,
639
670
  positionStrategy,
640
671
  position,
672
+ imperativeOptions === null || imperativeOptions === void 0 ? void 0 : imperativeOptions.position,
641
673
  float,
642
674
  ]);
643
675
  React.useEffect(() => {
@@ -794,7 +826,8 @@ content, contentWrapperRef, isOpen, setIsOpen, activeAnchor, setActiveAnchor, bo
794
826
  shouldOpenOnClick,
795
827
  ]);
796
828
  React.useEffect(() => {
797
- let selector = anchorSelect !== null && anchorSelect !== void 0 ? anchorSelect : '';
829
+ var _a, _b;
830
+ let selector = (_b = (_a = imperativeOptions === null || imperativeOptions === void 0 ? void 0 : imperativeOptions.anchorSelect) !== null && _a !== void 0 ? _a : anchorSelect) !== null && _b !== void 0 ? _b : '';
798
831
  if (!selector && id) {
799
832
  selector = `[data-tooltip-id='${id}']`;
800
833
  }
@@ -883,7 +916,7 @@ content, contentWrapperRef, isOpen, setIsOpen, activeAnchor, setActiveAnchor, bo
883
916
  return () => {
884
917
  documentObserver.disconnect();
885
918
  };
886
- }, [id, anchorSelect, activeAnchor]);
919
+ }, [id, anchorSelect, imperativeOptions === null || imperativeOptions === void 0 ? void 0 : imperativeOptions.anchorSelect, activeAnchor]);
887
920
  React.useEffect(() => {
888
921
  updateTooltipPosition();
889
922
  }, [updateTooltipPosition]);
@@ -923,7 +956,8 @@ content, contentWrapperRef, isOpen, setIsOpen, activeAnchor, setActiveAnchor, bo
923
956
  };
924
957
  }, []);
925
958
  React.useEffect(() => {
926
- let selector = anchorSelect;
959
+ var _a;
960
+ let selector = (_a = imperativeOptions === null || imperativeOptions === void 0 ? void 0 : imperativeOptions.anchorSelect) !== null && _a !== void 0 ? _a : anchorSelect;
927
961
  if (!selector && id) {
928
962
  selector = `[data-tooltip-id='${id}']`;
929
963
  }
@@ -934,12 +968,47 @@ content, contentWrapperRef, isOpen, setIsOpen, activeAnchor, setActiveAnchor, bo
934
968
  const anchors = Array.from(document.querySelectorAll(selector));
935
969
  setAnchorsBySelect(anchors);
936
970
  }
937
- catch (_a) {
971
+ catch (_b) {
938
972
  // warning was already issued in the controller
939
973
  setAnchorsBySelect([]);
940
974
  }
941
- }, [id, anchorSelect]);
942
- const canShow = !hidden && content && show && Object.keys(inlineStyles).length > 0;
975
+ }, [id, anchorSelect, imperativeOptions === null || imperativeOptions === void 0 ? void 0 : imperativeOptions.anchorSelect]);
976
+ const actualContent = (_a = imperativeOptions === null || imperativeOptions === void 0 ? void 0 : imperativeOptions.content) !== null && _a !== void 0 ? _a : content;
977
+ const canShow = Boolean(!hidden && actualContent && show && Object.keys(inlineStyles).length > 0);
978
+ React.useImperativeHandle(forwardRef, () => ({
979
+ open: (options) => {
980
+ if (options === null || options === void 0 ? void 0 : options.anchorSelect) {
981
+ try {
982
+ document.querySelector(options.anchorSelect);
983
+ }
984
+ catch (_a) {
985
+ {
986
+ // eslint-disable-next-line no-console
987
+ console.warn(`[react-tooltip] "${options.anchorSelect}" is not a valid CSS selector`);
988
+ }
989
+ return;
990
+ }
991
+ }
992
+ setImperativeOptions(options !== null && options !== void 0 ? options : null);
993
+ if (options === null || options === void 0 ? void 0 : options.delay) {
994
+ handleShowTooltipDelayed(options.delay);
995
+ }
996
+ else {
997
+ handleShow(true);
998
+ }
999
+ },
1000
+ close: (options) => {
1001
+ if (options === null || options === void 0 ? void 0 : options.delay) {
1002
+ handleHideTooltipDelayed(options.delay);
1003
+ }
1004
+ else {
1005
+ handleShow(false);
1006
+ }
1007
+ },
1008
+ activeAnchor,
1009
+ place: actualPlacement,
1010
+ isOpen: rendered && canShow,
1011
+ }));
943
1012
  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}`, coreStyles[canShow ? 'show' : 'closing'], canShow ? 'react-tooltip__show' : 'react-tooltip__closing', positionStrategy === 'fixed' && coreStyles['fixed'], clickable && coreStyles['clickable']), onTransitionEnd: (event) => {
944
1013
  /**
945
1014
  * @warning if `--rt-transition-closing-delay` is set to 0,
@@ -949,12 +1018,14 @@ content, contentWrapperRef, isOpen, setIsOpen, activeAnchor, setActiveAnchor, bo
949
1018
  return;
950
1019
  }
951
1020
  setRendered(false);
1021
+ setImperativeOptions(null);
1022
+ afterHide === null || afterHide === void 0 ? void 0 : afterHide();
952
1023
  }, style: {
953
1024
  ...externalStyles,
954
1025
  ...inlineStyles,
955
1026
  opacity: opacity !== undefined && canShow ? opacity : undefined,
956
1027
  }, ref: tooltipRef },
957
- content,
1028
+ actualContent,
958
1029
  React__default["default"].createElement(WrapperElement, { className: classNames__default["default"]('react-tooltip-arrow', coreStyles['arrow'], styles['arrow'], classNameArrow, noArrow && coreStyles['noArrow']), style: {
959
1030
  ...inlineArrowStyles,
960
1031
  background: arrowColor
@@ -968,7 +1039,7 @@ const TooltipContent = ({ content }) => {
968
1039
  return React__default["default"].createElement("span", { dangerouslySetInnerHTML: { __html: content } });
969
1040
  };
970
1041
 
971
- 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, }) => {
1042
+ 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, openEvents, closeEvents, globalCloseEvents, imperativeModeOnly = false, style, position, isOpen, disableStyleInjection = false, border, opacity, arrowColor, setIsOpen, afterShow, afterHide, }, ref) => {
972
1043
  const [tooltipContent, setTooltipContent] = React.useState(content);
973
1044
  const [tooltipHtml, setTooltipHtml] = React.useState(html);
974
1045
  const [tooltipPlace, setTooltipPlace] = React.useState(place);
@@ -1192,6 +1263,7 @@ const TooltipController = ({ id, anchorId, anchorSelect, content, html, render,
1192
1263
  renderedContent = React__default["default"].createElement(TooltipContent, { content: tooltipHtml });
1193
1264
  }
1194
1265
  const props = {
1266
+ forwardRef: ref,
1195
1267
  id,
1196
1268
  anchorId,
1197
1269
  anchorSelect,
@@ -1219,6 +1291,7 @@ const TooltipController = ({ id, anchorId, anchorSelect, content, html, render,
1219
1291
  openEvents,
1220
1292
  closeEvents,
1221
1293
  globalCloseEvents,
1294
+ imperativeModeOnly,
1222
1295
  style,
1223
1296
  position,
1224
1297
  isOpen,
@@ -1232,7 +1305,7 @@ const TooltipController = ({ id, anchorId, anchorSelect, content, html, render,
1232
1305
  setActiveAnchor: (anchor) => setActiveAnchor(anchor),
1233
1306
  };
1234
1307
  return React__default["default"].createElement(Tooltip, { ...props });
1235
- };
1308
+ });
1236
1309
 
1237
1310
  // those content will be replaced in build time with the `react-tooltip.css` builded content
1238
1311
  const TooltipCoreStyles = `:root {