react-tooltip 6.0.0-beta.1179.rc.17 → 6.0.0-beta.1179.rc.19

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.
@@ -84,13 +84,10 @@ function injectStyle({ css, id = REACT_TOOLTIP_BASE_STYLES_ID, type = 'base', re
84
84
  }
85
85
  }
86
86
 
87
- const computeTooltipPosition = async ({ elementReference = null, tooltipReference = null, tooltipArrowReference = null, place = 'top', offset: offsetValue = 10, strategy = 'absolute', middlewares = [
88
- dom.offset(Number(offsetValue)),
89
- dom.flip({
90
- fallbackAxisSideDirection: 'start',
91
- }),
92
- dom.shift({ padding: 5 }),
93
- ], border, arrowSize = 8, }) => {
87
+ // Hoisted constant middlewares these configs never change
88
+ const defaultFlip = dom.flip({ fallbackAxisSideDirection: 'start' });
89
+ const defaultShift = dom.shift({ padding: 5 });
90
+ const computeTooltipPosition = async ({ elementReference = null, tooltipReference = null, tooltipArrowReference = null, place = 'top', offset: offsetValue = 10, strategy = 'absolute', middlewares = [dom.offset(Number(offsetValue)), defaultFlip, defaultShift], border, arrowSize = 8, }) => {
94
91
  if (!elementReference) {
95
92
  // elementReference can be null or undefined and we will not compute the position
96
93
  // console.error('The reference element for tooltip was not defined: ', elementReference)
@@ -177,6 +174,7 @@ const cssTimeToMs = (time) => {
177
174
  */
178
175
  const debounce = (func, wait, immediate) => {
179
176
  let timeout = null;
177
+ let currentFunc = func;
180
178
  const debounced = function debounced(...args) {
181
179
  const later = () => {
182
180
  timeout = null;
@@ -186,7 +184,7 @@ const debounce = (func, wait, immediate) => {
186
184
  * there's no need to clear the timeout
187
185
  * since we expect it to resolve and set `timeout = null`
188
186
  */
189
- func.apply(this, args);
187
+ currentFunc.apply(this, args);
190
188
  timeout = setTimeout(later, wait);
191
189
  }
192
190
  };
@@ -199,36 +197,12 @@ const debounce = (func, wait, immediate) => {
199
197
  clearTimeout(timeout);
200
198
  timeout = null;
201
199
  };
200
+ debounced.setCallback = (newFunc) => {
201
+ currentFunc = newFunc;
202
+ };
202
203
  return debounced;
203
204
  };
204
205
 
205
- const isObject = (object) => {
206
- return object !== null && !Array.isArray(object) && typeof object === 'object';
207
- };
208
- const deepEqual = (object1, object2) => {
209
- if (object1 === object2) {
210
- return true;
211
- }
212
- if (Array.isArray(object1) && Array.isArray(object2)) {
213
- if (object1.length !== object2.length) {
214
- return false;
215
- }
216
- return object1.every((val, index) => deepEqual(val, object2[index]));
217
- }
218
- if (Array.isArray(object1) !== Array.isArray(object2)) {
219
- return false;
220
- }
221
- if (!isObject(object1) || !isObject(object2)) {
222
- return object1 === object2;
223
- }
224
- const keys1 = Object.keys(object1);
225
- const keys2 = Object.keys(object2);
226
- if (keys1.length !== keys2.length) {
227
- return false;
228
- }
229
- return keys1.every((key) => deepEqual(object1[key], object2[key]));
230
- };
231
-
232
206
  const isScrollable = (node) => {
233
207
  if (!(node instanceof HTMLElement || node instanceof SVGElement)) {
234
208
  return false;
@@ -298,6 +272,14 @@ var styles = {"tooltip":"styles-module_tooltip__mnnfp","content":"styles-module_
298
272
 
299
273
  const registry = new Map();
300
274
  let documentObserver = null;
275
+ /**
276
+ * Extract a tooltip ID from a simple `[data-tooltip-id='value']` selector.
277
+ * Returns null for complex or custom selectors.
278
+ */
279
+ function extractTooltipId(selector) {
280
+ const match = selector.match(/^\[data-tooltip-id=(['"])((?:\\.|(?!\1).)*)\1\]$/);
281
+ return match ? match[2].replace(/\\(['"])/g, '$1') : null;
282
+ }
301
283
  function areAnchorListsEqual(left, right) {
302
284
  if (left.length !== right.length) {
303
285
  return false;
@@ -319,8 +301,7 @@ function readAnchorsForSelector(selector) {
319
301
  }
320
302
  }
321
303
  function notifySubscribers(entry) {
322
- const anchors = [...entry.anchors];
323
- entry.subscribers.forEach((subscriber) => subscriber(anchors, entry.error));
304
+ entry.subscribers.forEach((subscriber) => subscriber(entry.anchors, entry.error));
324
305
  }
325
306
  function refreshEntry(selector, entry) {
326
307
  var _a, _b, _c, _d;
@@ -344,12 +325,117 @@ function refreshAllEntries() {
344
325
  refreshEntry(selector, entry);
345
326
  });
346
327
  }
328
+ let refreshScheduled = false;
329
+ let pendingTooltipIds = null;
330
+ let pendingFullRefresh = false;
331
+ function scheduleRefresh(affectedTooltipIds) {
332
+ if (affectedTooltipIds) {
333
+ if (!pendingTooltipIds) {
334
+ pendingTooltipIds = new Set();
335
+ }
336
+ affectedTooltipIds.forEach((id) => pendingTooltipIds.add(id));
337
+ }
338
+ else {
339
+ pendingFullRefresh = true;
340
+ }
341
+ if (refreshScheduled) {
342
+ return;
343
+ }
344
+ refreshScheduled = true;
345
+ const flush = () => {
346
+ refreshScheduled = false;
347
+ const fullRefresh = pendingFullRefresh;
348
+ const ids = pendingTooltipIds;
349
+ pendingFullRefresh = false;
350
+ pendingTooltipIds = null;
351
+ if (fullRefresh) {
352
+ refreshAllEntries();
353
+ }
354
+ else if (ids && ids.size > 0) {
355
+ refreshEntriesForTooltipIds(ids);
356
+ }
357
+ };
358
+ if (typeof requestAnimationFrame === 'function') {
359
+ requestAnimationFrame(flush);
360
+ }
361
+ else {
362
+ Promise.resolve().then(flush);
363
+ }
364
+ }
365
+ /**
366
+ * Only refresh entries whose tooltipId is in the affected set,
367
+ * plus any entries with custom (non-tooltipId) selectors.
368
+ */
369
+ function refreshEntriesForTooltipIds(affectedIds) {
370
+ registry.forEach((entry, selector) => {
371
+ if (entry.tooltipId === null || affectedIds.has(entry.tooltipId)) {
372
+ refreshEntry(selector, entry);
373
+ }
374
+ });
375
+ }
376
+ /**
377
+ * Collect tooltip IDs from mutation records. Returns null when targeted
378
+ * analysis is not worthwhile (few registry entries, or too many nodes to scan).
379
+ */
380
+ function collectAffectedTooltipIds(records) {
381
+ var _a;
382
+ // Targeted refresh only pays off when there are many distinct selectors.
383
+ // With few entries, full refresh is already cheap — skip the analysis overhead.
384
+ if (registry.size <= 4) {
385
+ return null;
386
+ }
387
+ const ids = new Set();
388
+ for (const record of records) {
389
+ if (record.type === 'attributes') {
390
+ const target = record.target;
391
+ const currentId = (_a = target.getAttribute) === null || _a === void 0 ? void 0 : _a.call(target, 'data-tooltip-id');
392
+ if (currentId)
393
+ ids.add(currentId);
394
+ if (record.oldValue)
395
+ ids.add(record.oldValue);
396
+ continue;
397
+ }
398
+ if (record.type === 'childList') {
399
+ const gatherIds = (nodes) => {
400
+ var _a, _b;
401
+ for (let i = 0; i < nodes.length; i++) {
402
+ const node = nodes[i];
403
+ if (node.nodeType !== Node.ELEMENT_NODE)
404
+ continue;
405
+ const el = node;
406
+ const id = (_a = el.getAttribute) === null || _a === void 0 ? void 0 : _a.call(el, 'data-tooltip-id');
407
+ if (id)
408
+ ids.add(id);
409
+ // For large subtrees, bail out to full refresh to avoid double-scanning
410
+ const descendants = (_b = el.querySelectorAll) === null || _b === void 0 ? void 0 : _b.call(el, '[data-tooltip-id]');
411
+ if (descendants) {
412
+ if (descendants.length > 50) {
413
+ return true; // signal bail-out
414
+ }
415
+ for (let j = 0; j < descendants.length; j++) {
416
+ const descId = descendants[j].getAttribute('data-tooltip-id');
417
+ if (descId)
418
+ ids.add(descId);
419
+ }
420
+ }
421
+ }
422
+ return false;
423
+ };
424
+ if (gatherIds(record.addedNodes) || gatherIds(record.removedNodes)) {
425
+ return null; // large mutation — full refresh is cheaper
426
+ }
427
+ continue;
428
+ }
429
+ }
430
+ return ids;
431
+ }
347
432
  function ensureDocumentObserver() {
348
433
  if (documentObserver || typeof MutationObserver === 'undefined') {
349
434
  return;
350
435
  }
351
- documentObserver = new MutationObserver(() => {
352
- refreshAllEntries();
436
+ documentObserver = new MutationObserver((records) => {
437
+ const affectedIds = collectAffectedTooltipIds(records);
438
+ scheduleRefresh(affectedIds);
353
439
  });
354
440
  documentObserver.observe(document.body, {
355
441
  childList: true,
@@ -374,6 +460,7 @@ function subscribeAnchorSelector(selector, subscriber) {
374
460
  anchors: initialState.anchors,
375
461
  error: initialState.error,
376
462
  subscribers: new Set(),
463
+ tooltipId: extractTooltipId(selector),
377
464
  };
378
465
  registry.set(selector, entry);
379
466
  }
@@ -455,141 +542,75 @@ const useTooltipAnchors = ({ id, anchorSelect, imperativeAnchorSelect, activeAnc
455
542
  };
456
543
  };
457
544
 
545
+ /**
546
+ * Shared document event delegation.
547
+ *
548
+ * Instead of N tooltips each calling document.addEventListener(type, handler),
549
+ * we maintain ONE document listener per event type. When the event fires,
550
+ * we iterate through all registered handlers for that type.
551
+ *
552
+ * This reduces document-level listeners from O(N × eventTypes) to O(eventTypes).
553
+ */
554
+ const handlersByType = new Map();
555
+ function getOrCreateSet(eventType) {
556
+ let set = handlersByType.get(eventType);
557
+ if (!set) {
558
+ set = new Set();
559
+ handlersByType.set(eventType, set);
560
+ document.addEventListener(eventType, dispatch);
561
+ }
562
+ return set;
563
+ }
564
+ function dispatch(event) {
565
+ const handlers = handlersByType.get(event.type);
566
+ if (handlers) {
567
+ // Safe to iterate directly — mutations (add/remove) only happen in
568
+ // setup/cleanup, not during dispatch. Set iteration is stable for
569
+ // entries that existed when iteration began.
570
+ handlers.forEach((handler) => {
571
+ handler(event);
572
+ });
573
+ }
574
+ }
575
+ /**
576
+ * Register a handler for a document-level event type.
577
+ * Returns an unsubscribe function.
578
+ */
579
+ function addDelegatedEventListener(eventType, handler) {
580
+ const set = getOrCreateSet(eventType);
581
+ set.add(handler);
582
+ return () => {
583
+ set.delete(handler);
584
+ if (set.size === 0) {
585
+ handlersByType.delete(eventType);
586
+ document.removeEventListener(eventType, dispatch);
587
+ }
588
+ };
589
+ }
590
+
458
591
  const useTooltipEvents = ({ activeAnchor, anchorElements, anchorSelector, clickable, closeEvents, delayHide, delayShow, disableTooltip, float, globalCloseEvents, handleHideTooltipDelayed, handleShow, handleShowTooltipDelayed, handleTooltipPosition, hoveringTooltip, imperativeModeOnly, lastFloatPosition, openEvents, openOnClick, setActiveAnchor, show, tooltipHideDelayTimerRef, tooltipRef, tooltipShowDelayTimerRef, updateTooltipPosition, }) => {
459
- React.useEffect(() => {
460
- const dataTooltipId = anchorSelector ? parseDataTooltipIdSelector(anchorSelector) : null;
461
- const resolveAnchorElement = (target) => {
462
- var _a, _b;
463
- const targetElement = target;
464
- if (!(targetElement === null || targetElement === void 0 ? void 0 : targetElement.isConnected)) {
465
- return null;
466
- }
467
- if (dataTooltipId) {
468
- const matchedAnchor = resolveDataTooltipAnchor(targetElement, dataTooltipId);
469
- if (matchedAnchor && !(disableTooltip === null || disableTooltip === void 0 ? void 0 : disableTooltip(matchedAnchor))) {
470
- return matchedAnchor;
471
- }
472
- }
473
- else if (anchorSelector) {
474
- try {
475
- const matchedAnchor = (_a = (targetElement.matches(anchorSelector)
476
- ? targetElement
477
- : targetElement.closest(anchorSelector))) !== null && _a !== void 0 ? _a : null;
478
- if (matchedAnchor && !(disableTooltip === null || disableTooltip === void 0 ? void 0 : disableTooltip(matchedAnchor))) {
479
- return matchedAnchor;
480
- }
481
- }
482
- catch (_c) {
483
- return null;
484
- }
485
- }
486
- return ((_b = anchorElements.find((anchor) => anchor === targetElement || anchor.contains(targetElement))) !== null && _b !== void 0 ? _b : null);
487
- };
488
- const handlePointerMove = (event) => {
489
- if (!event) {
490
- return;
491
- }
492
- if (!activeAnchor) {
493
- return;
494
- }
495
- const targetAnchor = resolveAnchorElement(event.target);
496
- if (targetAnchor !== activeAnchor) {
497
- return;
498
- }
499
- const mouseEvent = event;
500
- const mousePosition = {
501
- x: mouseEvent.clientX,
502
- y: mouseEvent.clientY,
503
- };
504
- handleTooltipPosition(mousePosition);
505
- lastFloatPosition.current = mousePosition;
506
- };
507
- const handleClickOutsideAnchors = (event) => {
508
- var _a;
509
- if (!show) {
510
- return;
511
- }
512
- const target = event.target;
513
- if (!(target === null || target === void 0 ? void 0 : target.isConnected)) {
514
- return;
515
- }
516
- if ((_a = tooltipRef.current) === null || _a === void 0 ? void 0 : _a.contains(target)) {
517
- return;
518
- }
519
- if (activeAnchor === null || activeAnchor === void 0 ? void 0 : activeAnchor.contains(target)) {
520
- return;
521
- }
522
- if (anchorElements.some((anchor) => anchor === null || anchor === void 0 ? void 0 : anchor.contains(target))) {
523
- return;
524
- }
525
- handleShow(false);
526
- clearTimeoutRef(tooltipShowDelayTimerRef);
527
- };
528
- const handleShowTooltip = (anchor) => {
529
- if (!anchor) {
530
- return;
531
- }
532
- if (!anchor.isConnected) {
533
- setActiveAnchor(null);
534
- return;
535
- }
536
- if (disableTooltip === null || disableTooltip === void 0 ? void 0 : disableTooltip(anchor)) {
537
- return;
538
- }
539
- if (delayShow) {
540
- handleShowTooltipDelayed();
541
- }
542
- else {
543
- handleShow(true);
544
- }
545
- if (delayShow && activeAnchor && anchor !== activeAnchor) {
546
- // Moving to a different anchor while one is already active — defer the anchor
547
- // switch until the show delay fires to prevent content/position from updating
548
- // before visibility transitions complete.
549
- if (tooltipShowDelayTimerRef.current) {
550
- clearTimeout(tooltipShowDelayTimerRef.current);
551
- }
552
- tooltipShowDelayTimerRef.current = setTimeout(() => {
553
- setActiveAnchor(anchor);
554
- handleShow(true);
555
- }, delayShow);
556
- }
557
- else {
558
- setActiveAnchor(anchor);
559
- }
560
- if (tooltipHideDelayTimerRef.current) {
561
- clearTimeout(tooltipHideDelayTimerRef.current);
562
- }
563
- };
564
- const handleHideTooltip = () => {
565
- if (clickable) {
566
- handleHideTooltipDelayed(delayHide || 100);
567
- }
568
- else if (delayHide) {
569
- handleHideTooltipDelayed();
570
- }
571
- else {
572
- handleShow(false);
573
- }
574
- if (tooltipShowDelayTimerRef.current) {
575
- clearTimeout(tooltipShowDelayTimerRef.current);
576
- }
577
- };
578
- const internalDebouncedHandleShowTooltip = debounce(handleShowTooltip, 50);
579
- const internalDebouncedHandleHideTooltip = debounce(handleHideTooltip, 50);
580
- const debouncedHandleShowTooltip = (anchor) => {
581
- internalDebouncedHandleHideTooltip.cancel();
582
- internalDebouncedHandleShowTooltip(anchor);
583
- };
584
- const debouncedHandleHideTooltip = () => {
585
- internalDebouncedHandleShowTooltip.cancel();
586
- internalDebouncedHandleHideTooltip();
587
- };
588
- const handleScrollResize = () => {
589
- handleShow(false);
590
- };
591
- const hasClickEvent = openOnClick || (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);
592
- const actualOpenEvents = openEvents
592
+ // Ref-stable debounced handlers — avoids recreating debounce instances on every effect run
593
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
594
+ const debouncedShowRef = React.useRef(debounce((_anchor) => { }, 50));
595
+ const debouncedHideRef = React.useRef(debounce(() => { }, 50));
596
+ // Cache scroll parents — only recompute when the element actually changes
597
+ const anchorScrollParentRef = React.useRef(null);
598
+ const tooltipScrollParentRef = React.useRef(null);
599
+ const prevAnchorRef = React.useRef(null);
600
+ const prevTooltipRef = React.useRef(null);
601
+ if (activeAnchor !== prevAnchorRef.current) {
602
+ prevAnchorRef.current = activeAnchor;
603
+ anchorScrollParentRef.current = getScrollParent(activeAnchor);
604
+ }
605
+ const currentTooltipEl = tooltipRef.current;
606
+ if (currentTooltipEl !== prevTooltipRef.current) {
607
+ prevTooltipRef.current = currentTooltipEl;
608
+ tooltipScrollParentRef.current = getScrollParent(currentTooltipEl);
609
+ }
610
+ // Memoize event config objects only rebuild when the relevant props change
611
+ const hasClickEvent = openOnClick || (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);
612
+ const actualOpenEvents = React.useMemo(() => {
613
+ const events = openEvents
593
614
  ? { ...openEvents }
594
615
  : {
595
616
  mouseenter: true,
@@ -599,13 +620,25 @@ const useTooltipEvents = ({ activeAnchor, anchorElements, anchorSelector, clicka
599
620
  mousedown: false,
600
621
  };
601
622
  if (!openEvents && openOnClick) {
602
- Object.assign(actualOpenEvents, {
623
+ Object.assign(events, {
603
624
  mouseenter: false,
604
625
  focus: false,
605
626
  click: true,
606
627
  });
607
628
  }
608
- const actualCloseEvents = closeEvents
629
+ if (imperativeModeOnly) {
630
+ Object.assign(events, {
631
+ mouseenter: false,
632
+ focus: false,
633
+ click: false,
634
+ dblclick: false,
635
+ mousedown: false,
636
+ });
637
+ }
638
+ return events;
639
+ }, [openEvents, openOnClick, imperativeModeOnly]);
640
+ const actualCloseEvents = React.useMemo(() => {
641
+ const events = closeEvents
609
642
  ? { ...closeEvents }
610
643
  : {
611
644
  mouseleave: true,
@@ -615,12 +648,24 @@ const useTooltipEvents = ({ activeAnchor, anchorElements, anchorSelector, clicka
615
648
  mouseup: false,
616
649
  };
617
650
  if (!closeEvents && openOnClick) {
618
- Object.assign(actualCloseEvents, {
651
+ Object.assign(events, {
652
+ mouseleave: false,
653
+ blur: false,
654
+ });
655
+ }
656
+ if (imperativeModeOnly) {
657
+ Object.assign(events, {
619
658
  mouseleave: false,
620
659
  blur: false,
660
+ click: false,
661
+ dblclick: false,
662
+ mouseup: false,
621
663
  });
622
664
  }
623
- const actualGlobalCloseEvents = globalCloseEvents
665
+ return events;
666
+ }, [closeEvents, openOnClick, imperativeModeOnly]);
667
+ const actualGlobalCloseEvents = React.useMemo(() => {
668
+ const events = globalCloseEvents
624
669
  ? { ...globalCloseEvents }
625
670
  : {
626
671
  escape: false,
@@ -629,108 +674,157 @@ const useTooltipEvents = ({ activeAnchor, anchorElements, anchorSelector, clicka
629
674
  clickOutsideAnchor: hasClickEvent || false,
630
675
  };
631
676
  if (imperativeModeOnly) {
632
- Object.assign(actualOpenEvents, {
633
- mouseenter: false,
634
- focus: false,
635
- click: false,
636
- dblclick: false,
637
- mousedown: false,
638
- });
639
- Object.assign(actualCloseEvents, {
640
- mouseleave: false,
641
- blur: false,
642
- click: false,
643
- dblclick: false,
644
- mouseup: false,
645
- });
646
- Object.assign(actualGlobalCloseEvents, {
677
+ Object.assign(events, {
647
678
  escape: false,
648
679
  scroll: false,
649
680
  resize: false,
650
681
  clickOutsideAnchor: false,
651
682
  });
652
683
  }
653
- const tooltipElement = tooltipRef.current;
654
- const tooltipScrollParent = getScrollParent(tooltipRef.current);
655
- const anchorScrollParent = getScrollParent(activeAnchor);
656
- if (actualGlobalCloseEvents.scroll) {
657
- window.addEventListener('scroll', handleScrollResize);
658
- anchorScrollParent === null || anchorScrollParent === void 0 ? void 0 : anchorScrollParent.addEventListener('scroll', handleScrollResize);
659
- tooltipScrollParent === null || tooltipScrollParent === void 0 ? void 0 : tooltipScrollParent.addEventListener('scroll', handleScrollResize);
684
+ return events;
685
+ }, [globalCloseEvents, hasClickEvent, imperativeModeOnly]);
686
+ // --- Refs for values read inside event handlers (avoids effect deps) ---
687
+ const activeAnchorRef = React.useRef(activeAnchor);
688
+ activeAnchorRef.current = activeAnchor;
689
+ const showRef = React.useRef(show);
690
+ showRef.current = show;
691
+ const anchorElementsRef = React.useRef(anchorElements);
692
+ anchorElementsRef.current = anchorElements;
693
+ const handleShowRef = React.useRef(handleShow);
694
+ handleShowRef.current = handleShow;
695
+ const handleTooltipPositionRef = React.useRef(handleTooltipPosition);
696
+ handleTooltipPositionRef.current = handleTooltipPosition;
697
+ const updateTooltipPositionRef = React.useRef(updateTooltipPosition);
698
+ updateTooltipPositionRef.current = updateTooltipPosition;
699
+ // --- Handler refs (updated every render, read via ref indirection in effects) ---
700
+ const resolveAnchorElementRef = React.useRef(() => null);
701
+ const handleShowTooltipRef = React.useRef(() => { });
702
+ const handleHideTooltipRef = React.useRef(() => { });
703
+ const dataTooltipId = anchorSelector ? parseDataTooltipIdSelector(anchorSelector) : null;
704
+ resolveAnchorElementRef.current = (target) => {
705
+ var _a, _b;
706
+ const targetElement = target;
707
+ if (!(targetElement === null || targetElement === void 0 ? void 0 : targetElement.isConnected)) {
708
+ return null;
709
+ }
710
+ if (dataTooltipId) {
711
+ const matchedAnchor = resolveDataTooltipAnchor(targetElement, dataTooltipId);
712
+ if (matchedAnchor && !(disableTooltip === null || disableTooltip === void 0 ? void 0 : disableTooltip(matchedAnchor))) {
713
+ return matchedAnchor;
714
+ }
660
715
  }
661
- let updateTooltipCleanup = null;
662
- if (actualGlobalCloseEvents.resize) {
663
- window.addEventListener('resize', handleScrollResize);
716
+ else if (anchorSelector) {
717
+ try {
718
+ const matchedAnchor = (_a = (targetElement.matches(anchorSelector)
719
+ ? targetElement
720
+ : targetElement.closest(anchorSelector))) !== null && _a !== void 0 ? _a : null;
721
+ if (matchedAnchor && !(disableTooltip === null || disableTooltip === void 0 ? void 0 : disableTooltip(matchedAnchor))) {
722
+ return matchedAnchor;
723
+ }
724
+ }
725
+ catch (_c) {
726
+ return null;
727
+ }
664
728
  }
665
- else if (activeAnchor && tooltipRef.current) {
666
- updateTooltipCleanup = dom.autoUpdate(activeAnchor, tooltipRef.current, updateTooltipPosition, {
667
- ancestorResize: true,
668
- elementResize: true,
669
- layoutShift: true,
670
- });
729
+ return ((_b = anchorElementsRef.current.find((anchor) => anchor === targetElement || anchor.contains(targetElement))) !== null && _b !== void 0 ? _b : null);
730
+ };
731
+ handleShowTooltipRef.current = (anchor) => {
732
+ if (!anchor) {
733
+ return;
671
734
  }
672
- const handleEsc = (event) => {
673
- if (event.key !== 'Escape') {
674
- return;
735
+ if (!anchor.isConnected) {
736
+ setActiveAnchor(null);
737
+ return;
738
+ }
739
+ if (disableTooltip === null || disableTooltip === void 0 ? void 0 : disableTooltip(anchor)) {
740
+ return;
741
+ }
742
+ if (delayShow) {
743
+ handleShowTooltipDelayed();
744
+ }
745
+ else {
746
+ handleShow(true);
747
+ }
748
+ if (delayShow && activeAnchorRef.current && anchor !== activeAnchorRef.current) {
749
+ // Moving to a different anchor while one is already active — defer the anchor
750
+ // switch until the show delay fires to prevent content/position from updating
751
+ // before visibility transitions complete.
752
+ if (tooltipShowDelayTimerRef.current) {
753
+ clearTimeout(tooltipShowDelayTimerRef.current);
675
754
  }
755
+ tooltipShowDelayTimerRef.current = setTimeout(() => {
756
+ setActiveAnchor(anchor);
757
+ handleShow(true);
758
+ }, delayShow);
759
+ }
760
+ else {
761
+ setActiveAnchor(anchor);
762
+ }
763
+ if (tooltipHideDelayTimerRef.current) {
764
+ clearTimeout(tooltipHideDelayTimerRef.current);
765
+ }
766
+ };
767
+ handleHideTooltipRef.current = () => {
768
+ if (clickable) {
769
+ handleHideTooltipDelayed(delayHide || 100);
770
+ }
771
+ else if (delayHide) {
772
+ handleHideTooltipDelayed();
773
+ }
774
+ else {
676
775
  handleShow(false);
677
- };
678
- if (actualGlobalCloseEvents.escape) {
679
- window.addEventListener('keydown', handleEsc);
680
776
  }
681
- if (actualGlobalCloseEvents.clickOutsideAnchor) {
682
- window.addEventListener('click', handleClickOutsideAnchors);
777
+ if (tooltipShowDelayTimerRef.current) {
778
+ clearTimeout(tooltipShowDelayTimerRef.current);
683
779
  }
684
- const activeAnchorContainsTarget = (event) => Boolean((event === null || event === void 0 ? void 0 : event.target) && (activeAnchor === null || activeAnchor === void 0 ? void 0 : activeAnchor.contains(event.target)));
685
- const handleClickOpenTooltipAnchor = (event) => {
686
- var _a;
687
- const anchor = resolveAnchorElement((_a = event === null || event === void 0 ? void 0 : event.target) !== null && _a !== void 0 ? _a : null);
688
- if (!anchor) {
689
- return;
690
- }
691
- if (show && activeAnchor === anchor) {
692
- return;
693
- }
694
- handleShowTooltip(anchor);
780
+ };
781
+ // Update debounced callbacks to always delegate to latest handler refs
782
+ const debouncedShow = debouncedShowRef.current;
783
+ const debouncedHide = debouncedHideRef.current;
784
+ debouncedShow.setCallback((anchor) => handleShowTooltipRef.current(anchor));
785
+ debouncedHide.setCallback(() => handleHideTooltipRef.current());
786
+ // --- Effect 1: Delegated anchor events + tooltip hover ---
787
+ // Only re-runs when the set of active event types or interaction mode changes.
788
+ // Handlers read reactive values (activeAnchor, show, etc.) from refs at invocation
789
+ // time, so this effect is decoupled from show/hide state changes.
790
+ React.useEffect(() => {
791
+ const cleanupFns = [];
792
+ const addDelegatedListener = (eventType, listener) => {
793
+ cleanupFns.push(addDelegatedEventListener(eventType, listener));
695
794
  };
696
- const handleClickCloseTooltipAnchor = (event) => {
697
- if (!show || !activeAnchorContainsTarget(event)) {
698
- return;
699
- }
700
- handleHideTooltip();
795
+ const activeAnchorContainsTarget = (event) => { var _a; return Boolean((event === null || event === void 0 ? void 0 : event.target) && ((_a = activeAnchorRef.current) === null || _a === void 0 ? void 0 : _a.contains(event.target))); };
796
+ const debouncedHandleShowTooltip = (anchor) => {
797
+ debouncedHide.cancel();
798
+ debouncedShow(anchor);
799
+ };
800
+ const debouncedHandleHideTooltip = () => {
801
+ debouncedShow.cancel();
802
+ debouncedHide();
701
803
  };
702
- const regularEvents = ['mouseover', 'mouseout', 'mouseenter', 'mouseleave', 'focus', 'blur'];
703
- const clickEvents = ['click', 'dblclick', 'mousedown', 'mouseup'];
704
- const delegatedEvents = [];
705
804
  const addDelegatedHoverOpenListener = () => {
706
- delegatedEvents.push({
707
- event: 'mouseover',
708
- listener: (event) => {
709
- const anchor = resolveAnchorElement(event.target);
710
- if (!anchor) {
711
- return;
712
- }
713
- const relatedAnchor = resolveAnchorElement(event.relatedTarget);
714
- if (relatedAnchor === anchor) {
715
- return;
716
- }
717
- debouncedHandleShowTooltip(anchor);
718
- },
805
+ addDelegatedListener('mouseover', (event) => {
806
+ const anchor = resolveAnchorElementRef.current(event.target);
807
+ if (!anchor) {
808
+ return;
809
+ }
810
+ const relatedAnchor = resolveAnchorElementRef.current(event.relatedTarget);
811
+ if (relatedAnchor === anchor) {
812
+ return;
813
+ }
814
+ debouncedHandleShowTooltip(anchor);
719
815
  });
720
816
  };
721
817
  const addDelegatedHoverCloseListener = () => {
722
- delegatedEvents.push({
723
- event: 'mouseout',
724
- listener: (event) => {
725
- if (!activeAnchorContainsTarget(event)) {
726
- return;
727
- }
728
- const relatedTarget = event.relatedTarget;
729
- if (activeAnchor === null || activeAnchor === void 0 ? void 0 : activeAnchor.contains(relatedTarget)) {
730
- return;
731
- }
732
- debouncedHandleHideTooltip();
733
- },
818
+ addDelegatedListener('mouseout', (event) => {
819
+ var _a;
820
+ if (!activeAnchorContainsTarget(event)) {
821
+ return;
822
+ }
823
+ const relatedTarget = event.relatedTarget;
824
+ if ((_a = activeAnchorRef.current) === null || _a === void 0 ? void 0 : _a.contains(relatedTarget)) {
825
+ return;
826
+ }
827
+ debouncedHandleHideTooltip();
734
828
  });
735
829
  };
736
830
  if (actualOpenEvents.mouseenter) {
@@ -746,37 +840,48 @@ const useTooltipEvents = ({ activeAnchor, anchorElements, anchorSelector, clicka
746
840
  addDelegatedHoverCloseListener();
747
841
  }
748
842
  if (actualOpenEvents.focus) {
749
- delegatedEvents.push({
750
- event: 'focusin',
751
- listener: (event) => {
752
- debouncedHandleShowTooltip(resolveAnchorElement(event.target));
753
- },
843
+ addDelegatedListener('focusin', (event) => {
844
+ debouncedHandleShowTooltip(resolveAnchorElementRef.current(event.target));
754
845
  });
755
846
  }
756
847
  if (actualCloseEvents.blur) {
757
- delegatedEvents.push({
758
- event: 'focusout',
759
- listener: (event) => {
760
- if (!activeAnchorContainsTarget(event)) {
761
- return;
762
- }
763
- const relatedTarget = event.relatedTarget;
764
- if (activeAnchor === null || activeAnchor === void 0 ? void 0 : activeAnchor.contains(relatedTarget)) {
765
- return;
766
- }
767
- debouncedHandleHideTooltip();
768
- },
848
+ addDelegatedListener('focusout', (event) => {
849
+ var _a;
850
+ if (!activeAnchorContainsTarget(event)) {
851
+ return;
852
+ }
853
+ const relatedTarget = event.relatedTarget;
854
+ if ((_a = activeAnchorRef.current) === null || _a === void 0 ? void 0 : _a.contains(relatedTarget)) {
855
+ return;
856
+ }
857
+ debouncedHandleHideTooltip();
769
858
  });
770
859
  }
860
+ const regularEvents = ['mouseover', 'mouseout', 'mouseenter', 'mouseleave', 'focus', 'blur'];
861
+ const clickEvents = ['click', 'dblclick', 'mousedown', 'mouseup'];
862
+ const handleClickOpenTooltipAnchor = (event) => {
863
+ var _a;
864
+ const anchor = resolveAnchorElementRef.current((_a = event === null || event === void 0 ? void 0 : event.target) !== null && _a !== void 0 ? _a : null);
865
+ if (!anchor) {
866
+ return;
867
+ }
868
+ if (showRef.current && activeAnchorRef.current === anchor) {
869
+ return;
870
+ }
871
+ handleShowTooltipRef.current(anchor);
872
+ };
873
+ const handleClickCloseTooltipAnchor = (event) => {
874
+ if (!showRef.current || !activeAnchorContainsTarget(event)) {
875
+ return;
876
+ }
877
+ handleHideTooltipRef.current();
878
+ };
771
879
  Object.entries(actualOpenEvents).forEach(([event, enabled]) => {
772
880
  if (!enabled || regularEvents.includes(event)) {
773
881
  return;
774
882
  }
775
883
  if (clickEvents.includes(event)) {
776
- delegatedEvents.push({
777
- event,
778
- listener: handleClickOpenTooltipAnchor,
779
- });
884
+ addDelegatedListener(event, handleClickOpenTooltipAnchor);
780
885
  }
781
886
  });
782
887
  Object.entries(actualCloseEvents).forEach(([event, enabled]) => {
@@ -784,33 +889,110 @@ const useTooltipEvents = ({ activeAnchor, anchorElements, anchorSelector, clicka
784
889
  return;
785
890
  }
786
891
  if (clickEvents.includes(event)) {
787
- delegatedEvents.push({
788
- event,
789
- listener: handleClickCloseTooltipAnchor,
790
- });
892
+ addDelegatedListener(event, handleClickCloseTooltipAnchor);
791
893
  }
792
894
  });
793
895
  if (float) {
794
- delegatedEvents.push({
795
- event: 'pointermove',
796
- listener: handlePointerMove,
896
+ addDelegatedListener('pointermove', (event) => {
897
+ const currentActiveAnchor = activeAnchorRef.current;
898
+ if (!currentActiveAnchor) {
899
+ return;
900
+ }
901
+ const targetAnchor = resolveAnchorElementRef.current(event.target);
902
+ if (targetAnchor !== currentActiveAnchor) {
903
+ return;
904
+ }
905
+ const mouseEvent = event;
906
+ const mousePosition = {
907
+ x: mouseEvent.clientX,
908
+ y: mouseEvent.clientY,
909
+ };
910
+ handleTooltipPositionRef.current(mousePosition);
911
+ lastFloatPosition.current = mousePosition;
797
912
  });
798
913
  }
914
+ const tooltipElement = tooltipRef.current;
799
915
  const handleMouseOverTooltip = () => {
800
916
  hoveringTooltip.current = true;
801
917
  };
802
918
  const handleMouseOutTooltip = () => {
803
919
  hoveringTooltip.current = false;
804
- handleHideTooltip();
920
+ handleHideTooltipRef.current();
805
921
  };
806
922
  const addHoveringTooltipListeners = clickable && (actualCloseEvents.mouseout || actualCloseEvents.mouseleave);
807
923
  if (addHoveringTooltipListeners) {
808
924
  tooltipElement === null || tooltipElement === void 0 ? void 0 : tooltipElement.addEventListener('mouseover', handleMouseOverTooltip);
809
925
  tooltipElement === null || tooltipElement === void 0 ? void 0 : tooltipElement.addEventListener('mouseout', handleMouseOutTooltip);
810
926
  }
811
- delegatedEvents.forEach(({ event, listener }) => {
812
- document.addEventListener(event, listener);
813
- });
927
+ return () => {
928
+ cleanupFns.forEach((fn) => fn());
929
+ if (addHoveringTooltipListeners) {
930
+ tooltipElement === null || tooltipElement === void 0 ? void 0 : tooltipElement.removeEventListener('mouseover', handleMouseOverTooltip);
931
+ tooltipElement === null || tooltipElement === void 0 ? void 0 : tooltipElement.removeEventListener('mouseout', handleMouseOutTooltip);
932
+ }
933
+ debouncedShow.cancel();
934
+ debouncedHide.cancel();
935
+ };
936
+ // eslint-disable-next-line react-hooks/exhaustive-deps
937
+ }, [actualOpenEvents, actualCloseEvents, float, clickable]);
938
+ // --- Effect 2: Global close events + auto-update ---
939
+ // Re-runs when the global close config changes, or when the active anchor changes
940
+ // (for scroll parent listeners and floating-ui autoUpdate).
941
+ React.useEffect(() => {
942
+ const handleScrollResize = () => {
943
+ handleShowRef.current(false);
944
+ };
945
+ const tooltipScrollParent = tooltipScrollParentRef.current;
946
+ const anchorScrollParent = anchorScrollParentRef.current;
947
+ if (actualGlobalCloseEvents.scroll) {
948
+ window.addEventListener('scroll', handleScrollResize);
949
+ anchorScrollParent === null || anchorScrollParent === void 0 ? void 0 : anchorScrollParent.addEventListener('scroll', handleScrollResize);
950
+ tooltipScrollParent === null || tooltipScrollParent === void 0 ? void 0 : tooltipScrollParent.addEventListener('scroll', handleScrollResize);
951
+ }
952
+ let updateTooltipCleanup = null;
953
+ if (actualGlobalCloseEvents.resize) {
954
+ window.addEventListener('resize', handleScrollResize);
955
+ }
956
+ else if (activeAnchor && tooltipRef.current) {
957
+ updateTooltipCleanup = dom.autoUpdate(activeAnchor, tooltipRef.current, () => updateTooltipPositionRef.current(), {
958
+ ancestorResize: true,
959
+ elementResize: true,
960
+ layoutShift: true,
961
+ });
962
+ }
963
+ const handleEsc = (event) => {
964
+ if (event.key !== 'Escape') {
965
+ return;
966
+ }
967
+ handleShowRef.current(false);
968
+ };
969
+ if (actualGlobalCloseEvents.escape) {
970
+ window.addEventListener('keydown', handleEsc);
971
+ }
972
+ const handleClickOutsideAnchors = (event) => {
973
+ var _a, _b;
974
+ if (!showRef.current) {
975
+ return;
976
+ }
977
+ const target = event.target;
978
+ if (!(target === null || target === void 0 ? void 0 : target.isConnected)) {
979
+ return;
980
+ }
981
+ if ((_a = tooltipRef.current) === null || _a === void 0 ? void 0 : _a.contains(target)) {
982
+ return;
983
+ }
984
+ if ((_b = activeAnchorRef.current) === null || _b === void 0 ? void 0 : _b.contains(target)) {
985
+ return;
986
+ }
987
+ if (anchorElementsRef.current.some((anchor) => anchor === null || anchor === void 0 ? void 0 : anchor.contains(target))) {
988
+ return;
989
+ }
990
+ handleShowRef.current(false);
991
+ clearTimeoutRef(tooltipShowDelayTimerRef);
992
+ };
993
+ if (actualGlobalCloseEvents.clickOutsideAnchor) {
994
+ window.addEventListener('click', handleClickOutsideAnchors);
995
+ }
814
996
  return () => {
815
997
  if (actualGlobalCloseEvents.scroll) {
816
998
  window.removeEventListener('scroll', handleScrollResize);
@@ -823,51 +1005,19 @@ const useTooltipEvents = ({ activeAnchor, anchorElements, anchorSelector, clicka
823
1005
  if (updateTooltipCleanup) {
824
1006
  updateTooltipCleanup();
825
1007
  }
826
- if (actualGlobalCloseEvents.clickOutsideAnchor) {
827
- window.removeEventListener('click', handleClickOutsideAnchors);
828
- }
829
1008
  if (actualGlobalCloseEvents.escape) {
830
1009
  window.removeEventListener('keydown', handleEsc);
831
1010
  }
832
- if (addHoveringTooltipListeners) {
833
- tooltipElement === null || tooltipElement === void 0 ? void 0 : tooltipElement.removeEventListener('mouseover', handleMouseOverTooltip);
834
- tooltipElement === null || tooltipElement === void 0 ? void 0 : tooltipElement.removeEventListener('mouseout', handleMouseOutTooltip);
1011
+ if (actualGlobalCloseEvents.clickOutsideAnchor) {
1012
+ window.removeEventListener('click', handleClickOutsideAnchors);
835
1013
  }
836
- delegatedEvents.forEach(({ event, listener }) => {
837
- document.removeEventListener(event, listener);
838
- });
839
- internalDebouncedHandleShowTooltip.cancel();
840
- internalDebouncedHandleHideTooltip.cancel();
841
1014
  };
842
- }, [
843
- activeAnchor,
844
- anchorElements,
845
- anchorSelector,
846
- clickable,
847
- closeEvents,
848
- delayHide,
849
- delayShow,
850
- disableTooltip,
851
- float,
852
- globalCloseEvents,
853
- handleHideTooltipDelayed,
854
- handleShow,
855
- handleShowTooltipDelayed,
856
- handleTooltipPosition,
857
- imperativeModeOnly,
858
- lastFloatPosition,
859
- openEvents,
860
- openOnClick,
861
- setActiveAnchor,
862
- show,
863
- tooltipHideDelayTimerRef,
864
- tooltipRef,
865
- tooltipShowDelayTimerRef,
866
- updateTooltipPosition,
867
- hoveringTooltip,
868
- ]);
1015
+ // eslint-disable-next-line react-hooks/exhaustive-deps
1016
+ }, [actualGlobalCloseEvents, activeAnchor]);
869
1017
  };
870
1018
 
1019
+ // Shared across all tooltip instances — the CSS variable is on :root and never changes per-instance
1020
+ let globalTransitionShowDelay = null;
871
1021
  const Tooltip = ({
872
1022
  // props
873
1023
  forwardRef, id, className, classNameArrow, variant = 'dark', portalRoot, anchorSelect, place = 'top', offset = 10, openOnClick = false, positionStrategy = 'absolute', middlewares, wrapper: WrapperElement, delayShow = 0, delayHide = 0, autoClose, float = false, hidden = false, noArrow = false, clickable = false, openEvents, closeEvents, globalCloseEvents, imperativeModeOnly, style: externalStyles, position, afterShow, afterHide, disableTooltip,
@@ -892,6 +1042,18 @@ content, contentWrapperRef, isOpen, defaultIsOpen = false, setIsOpen, previousAc
892
1042
  const lastFloatPosition = React.useRef(null);
893
1043
  const hoveringTooltip = React.useRef(false);
894
1044
  const mounted = React.useRef(false);
1045
+ const virtualElementRef = React.useRef({
1046
+ getBoundingClientRect: () => ({
1047
+ x: 0,
1048
+ y: 0,
1049
+ width: 0,
1050
+ height: 0,
1051
+ top: 0,
1052
+ left: 0,
1053
+ right: 0,
1054
+ bottom: 0,
1055
+ }),
1056
+ });
895
1057
  /**
896
1058
  * useLayoutEffect runs before useEffect,
897
1059
  * but should be used carefully because of caveats
@@ -989,8 +1151,11 @@ content, contentWrapperRef, isOpen, defaultIsOpen = false, setIsOpen, previousAc
989
1151
  /**
990
1152
  * see `onTransitionEnd` on tooltip wrapper
991
1153
  */
992
- const style = getComputedStyle(document.body);
993
- const transitionShowDelay = cssTimeToMs(style.getPropertyValue('--rt-transition-show-delay'));
1154
+ if (globalTransitionShowDelay === null) {
1155
+ const style = getComputedStyle(document.body);
1156
+ globalTransitionShowDelay = cssTimeToMs(style.getPropertyValue('--rt-transition-show-delay'));
1157
+ }
1158
+ const transitionShowDelay = globalTransitionShowDelay;
994
1159
  missedTransitionTimerRef.current = setTimeout(() => {
995
1160
  /**
996
1161
  * if the tooltip switches from `show === true` to `show === false` too fast
@@ -1021,9 +1186,26 @@ content, contentWrapperRef, isOpen, defaultIsOpen = false, setIsOpen, previousAc
1021
1186
  if (!mounted.current) {
1022
1187
  return;
1023
1188
  }
1024
- setComputedPosition((oldComputedPosition) => deepEqual(oldComputedPosition, newComputedPosition)
1025
- ? oldComputedPosition
1026
- : newComputedPosition);
1189
+ setComputedPosition((oldComputedPosition) => {
1190
+ if (oldComputedPosition.place === newComputedPosition.place &&
1191
+ oldComputedPosition.tooltipStyles.left === newComputedPosition.tooltipStyles.left &&
1192
+ oldComputedPosition.tooltipStyles.top === newComputedPosition.tooltipStyles.top &&
1193
+ oldComputedPosition.tooltipStyles.border === newComputedPosition.tooltipStyles.border &&
1194
+ oldComputedPosition.tooltipArrowStyles.left ===
1195
+ newComputedPosition.tooltipArrowStyles.left &&
1196
+ oldComputedPosition.tooltipArrowStyles.top === newComputedPosition.tooltipArrowStyles.top &&
1197
+ oldComputedPosition.tooltipArrowStyles.right ===
1198
+ newComputedPosition.tooltipArrowStyles.right &&
1199
+ oldComputedPosition.tooltipArrowStyles.bottom ===
1200
+ newComputedPosition.tooltipArrowStyles.bottom &&
1201
+ oldComputedPosition.tooltipArrowStyles.borderBottom ===
1202
+ newComputedPosition.tooltipArrowStyles.borderBottom &&
1203
+ oldComputedPosition.tooltipArrowStyles.borderRight ===
1204
+ newComputedPosition.tooltipArrowStyles.borderRight) {
1205
+ return oldComputedPosition;
1206
+ }
1207
+ return newComputedPosition;
1208
+ });
1027
1209
  }, []);
1028
1210
  const handleShowTooltipDelayed = React.useCallback((delay = delayShow) => {
1029
1211
  if (tooltipShowDelayTimerRef.current) {
@@ -1051,24 +1233,20 @@ content, contentWrapperRef, isOpen, defaultIsOpen = false, setIsOpen, previousAc
1051
1233
  }, [delayHide, handleShow]);
1052
1234
  const handleTooltipPosition = React.useCallback(({ x, y }) => {
1053
1235
  var _a;
1054
- const virtualElement = {
1055
- getBoundingClientRect() {
1056
- return {
1057
- x,
1058
- y,
1059
- width: 0,
1060
- height: 0,
1061
- top: y,
1062
- left: x,
1063
- right: x,
1064
- bottom: y,
1065
- };
1066
- },
1067
- };
1236
+ virtualElementRef.current.getBoundingClientRect = () => ({
1237
+ x,
1238
+ y,
1239
+ width: 0,
1240
+ height: 0,
1241
+ top: y,
1242
+ left: x,
1243
+ right: x,
1244
+ bottom: y,
1245
+ });
1068
1246
  computeTooltipPosition({
1069
1247
  place: (_a = imperativeOptions === null || imperativeOptions === void 0 ? void 0 : imperativeOptions.place) !== null && _a !== void 0 ? _a : place,
1070
1248
  offset,
1071
- elementReference: virtualElement,
1249
+ elementReference: virtualElementRef.current,
1072
1250
  tooltipReference: tooltipRef.current,
1073
1251
  tooltipArrowReference: tooltipArrowRef.current,
1074
1252
  strategy: positionStrategy,
@@ -1194,6 +1372,8 @@ content, contentWrapperRef, isOpen, defaultIsOpen = false, setIsOpen, previousAc
1194
1372
  tooltipShowDelayTimerRef,
1195
1373
  updateTooltipPosition,
1196
1374
  });
1375
+ const updateTooltipPositionRef = React.useRef(updateTooltipPosition);
1376
+ updateTooltipPositionRef.current = updateTooltipPosition;
1197
1377
  React.useEffect(() => {
1198
1378
  if (!rendered) {
1199
1379
  return;
@@ -1212,7 +1392,7 @@ content, contentWrapperRef, isOpen, defaultIsOpen = false, setIsOpen, previousAc
1212
1392
  }
1213
1393
  timeoutId = setTimeout(() => {
1214
1394
  if (mounted.current) {
1215
- updateTooltipPosition();
1395
+ updateTooltipPositionRef.current();
1216
1396
  }
1217
1397
  timeoutId = null;
1218
1398
  }, 0);
@@ -1224,7 +1404,7 @@ content, contentWrapperRef, isOpen, defaultIsOpen = false, setIsOpen, previousAc
1224
1404
  clearTimeout(timeoutId);
1225
1405
  }
1226
1406
  };
1227
- }, [content, contentWrapperRef, rendered, updateTooltipPosition]);
1407
+ }, [content, contentWrapperRef, rendered]);
1228
1408
  React.useEffect(() => {
1229
1409
  var _a;
1230
1410
  const shouldResolveInitialActiveAnchor = rendered || defaultIsOpen || Boolean(isOpen);
@@ -1285,7 +1465,20 @@ content, contentWrapperRef, isOpen, defaultIsOpen = false, setIsOpen, previousAc
1285
1465
  }, [delayShow, handleShowTooltipDelayed]);
1286
1466
  const actualContent = (_a = imperativeOptions === null || imperativeOptions === void 0 ? void 0 : imperativeOptions.content) !== null && _a !== void 0 ? _a : content;
1287
1467
  const hasContent = actualContent !== null && actualContent !== undefined;
1288
- const canShow = show && Object.keys(computedPosition.tooltipStyles).length > 0;
1468
+ const canShow = show && computedPosition.tooltipStyles.left !== undefined;
1469
+ const tooltipStyle = React.useMemo(() => ({
1470
+ ...externalStyles,
1471
+ ...computedPosition.tooltipStyles,
1472
+ opacity: opacity !== undefined && canShow ? opacity : undefined,
1473
+ }), [externalStyles, computedPosition.tooltipStyles, opacity, canShow]);
1474
+ const arrowBackground = React.useMemo(() => arrowColor
1475
+ ? `linear-gradient(to right bottom, transparent 50%, ${arrowColor} 50%)`
1476
+ : undefined, [arrowColor]);
1477
+ const arrowStyle = React.useMemo(() => ({
1478
+ ...computedPosition.tooltipArrowStyles,
1479
+ background: arrowBackground,
1480
+ '--rt-arrow-size': `${arrowSize}px`,
1481
+ }), [computedPosition.tooltipArrowStyles, arrowBackground, arrowSize]);
1289
1482
  React.useImperativeHandle(forwardRef, () => ({
1290
1483
  open: (options) => {
1291
1484
  let imperativeAnchor = null;
@@ -1340,19 +1533,9 @@ content, contentWrapperRef, isOpen, defaultIsOpen = false, setIsOpen, previousAc
1340
1533
  setRendered(false);
1341
1534
  setImperativeOptions(null);
1342
1535
  afterHide === null || afterHide === void 0 ? void 0 : afterHide();
1343
- }, style: {
1344
- ...externalStyles,
1345
- ...computedPosition.tooltipStyles,
1346
- opacity: opacity !== undefined && canShow ? opacity : undefined,
1347
- }, ref: tooltipRef },
1536
+ }, style: tooltipStyle, ref: tooltipRef },
1348
1537
  React.createElement(WrapperElement, { className: clsx('react-tooltip-content-wrapper', coreStyles['content'], styles['content']) }, actualContent),
1349
- React.createElement(WrapperElement, { className: clsx('react-tooltip-arrow', coreStyles['arrow'], styles['arrow'], classNameArrow, noArrow && coreStyles['noArrow']), style: {
1350
- ...computedPosition.tooltipArrowStyles,
1351
- background: arrowColor
1352
- ? `linear-gradient(to right bottom, transparent 50%, ${arrowColor} 50%)`
1353
- : undefined,
1354
- '--rt-arrow-size': `${arrowSize}px`,
1355
- }, ref: tooltipArrowRef }))) : null;
1538
+ React.createElement(WrapperElement, { className: clsx('react-tooltip-arrow', coreStyles['arrow'], styles['arrow'], classNameArrow, noArrow && coreStyles['noArrow']), style: arrowStyle, ref: tooltipArrowRef }))) : null;
1356
1539
  if (!tooltipNode) {
1357
1540
  return null;
1358
1541
  }
@@ -1363,12 +1546,82 @@ content, contentWrapperRef, isOpen, defaultIsOpen = false, setIsOpen, previousAc
1363
1546
  };
1364
1547
  var Tooltip$1 = React.memo(Tooltip);
1365
1548
 
1549
+ /**
1550
+ * Shared MutationObserver for data-tooltip-* attribute changes.
1551
+ * Instead of N observers (one per tooltip), a single observer watches
1552
+ * all active anchors and dispatches changes to registered callbacks.
1553
+ */
1554
+ const observedElements = new Map();
1555
+ let sharedObserver = null;
1556
+ const observerConfig = {
1557
+ attributes: true,
1558
+ childList: false,
1559
+ subtree: false,
1560
+ };
1561
+ function getObserver() {
1562
+ if (!sharedObserver) {
1563
+ sharedObserver = new MutationObserver((mutationList) => {
1564
+ var _a;
1565
+ for (const mutation of mutationList) {
1566
+ if (mutation.type !== 'attributes' ||
1567
+ !((_a = mutation.attributeName) === null || _a === void 0 ? void 0 : _a.startsWith('data-tooltip-'))) {
1568
+ continue;
1569
+ }
1570
+ const target = mutation.target;
1571
+ const callbacks = observedElements.get(target);
1572
+ if (callbacks) {
1573
+ callbacks.forEach((cb) => cb(target));
1574
+ }
1575
+ }
1576
+ });
1577
+ }
1578
+ return sharedObserver;
1579
+ }
1580
+ function observeAnchorAttributes(element, callback) {
1581
+ const observer = getObserver();
1582
+ let callbacks = observedElements.get(element);
1583
+ if (!callbacks) {
1584
+ callbacks = new Set();
1585
+ observedElements.set(element, callbacks);
1586
+ observer.observe(element, observerConfig);
1587
+ }
1588
+ callbacks.add(callback);
1589
+ return () => {
1590
+ const cbs = observedElements.get(element);
1591
+ if (cbs) {
1592
+ cbs.delete(callback);
1593
+ if (cbs.size === 0) {
1594
+ observedElements.delete(element);
1595
+ // MutationObserver doesn't have unobserve — if no elements left, disconnect & reset
1596
+ if (observedElements.size === 0) {
1597
+ observer.disconnect();
1598
+ }
1599
+ else {
1600
+ // Re-observe remaining elements (MutationObserver has no per-target unobserve)
1601
+ observer.disconnect();
1602
+ observedElements.forEach((_cbs, el) => {
1603
+ observer.observe(el, observerConfig);
1604
+ });
1605
+ }
1606
+ }
1607
+ }
1608
+ };
1609
+ }
1610
+
1366
1611
  const TooltipController = React.forwardRef(({ id, anchorSelect, content, render, className, classNameArrow, variant = 'dark', portalRoot, place = 'top', offset = 10, wrapper = 'div', children = null, openOnClick = false, positionStrategy = 'absolute', middlewares, delayShow = 0, delayHide = 0, autoClose, float = false, hidden = false, noArrow = false, clickable = false, openEvents, closeEvents, globalCloseEvents, imperativeModeOnly = false, style, position, isOpen, defaultIsOpen = false, disableStyleInjection = false, border, opacity, arrowColor, arrowSize, setIsOpen, afterShow, afterHide, disableTooltip, role = 'tooltip', }, ref) => {
1367
1612
  var _a, _b, _c, _d, _e, _f, _g, _h;
1368
1613
  const [activeAnchor, setActiveAnchor] = React.useState(null);
1369
1614
  const [anchorDataAttributes, setAnchorDataAttributes] = React.useState({});
1370
1615
  const previousActiveAnchorRef = React.useRef(null);
1371
1616
  const styleInjectionRef = React.useRef(disableStyleInjection);
1617
+ const handleSetActiveAnchor = React.useCallback((anchor) => {
1618
+ setActiveAnchor((prev) => {
1619
+ if (!(anchor === null || anchor === void 0 ? void 0 : anchor.isSameNode(prev))) {
1620
+ previousActiveAnchorRef.current = prev;
1621
+ }
1622
+ return anchor;
1623
+ });
1624
+ }, []);
1372
1625
  /* c8 ignore start */
1373
1626
  const getDataAttributesFromAnchorElement = (elementReference) => {
1374
1627
  const dataAttributes = elementReference === null || elementReference === void 0 ? void 0 : elementReference.getAttributeNames().reduce((acc, name) => {
@@ -1400,37 +1653,24 @@ const TooltipController = React.forwardRef(({ id, anchorSelect, content, render,
1400
1653
  // eslint-disable-next-line react-hooks/exhaustive-deps
1401
1654
  }, []);
1402
1655
  React.useEffect(() => {
1403
- const observerCallback = (mutationList) => {
1404
- mutationList.forEach((mutation) => {
1405
- var _a;
1406
- if (!activeAnchor ||
1407
- mutation.type !== 'attributes' ||
1408
- !((_a = mutation.attributeName) === null || _a === void 0 ? void 0 : _a.startsWith('data-tooltip-'))) {
1409
- return;
1656
+ if (!activeAnchor) {
1657
+ setAnchorDataAttributes({});
1658
+ return () => { };
1659
+ }
1660
+ const updateAttributes = (element) => {
1661
+ const attrs = getDataAttributesFromAnchorElement(element);
1662
+ setAnchorDataAttributes((prev) => {
1663
+ const keys = Object.keys(attrs);
1664
+ const prevKeys = Object.keys(prev);
1665
+ if (keys.length === prevKeys.length && keys.every((key) => attrs[key] === prev[key])) {
1666
+ return prev;
1410
1667
  }
1411
- // make sure to get all set attributes, since all unset attributes are reset
1412
- const dataAttributes = getDataAttributesFromAnchorElement(activeAnchor);
1413
- setAnchorDataAttributes(dataAttributes);
1668
+ return attrs;
1414
1669
  });
1415
1670
  };
1416
- // Create an observer instance linked to the callback function
1417
- const observer = new MutationObserver(observerCallback);
1418
- // do not check for subtree and childrens, we only want to know attribute changes
1419
- // to stay watching `data-attributes-*` from anchor element
1420
- const observerConfig = { attributes: true, childList: false, subtree: false };
1421
- if (activeAnchor) {
1422
- const dataAttributes = getDataAttributesFromAnchorElement(activeAnchor);
1423
- setAnchorDataAttributes(dataAttributes);
1424
- // Start observing the target node for configured mutations
1425
- observer.observe(activeAnchor, observerConfig);
1426
- }
1427
- else {
1428
- setAnchorDataAttributes({});
1429
- }
1430
- return () => {
1431
- // Remove the observer when the tooltip is destroyed
1432
- observer.disconnect();
1433
- };
1671
+ updateAttributes(activeAnchor);
1672
+ const unsubscribe = observeAnchorAttributes(activeAnchor, updateAttributes);
1673
+ return unsubscribe;
1434
1674
  }, [activeAnchor, anchorSelect]);
1435
1675
  React.useEffect(() => {
1436
1676
  /* c8 ignore start */
@@ -1511,14 +1751,7 @@ const TooltipController = React.forwardRef(({ id, anchorSelect, content, render,
1511
1751
  disableTooltip,
1512
1752
  activeAnchor,
1513
1753
  previousActiveAnchor: previousActiveAnchorRef.current,
1514
- setActiveAnchor: (anchor) => {
1515
- setActiveAnchor((prev) => {
1516
- if (!(anchor === null || anchor === void 0 ? void 0 : anchor.isSameNode(prev))) {
1517
- previousActiveAnchorRef.current = prev;
1518
- }
1519
- return anchor;
1520
- });
1521
- },
1754
+ setActiveAnchor: handleSetActiveAnchor,
1522
1755
  role,
1523
1756
  };
1524
1757
  return React.createElement(Tooltip$1, { ...props });
@@ -1545,7 +1778,6 @@ const TooltipCoreStyles = `:root {
1545
1778
  left: 0;
1546
1779
  pointer-events: none;
1547
1780
  opacity: 0;
1548
- will-change: opacity;
1549
1781
  }
1550
1782
 
1551
1783
  .core-styles-module_fixed__pcSol {
@@ -1576,11 +1808,13 @@ const TooltipCoreStyles = `:root {
1576
1808
  .core-styles-module_show__Nt9eE {
1577
1809
  opacity: var(--rt-opacity);
1578
1810
  transition: opacity var(--rt-transition-show-delay) ease-out;
1811
+ will-change: opacity;
1579
1812
  }
1580
1813
 
1581
1814
  .core-styles-module_closing__sGnxF {
1582
1815
  opacity: 0;
1583
1816
  transition: opacity var(--rt-transition-closing-delay) ease-in;
1817
+ will-change: opacity;
1584
1818
  }
1585
1819
 
1586
1820
  `;