react-tooltip 6.0.0-beta.1179.rc.9 → 6.0.1
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 +3 -1
- package/dist/react-tooltip.cjs +695 -389
- package/dist/react-tooltip.cjs.map +1 -1
- package/dist/react-tooltip.css +2 -1
- package/dist/react-tooltip.d.ts +2 -1
- package/dist/react-tooltip.min.cjs +2 -2
- package/dist/react-tooltip.min.cjs.map +1 -1
- package/dist/react-tooltip.min.css +1 -1
- package/dist/react-tooltip.min.mjs +2 -2
- package/dist/react-tooltip.min.mjs.map +1 -1
- package/dist/react-tooltip.mjs +696 -390
- package/dist/react-tooltip.mjs.map +1 -1
- package/dist/react-tooltip.umd.js +695 -389
- package/dist/react-tooltip.umd.js.map +1 -1
- package/dist/react-tooltip.umd.min.js +2 -2
- package/dist/react-tooltip.umd.min.js.map +1 -1
- package/eslint.config.js +155 -0
- package/package.json +47 -46
package/dist/react-tooltip.cjs
CHANGED
|
@@ -44,11 +44,9 @@ function injectStyle({ css, id = REACT_TOOLTIP_BASE_STYLES_ID, type = 'base', re
|
|
|
44
44
|
return;
|
|
45
45
|
}
|
|
46
46
|
if (type === 'core') {
|
|
47
|
-
// eslint-disable-next-line no-param-reassign
|
|
48
47
|
id = REACT_TOOLTIP_CORE_STYLES_ID;
|
|
49
48
|
}
|
|
50
49
|
if (!ref) {
|
|
51
|
-
// eslint-disable-next-line no-param-reassign
|
|
52
50
|
ref = {};
|
|
53
51
|
}
|
|
54
52
|
const { insertAt } = ref;
|
|
@@ -79,7 +77,6 @@ function injectStyle({ css, id = REACT_TOOLTIP_BASE_STYLES_ID, type = 'base', re
|
|
|
79
77
|
style.appendChild(document.createTextNode(css));
|
|
80
78
|
}
|
|
81
79
|
if (typeof state[type] !== 'undefined') {
|
|
82
|
-
// eslint-disable-next-line no-param-reassign
|
|
83
80
|
state[type] = true;
|
|
84
81
|
}
|
|
85
82
|
else {
|
|
@@ -87,16 +84,12 @@ function injectStyle({ css, id = REACT_TOOLTIP_BASE_STYLES_ID, type = 'base', re
|
|
|
87
84
|
}
|
|
88
85
|
}
|
|
89
86
|
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
}),
|
|
95
|
-
dom.shift({ padding: 5 }),
|
|
96
|
-
], 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, }) => {
|
|
97
91
|
if (!elementReference) {
|
|
98
92
|
// elementReference can be null or undefined and we will not compute the position
|
|
99
|
-
// eslint-disable-next-line no-console
|
|
100
93
|
// console.error('The reference element for tooltip was not defined: ', elementReference)
|
|
101
94
|
return { tooltipStyles: {}, tooltipArrowStyles: {}, place };
|
|
102
95
|
}
|
|
@@ -181,6 +174,7 @@ const cssTimeToMs = (time) => {
|
|
|
181
174
|
*/
|
|
182
175
|
const debounce = (func, wait, immediate) => {
|
|
183
176
|
let timeout = null;
|
|
177
|
+
let currentFunc = func;
|
|
184
178
|
const debounced = function debounced(...args) {
|
|
185
179
|
const later = () => {
|
|
186
180
|
timeout = null;
|
|
@@ -190,7 +184,7 @@ const debounce = (func, wait, immediate) => {
|
|
|
190
184
|
* there's no need to clear the timeout
|
|
191
185
|
* since we expect it to resolve and set `timeout = null`
|
|
192
186
|
*/
|
|
193
|
-
|
|
187
|
+
currentFunc.apply(this, args);
|
|
194
188
|
timeout = setTimeout(later, wait);
|
|
195
189
|
}
|
|
196
190
|
};
|
|
@@ -203,36 +197,12 @@ const debounce = (func, wait, immediate) => {
|
|
|
203
197
|
clearTimeout(timeout);
|
|
204
198
|
timeout = null;
|
|
205
199
|
};
|
|
200
|
+
debounced.setCallback = (newFunc) => {
|
|
201
|
+
currentFunc = newFunc;
|
|
202
|
+
};
|
|
206
203
|
return debounced;
|
|
207
204
|
};
|
|
208
205
|
|
|
209
|
-
const isObject = (object) => {
|
|
210
|
-
return object !== null && !Array.isArray(object) && typeof object === 'object';
|
|
211
|
-
};
|
|
212
|
-
const deepEqual = (object1, object2) => {
|
|
213
|
-
if (object1 === object2) {
|
|
214
|
-
return true;
|
|
215
|
-
}
|
|
216
|
-
if (Array.isArray(object1) && Array.isArray(object2)) {
|
|
217
|
-
if (object1.length !== object2.length) {
|
|
218
|
-
return false;
|
|
219
|
-
}
|
|
220
|
-
return object1.every((val, index) => deepEqual(val, object2[index]));
|
|
221
|
-
}
|
|
222
|
-
if (Array.isArray(object1) !== Array.isArray(object2)) {
|
|
223
|
-
return false;
|
|
224
|
-
}
|
|
225
|
-
if (!isObject(object1) || !isObject(object2)) {
|
|
226
|
-
return object1 === object2;
|
|
227
|
-
}
|
|
228
|
-
const keys1 = Object.keys(object1);
|
|
229
|
-
const keys2 = Object.keys(object2);
|
|
230
|
-
if (keys1.length !== keys2.length) {
|
|
231
|
-
return false;
|
|
232
|
-
}
|
|
233
|
-
return keys1.every((key) => deepEqual(object1[key], object2[key]));
|
|
234
|
-
};
|
|
235
|
-
|
|
236
206
|
const isScrollable = (node) => {
|
|
237
207
|
if (!(node instanceof HTMLElement || node instanceof SVGElement)) {
|
|
238
208
|
return false;
|
|
@@ -273,17 +243,43 @@ const useIsomorphicLayoutEffect = isHopefullyDomEnvironment ? React.useLayoutEff
|
|
|
273
243
|
const clearTimeoutRef = (ref) => {
|
|
274
244
|
if (ref.current) {
|
|
275
245
|
clearTimeout(ref.current);
|
|
276
|
-
// eslint-disable-next-line no-param-reassign
|
|
277
246
|
ref.current = null;
|
|
278
247
|
}
|
|
279
248
|
};
|
|
280
249
|
|
|
250
|
+
function parseDataTooltipIdSelector(selector) {
|
|
251
|
+
const match = selector.match(/^\[data-tooltip-id=(['"])((?:\\.|(?!\1).)*)\1\]$/);
|
|
252
|
+
if (!match) {
|
|
253
|
+
return null;
|
|
254
|
+
}
|
|
255
|
+
return match[2].replace(/\\(['"])/g, '$1');
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
function resolveDataTooltipAnchor(targetElement, tooltipId) {
|
|
259
|
+
let currentElement = targetElement;
|
|
260
|
+
while (currentElement) {
|
|
261
|
+
if (currentElement.dataset.tooltipId === tooltipId) {
|
|
262
|
+
return currentElement;
|
|
263
|
+
}
|
|
264
|
+
currentElement = currentElement.parentElement;
|
|
265
|
+
}
|
|
266
|
+
return null;
|
|
267
|
+
}
|
|
268
|
+
|
|
281
269
|
var coreStyles = {"tooltip":"core-styles-module_tooltip__3vRRp","fixed":"core-styles-module_fixed__pcSol","arrow":"core-styles-module_arrow__cvMwQ","content":"core-styles-module_content__BRKdB","noArrow":"core-styles-module_noArrow__xock6","clickable":"core-styles-module_clickable__ZuTTB","show":"core-styles-module_show__Nt9eE","closing":"core-styles-module_closing__sGnxF"};
|
|
282
270
|
|
|
283
271
|
var styles = {"tooltip":"styles-module_tooltip__mnnfp","content":"styles-module_content__ydYdI","arrow":"styles-module_arrow__K0L3T","dark":"styles-module_dark__xNqje","light":"styles-module_light__Z6W-X","success":"styles-module_success__A2AKt","warning":"styles-module_warning__SCK0X","error":"styles-module_error__JvumD","info":"styles-module_info__BWdHW"};
|
|
284
272
|
|
|
285
273
|
const registry = new Map();
|
|
286
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
|
+
}
|
|
287
283
|
function areAnchorListsEqual(left, right) {
|
|
288
284
|
if (left.length !== right.length) {
|
|
289
285
|
return false;
|
|
@@ -305,8 +301,7 @@ function readAnchorsForSelector(selector) {
|
|
|
305
301
|
}
|
|
306
302
|
}
|
|
307
303
|
function notifySubscribers(entry) {
|
|
308
|
-
|
|
309
|
-
entry.subscribers.forEach((subscriber) => subscriber(anchors, entry.error));
|
|
304
|
+
entry.subscribers.forEach((subscriber) => subscriber(entry.anchors, entry.error));
|
|
310
305
|
}
|
|
311
306
|
function refreshEntry(selector, entry) {
|
|
312
307
|
var _a, _b, _c, _d;
|
|
@@ -330,12 +325,117 @@ function refreshAllEntries() {
|
|
|
330
325
|
refreshEntry(selector, entry);
|
|
331
326
|
});
|
|
332
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
|
+
}
|
|
333
432
|
function ensureDocumentObserver() {
|
|
334
433
|
if (documentObserver || typeof MutationObserver === 'undefined') {
|
|
335
434
|
return;
|
|
336
435
|
}
|
|
337
|
-
documentObserver = new MutationObserver(() => {
|
|
338
|
-
|
|
436
|
+
documentObserver = new MutationObserver((records) => {
|
|
437
|
+
const affectedIds = collectAffectedTooltipIds(records);
|
|
438
|
+
scheduleRefresh(affectedIds);
|
|
339
439
|
});
|
|
340
440
|
documentObserver.observe(document.body, {
|
|
341
441
|
childList: true,
|
|
@@ -360,6 +460,7 @@ function subscribeAnchorSelector(selector, subscriber) {
|
|
|
360
460
|
anchors: initialState.anchors,
|
|
361
461
|
error: initialState.error,
|
|
362
462
|
subscribers: new Set(),
|
|
463
|
+
tooltipId: extractTooltipId(selector),
|
|
363
464
|
};
|
|
364
465
|
registry.set(selector, entry);
|
|
365
466
|
}
|
|
@@ -387,7 +488,7 @@ const getAnchorSelector = ({ id, anchorSelect, imperativeAnchorSelect, }) => {
|
|
|
387
488
|
}
|
|
388
489
|
return selector;
|
|
389
490
|
};
|
|
390
|
-
const useTooltipAnchors = ({ id, anchorSelect, imperativeAnchorSelect, activeAnchor, disableTooltip, onActiveAnchorRemoved, }) => {
|
|
491
|
+
const useTooltipAnchors = ({ id, anchorSelect, imperativeAnchorSelect, activeAnchor, disableTooltip, onActiveAnchorRemoved, trackAnchors, }) => {
|
|
391
492
|
const [rawAnchorElements, setRawAnchorElements] = React.useState([]);
|
|
392
493
|
const [selectorError, setSelectorError] = React.useState(null);
|
|
393
494
|
const warnedSelectorRef = React.useRef(null);
|
|
@@ -403,9 +504,10 @@ const useTooltipAnchors = ({ id, anchorSelect, imperativeAnchorSelect, activeAnc
|
|
|
403
504
|
catch (_a) {
|
|
404
505
|
return false;
|
|
405
506
|
}
|
|
406
|
-
|
|
507
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
508
|
+
}, [activeAnchor, selector, anchorElements]);
|
|
407
509
|
React.useEffect(() => {
|
|
408
|
-
if (!selector) {
|
|
510
|
+
if (!selector || !trackAnchors) {
|
|
409
511
|
setRawAnchorElements([]);
|
|
410
512
|
setSelectorError(null);
|
|
411
513
|
return undefined;
|
|
@@ -414,7 +516,7 @@ const useTooltipAnchors = ({ id, anchorSelect, imperativeAnchorSelect, activeAnc
|
|
|
414
516
|
setRawAnchorElements(anchors);
|
|
415
517
|
setSelectorError(error);
|
|
416
518
|
});
|
|
417
|
-
}, [selector]);
|
|
519
|
+
}, [selector, trackAnchors]);
|
|
418
520
|
React.useEffect(() => {
|
|
419
521
|
if (!selectorError || warnedSelectorRef.current === selector) {
|
|
420
522
|
return;
|
|
@@ -434,111 +536,81 @@ const useTooltipAnchors = ({ id, anchorSelect, imperativeAnchorSelect, activeAnc
|
|
|
434
536
|
onActiveAnchorRemoved();
|
|
435
537
|
}
|
|
436
538
|
}, [activeAnchor, anchorElements, activeAnchorMatchesSelector, onActiveAnchorRemoved]);
|
|
437
|
-
return
|
|
539
|
+
return {
|
|
540
|
+
anchorElements,
|
|
541
|
+
selector,
|
|
542
|
+
};
|
|
438
543
|
};
|
|
439
544
|
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
if (tooltipHideDelayTimerRef.current) {
|
|
510
|
-
clearTimeout(tooltipHideDelayTimerRef.current);
|
|
511
|
-
}
|
|
512
|
-
};
|
|
513
|
-
const handleHideTooltip = () => {
|
|
514
|
-
if (clickable) {
|
|
515
|
-
handleHideTooltipDelayed(delayHide || 100);
|
|
516
|
-
}
|
|
517
|
-
else if (delayHide) {
|
|
518
|
-
handleHideTooltipDelayed();
|
|
519
|
-
}
|
|
520
|
-
else {
|
|
521
|
-
handleShow(false);
|
|
522
|
-
}
|
|
523
|
-
if (tooltipShowDelayTimerRef.current) {
|
|
524
|
-
clearTimeout(tooltipShowDelayTimerRef.current);
|
|
525
|
-
}
|
|
526
|
-
};
|
|
527
|
-
const internalDebouncedHandleShowTooltip = debounce(handleShowTooltip, 50);
|
|
528
|
-
const internalDebouncedHandleHideTooltip = debounce(handleHideTooltip, 50);
|
|
529
|
-
const debouncedHandleShowTooltip = (anchor) => {
|
|
530
|
-
internalDebouncedHandleHideTooltip.cancel();
|
|
531
|
-
internalDebouncedHandleShowTooltip(anchor);
|
|
532
|
-
};
|
|
533
|
-
const debouncedHandleHideTooltip = () => {
|
|
534
|
-
internalDebouncedHandleShowTooltip.cancel();
|
|
535
|
-
internalDebouncedHandleHideTooltip();
|
|
536
|
-
};
|
|
537
|
-
const handleScrollResize = () => {
|
|
538
|
-
handleShow(false);
|
|
539
|
-
};
|
|
540
|
-
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);
|
|
541
|
-
const actualOpenEvents = openEvents
|
|
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
|
+
|
|
591
|
+
const useTooltipEvents = ({ activeAnchor, anchorElements, anchorSelector, clickable, closeEvents, delayHide, delayShow, disableTooltip, float, globalCloseEvents, handleHideTooltipDelayed, handleShow, handleShowTooltipDelayed, handleTooltipPosition, hoveringTooltip, imperativeModeOnly, lastFloatPosition, openEvents, openOnClick, rendered, setActiveAnchor, show, tooltipHideDelayTimerRef, tooltipRef, tooltipShowDelayTimerRef, updateTooltipPosition, }) => {
|
|
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
|
|
542
614
|
? { ...openEvents }
|
|
543
615
|
: {
|
|
544
616
|
mouseenter: true,
|
|
@@ -548,13 +620,25 @@ const useTooltipEvents = ({ activeAnchor, anchorElements, clickable, closeEvents
|
|
|
548
620
|
mousedown: false,
|
|
549
621
|
};
|
|
550
622
|
if (!openEvents && openOnClick) {
|
|
551
|
-
Object.assign(
|
|
623
|
+
Object.assign(events, {
|
|
552
624
|
mouseenter: false,
|
|
553
625
|
focus: false,
|
|
554
626
|
click: true,
|
|
555
627
|
});
|
|
556
628
|
}
|
|
557
|
-
|
|
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
|
|
558
642
|
? { ...closeEvents }
|
|
559
643
|
: {
|
|
560
644
|
mouseleave: true,
|
|
@@ -564,12 +648,24 @@ const useTooltipEvents = ({ activeAnchor, anchorElements, clickable, closeEvents
|
|
|
564
648
|
mouseup: false,
|
|
565
649
|
};
|
|
566
650
|
if (!closeEvents && openOnClick) {
|
|
567
|
-
Object.assign(
|
|
651
|
+
Object.assign(events, {
|
|
568
652
|
mouseleave: false,
|
|
569
653
|
blur: false,
|
|
570
654
|
});
|
|
571
655
|
}
|
|
572
|
-
|
|
656
|
+
if (imperativeModeOnly) {
|
|
657
|
+
Object.assign(events, {
|
|
658
|
+
mouseleave: false,
|
|
659
|
+
blur: false,
|
|
660
|
+
click: false,
|
|
661
|
+
dblclick: false,
|
|
662
|
+
mouseup: false,
|
|
663
|
+
});
|
|
664
|
+
}
|
|
665
|
+
return events;
|
|
666
|
+
}, [closeEvents, openOnClick, imperativeModeOnly]);
|
|
667
|
+
const actualGlobalCloseEvents = React.useMemo(() => {
|
|
668
|
+
const events = globalCloseEvents
|
|
573
669
|
? { ...globalCloseEvents }
|
|
574
670
|
: {
|
|
575
671
|
escape: false,
|
|
@@ -578,108 +674,157 @@ const useTooltipEvents = ({ activeAnchor, anchorElements, clickable, closeEvents
|
|
|
578
674
|
clickOutsideAnchor: hasClickEvent || false,
|
|
579
675
|
};
|
|
580
676
|
if (imperativeModeOnly) {
|
|
581
|
-
Object.assign(
|
|
582
|
-
mouseenter: false,
|
|
583
|
-
focus: false,
|
|
584
|
-
click: false,
|
|
585
|
-
dblclick: false,
|
|
586
|
-
mousedown: false,
|
|
587
|
-
});
|
|
588
|
-
Object.assign(actualCloseEvents, {
|
|
589
|
-
mouseleave: false,
|
|
590
|
-
blur: false,
|
|
591
|
-
click: false,
|
|
592
|
-
dblclick: false,
|
|
593
|
-
mouseup: false,
|
|
594
|
-
});
|
|
595
|
-
Object.assign(actualGlobalCloseEvents, {
|
|
677
|
+
Object.assign(events, {
|
|
596
678
|
escape: false,
|
|
597
679
|
scroll: false,
|
|
598
680
|
resize: false,
|
|
599
681
|
clickOutsideAnchor: false,
|
|
600
682
|
});
|
|
601
683
|
}
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
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;
|
|
609
709
|
}
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
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
|
+
}
|
|
613
715
|
}
|
|
614
|
-
else if (
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
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
|
+
}
|
|
620
728
|
}
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
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;
|
|
734
|
+
}
|
|
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);
|
|
624
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 {
|
|
625
775
|
handleShow(false);
|
|
626
|
-
};
|
|
627
|
-
if (actualGlobalCloseEvents.escape) {
|
|
628
|
-
window.addEventListener('keydown', handleEsc);
|
|
629
776
|
}
|
|
630
|
-
if (
|
|
631
|
-
|
|
777
|
+
if (tooltipShowDelayTimerRef.current) {
|
|
778
|
+
clearTimeout(tooltipShowDelayTimerRef.current);
|
|
632
779
|
}
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
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));
|
|
644
794
|
};
|
|
645
|
-
const
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
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();
|
|
650
803
|
};
|
|
651
|
-
const regularEvents = ['mouseover', 'mouseout', 'mouseenter', 'mouseleave', 'focus', 'blur'];
|
|
652
|
-
const clickEvents = ['click', 'dblclick', 'mousedown', 'mouseup'];
|
|
653
|
-
const delegatedEvents = [];
|
|
654
804
|
const addDelegatedHoverOpenListener = () => {
|
|
655
|
-
|
|
656
|
-
event
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
}
|
|
666
|
-
debouncedHandleShowTooltip(anchor);
|
|
667
|
-
},
|
|
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);
|
|
668
815
|
});
|
|
669
816
|
};
|
|
670
817
|
const addDelegatedHoverCloseListener = () => {
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
debouncedHandleHideTooltip();
|
|
682
|
-
},
|
|
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();
|
|
683
828
|
});
|
|
684
829
|
};
|
|
685
830
|
if (actualOpenEvents.mouseenter) {
|
|
@@ -695,37 +840,48 @@ const useTooltipEvents = ({ activeAnchor, anchorElements, clickable, closeEvents
|
|
|
695
840
|
addDelegatedHoverCloseListener();
|
|
696
841
|
}
|
|
697
842
|
if (actualOpenEvents.focus) {
|
|
698
|
-
|
|
699
|
-
event
|
|
700
|
-
listener: (event) => {
|
|
701
|
-
debouncedHandleShowTooltip(resolveAnchorElement(event.target));
|
|
702
|
-
},
|
|
843
|
+
addDelegatedListener('focusin', (event) => {
|
|
844
|
+
debouncedHandleShowTooltip(resolveAnchorElementRef.current(event.target));
|
|
703
845
|
});
|
|
704
846
|
}
|
|
705
847
|
if (actualCloseEvents.blur) {
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
debouncedHandleHideTooltip();
|
|
717
|
-
},
|
|
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();
|
|
718
858
|
});
|
|
719
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
|
+
};
|
|
720
879
|
Object.entries(actualOpenEvents).forEach(([event, enabled]) => {
|
|
721
880
|
if (!enabled || regularEvents.includes(event)) {
|
|
722
881
|
return;
|
|
723
882
|
}
|
|
724
883
|
if (clickEvents.includes(event)) {
|
|
725
|
-
|
|
726
|
-
event,
|
|
727
|
-
listener: handleClickOpenTooltipAnchor,
|
|
728
|
-
});
|
|
884
|
+
addDelegatedListener(event, handleClickOpenTooltipAnchor);
|
|
729
885
|
}
|
|
730
886
|
});
|
|
731
887
|
Object.entries(actualCloseEvents).forEach(([event, enabled]) => {
|
|
@@ -733,38 +889,113 @@ const useTooltipEvents = ({ activeAnchor, anchorElements, clickable, closeEvents
|
|
|
733
889
|
return;
|
|
734
890
|
}
|
|
735
891
|
if (clickEvents.includes(event)) {
|
|
736
|
-
|
|
737
|
-
event,
|
|
738
|
-
listener: handleClickCloseTooltipAnchor,
|
|
739
|
-
});
|
|
892
|
+
addDelegatedListener(event, handleClickCloseTooltipAnchor);
|
|
740
893
|
}
|
|
741
894
|
});
|
|
742
895
|
if (float) {
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
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;
|
|
746
912
|
});
|
|
747
913
|
}
|
|
914
|
+
const tooltipElement = tooltipRef.current;
|
|
748
915
|
const handleMouseOverTooltip = () => {
|
|
749
|
-
// eslint-disable-next-line no-param-reassign
|
|
750
916
|
hoveringTooltip.current = true;
|
|
751
917
|
};
|
|
752
918
|
const handleMouseOutTooltip = () => {
|
|
753
|
-
// eslint-disable-next-line no-param-reassign
|
|
754
919
|
hoveringTooltip.current = false;
|
|
755
|
-
|
|
920
|
+
handleHideTooltipRef.current();
|
|
756
921
|
};
|
|
757
922
|
const addHoveringTooltipListeners = clickable && (actualCloseEvents.mouseout || actualCloseEvents.mouseleave);
|
|
758
923
|
if (addHoveringTooltipListeners) {
|
|
759
924
|
tooltipElement === null || tooltipElement === void 0 ? void 0 : tooltipElement.addEventListener('mouseover', handleMouseOverTooltip);
|
|
760
925
|
tooltipElement === null || tooltipElement === void 0 ? void 0 : tooltipElement.addEventListener('mouseout', handleMouseOutTooltip);
|
|
761
926
|
}
|
|
762
|
-
delegatedEvents.forEach(({ event, listener }) => {
|
|
763
|
-
document.addEventListener(event, listener);
|
|
764
|
-
});
|
|
765
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
|
+
// `rendered` needs to be a dependency because `tooltipRef` becomes stale when the
|
|
937
|
+
// tooltip is removed from / added to the DOM.
|
|
938
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
939
|
+
}, [actualOpenEvents, actualCloseEvents, float, clickable, rendered]);
|
|
940
|
+
// --- Effect 2: Global close events + auto-update ---
|
|
941
|
+
// Re-runs when the global close config changes, or when the active anchor changes
|
|
942
|
+
// (for scroll parent listeners and floating-ui autoUpdate).
|
|
943
|
+
React.useEffect(() => {
|
|
944
|
+
const handleScrollResize = () => {
|
|
945
|
+
handleShowRef.current(false);
|
|
946
|
+
};
|
|
947
|
+
const tooltipScrollParent = tooltipScrollParentRef.current;
|
|
948
|
+
const anchorScrollParent = anchorScrollParentRef.current;
|
|
949
|
+
if (actualGlobalCloseEvents.scroll) {
|
|
950
|
+
window.addEventListener('scroll', handleScrollResize);
|
|
951
|
+
anchorScrollParent === null || anchorScrollParent === void 0 ? void 0 : anchorScrollParent.addEventListener('scroll', handleScrollResize);
|
|
952
|
+
tooltipScrollParent === null || tooltipScrollParent === void 0 ? void 0 : tooltipScrollParent.addEventListener('scroll', handleScrollResize);
|
|
953
|
+
}
|
|
954
|
+
let updateTooltipCleanup = null;
|
|
955
|
+
if (actualGlobalCloseEvents.resize) {
|
|
956
|
+
window.addEventListener('resize', handleScrollResize);
|
|
957
|
+
}
|
|
958
|
+
else if (activeAnchor && tooltipRef.current) {
|
|
959
|
+
updateTooltipCleanup = dom.autoUpdate(activeAnchor, tooltipRef.current, () => updateTooltipPositionRef.current(), {
|
|
960
|
+
ancestorResize: true,
|
|
961
|
+
elementResize: true,
|
|
962
|
+
layoutShift: true,
|
|
963
|
+
});
|
|
964
|
+
}
|
|
965
|
+
const handleEsc = (event) => {
|
|
966
|
+
if (event.key !== 'Escape') {
|
|
967
|
+
return;
|
|
968
|
+
}
|
|
969
|
+
handleShowRef.current(false);
|
|
970
|
+
};
|
|
971
|
+
if (actualGlobalCloseEvents.escape) {
|
|
972
|
+
window.addEventListener('keydown', handleEsc);
|
|
973
|
+
}
|
|
974
|
+
const handleClickOutsideAnchors = (event) => {
|
|
975
|
+
var _a, _b;
|
|
976
|
+
if (!showRef.current) {
|
|
977
|
+
return;
|
|
978
|
+
}
|
|
979
|
+
const target = event.target;
|
|
980
|
+
if (!(target === null || target === void 0 ? void 0 : target.isConnected)) {
|
|
981
|
+
return;
|
|
982
|
+
}
|
|
983
|
+
if ((_a = tooltipRef.current) === null || _a === void 0 ? void 0 : _a.contains(target)) {
|
|
984
|
+
return;
|
|
985
|
+
}
|
|
986
|
+
if ((_b = activeAnchorRef.current) === null || _b === void 0 ? void 0 : _b.contains(target)) {
|
|
987
|
+
return;
|
|
988
|
+
}
|
|
989
|
+
if (anchorElementsRef.current.some((anchor) => anchor === null || anchor === void 0 ? void 0 : anchor.contains(target))) {
|
|
990
|
+
return;
|
|
991
|
+
}
|
|
992
|
+
handleShowRef.current(false);
|
|
766
993
|
clearTimeoutRef(tooltipShowDelayTimerRef);
|
|
767
|
-
|
|
994
|
+
};
|
|
995
|
+
if (actualGlobalCloseEvents.clickOutsideAnchor) {
|
|
996
|
+
window.addEventListener('click', handleClickOutsideAnchors);
|
|
997
|
+
}
|
|
998
|
+
return () => {
|
|
768
999
|
if (actualGlobalCloseEvents.scroll) {
|
|
769
1000
|
window.removeEventListener('scroll', handleScrollResize);
|
|
770
1001
|
anchorScrollParent === null || anchorScrollParent === void 0 ? void 0 : anchorScrollParent.removeEventListener('scroll', handleScrollResize);
|
|
@@ -776,50 +1007,19 @@ const useTooltipEvents = ({ activeAnchor, anchorElements, clickable, closeEvents
|
|
|
776
1007
|
if (updateTooltipCleanup) {
|
|
777
1008
|
updateTooltipCleanup();
|
|
778
1009
|
}
|
|
779
|
-
if (actualGlobalCloseEvents.clickOutsideAnchor) {
|
|
780
|
-
window.removeEventListener('click', handleClickOutsideAnchors);
|
|
781
|
-
}
|
|
782
1010
|
if (actualGlobalCloseEvents.escape) {
|
|
783
1011
|
window.removeEventListener('keydown', handleEsc);
|
|
784
1012
|
}
|
|
785
|
-
if (
|
|
786
|
-
|
|
787
|
-
tooltipElement === null || tooltipElement === void 0 ? void 0 : tooltipElement.removeEventListener('mouseout', handleMouseOutTooltip);
|
|
1013
|
+
if (actualGlobalCloseEvents.clickOutsideAnchor) {
|
|
1014
|
+
window.removeEventListener('click', handleClickOutsideAnchors);
|
|
788
1015
|
}
|
|
789
|
-
delegatedEvents.forEach(({ event, listener }) => {
|
|
790
|
-
document.removeEventListener(event, listener);
|
|
791
|
-
});
|
|
792
|
-
internalDebouncedHandleShowTooltip.cancel();
|
|
793
|
-
internalDebouncedHandleHideTooltip.cancel();
|
|
794
1016
|
};
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
anchorElements,
|
|
798
|
-
clickable,
|
|
799
|
-
closeEvents,
|
|
800
|
-
delayHide,
|
|
801
|
-
delayShow,
|
|
802
|
-
disableTooltip,
|
|
803
|
-
float,
|
|
804
|
-
globalCloseEvents,
|
|
805
|
-
handleHideTooltipDelayed,
|
|
806
|
-
handleShow,
|
|
807
|
-
handleShowTooltipDelayed,
|
|
808
|
-
handleTooltipPosition,
|
|
809
|
-
imperativeModeOnly,
|
|
810
|
-
lastFloatPosition,
|
|
811
|
-
openEvents,
|
|
812
|
-
openOnClick,
|
|
813
|
-
setActiveAnchor,
|
|
814
|
-
show,
|
|
815
|
-
tooltipHideDelayTimerRef,
|
|
816
|
-
tooltipRef,
|
|
817
|
-
tooltipShowDelayTimerRef,
|
|
818
|
-
updateTooltipPosition,
|
|
819
|
-
hoveringTooltip,
|
|
820
|
-
]);
|
|
1017
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
1018
|
+
}, [actualGlobalCloseEvents, activeAnchor]);
|
|
821
1019
|
};
|
|
822
1020
|
|
|
1021
|
+
// Shared across all tooltip instances — the CSS variable is on :root and never changes per-instance
|
|
1022
|
+
let globalTransitionShowDelay = null;
|
|
823
1023
|
const Tooltip = ({
|
|
824
1024
|
// props
|
|
825
1025
|
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,
|
|
@@ -844,6 +1044,18 @@ content, contentWrapperRef, isOpen, defaultIsOpen = false, setIsOpen, previousAc
|
|
|
844
1044
|
const lastFloatPosition = React.useRef(null);
|
|
845
1045
|
const hoveringTooltip = React.useRef(false);
|
|
846
1046
|
const mounted = React.useRef(false);
|
|
1047
|
+
const virtualElementRef = React.useRef({
|
|
1048
|
+
getBoundingClientRect: () => ({
|
|
1049
|
+
x: 0,
|
|
1050
|
+
y: 0,
|
|
1051
|
+
width: 0,
|
|
1052
|
+
height: 0,
|
|
1053
|
+
top: 0,
|
|
1054
|
+
left: 0,
|
|
1055
|
+
right: 0,
|
|
1056
|
+
bottom: 0,
|
|
1057
|
+
}),
|
|
1058
|
+
});
|
|
847
1059
|
/**
|
|
848
1060
|
* useLayoutEffect runs before useEffect,
|
|
849
1061
|
* but should be used carefully because of caveats
|
|
@@ -904,7 +1116,6 @@ content, contentWrapperRef, isOpen, defaultIsOpen = false, setIsOpen, previousAc
|
|
|
904
1116
|
else {
|
|
905
1117
|
removeAriaDescribedBy(activeAnchor);
|
|
906
1118
|
}
|
|
907
|
-
// eslint-disable-next-line consistent-return
|
|
908
1119
|
return () => {
|
|
909
1120
|
// cleanup aria-describedby when the tooltip is closed
|
|
910
1121
|
removeAriaDescribedBy(activeAnchor);
|
|
@@ -942,8 +1153,11 @@ content, contentWrapperRef, isOpen, defaultIsOpen = false, setIsOpen, previousAc
|
|
|
942
1153
|
/**
|
|
943
1154
|
* see `onTransitionEnd` on tooltip wrapper
|
|
944
1155
|
*/
|
|
945
|
-
|
|
946
|
-
|
|
1156
|
+
if (globalTransitionShowDelay === null) {
|
|
1157
|
+
const style = getComputedStyle(document.body);
|
|
1158
|
+
globalTransitionShowDelay = cssTimeToMs(style.getPropertyValue('--rt-transition-show-delay'));
|
|
1159
|
+
}
|
|
1160
|
+
const transitionShowDelay = globalTransitionShowDelay;
|
|
947
1161
|
missedTransitionTimerRef.current = setTimeout(() => {
|
|
948
1162
|
/**
|
|
949
1163
|
* if the tooltip switches from `show === true` to `show === false` too fast
|
|
@@ -974,9 +1188,26 @@ content, contentWrapperRef, isOpen, defaultIsOpen = false, setIsOpen, previousAc
|
|
|
974
1188
|
if (!mounted.current) {
|
|
975
1189
|
return;
|
|
976
1190
|
}
|
|
977
|
-
setComputedPosition((oldComputedPosition) =>
|
|
978
|
-
|
|
979
|
-
|
|
1191
|
+
setComputedPosition((oldComputedPosition) => {
|
|
1192
|
+
if (oldComputedPosition.place === newComputedPosition.place &&
|
|
1193
|
+
oldComputedPosition.tooltipStyles.left === newComputedPosition.tooltipStyles.left &&
|
|
1194
|
+
oldComputedPosition.tooltipStyles.top === newComputedPosition.tooltipStyles.top &&
|
|
1195
|
+
oldComputedPosition.tooltipStyles.border === newComputedPosition.tooltipStyles.border &&
|
|
1196
|
+
oldComputedPosition.tooltipArrowStyles.left ===
|
|
1197
|
+
newComputedPosition.tooltipArrowStyles.left &&
|
|
1198
|
+
oldComputedPosition.tooltipArrowStyles.top === newComputedPosition.tooltipArrowStyles.top &&
|
|
1199
|
+
oldComputedPosition.tooltipArrowStyles.right ===
|
|
1200
|
+
newComputedPosition.tooltipArrowStyles.right &&
|
|
1201
|
+
oldComputedPosition.tooltipArrowStyles.bottom ===
|
|
1202
|
+
newComputedPosition.tooltipArrowStyles.bottom &&
|
|
1203
|
+
oldComputedPosition.tooltipArrowStyles.borderBottom ===
|
|
1204
|
+
newComputedPosition.tooltipArrowStyles.borderBottom &&
|
|
1205
|
+
oldComputedPosition.tooltipArrowStyles.borderRight ===
|
|
1206
|
+
newComputedPosition.tooltipArrowStyles.borderRight) {
|
|
1207
|
+
return oldComputedPosition;
|
|
1208
|
+
}
|
|
1209
|
+
return newComputedPosition;
|
|
1210
|
+
});
|
|
980
1211
|
}, []);
|
|
981
1212
|
const handleShowTooltipDelayed = React.useCallback((delay = delayShow) => {
|
|
982
1213
|
if (tooltipShowDelayTimerRef.current) {
|
|
@@ -1004,24 +1235,20 @@ content, contentWrapperRef, isOpen, defaultIsOpen = false, setIsOpen, previousAc
|
|
|
1004
1235
|
}, [delayHide, handleShow]);
|
|
1005
1236
|
const handleTooltipPosition = React.useCallback(({ x, y }) => {
|
|
1006
1237
|
var _a;
|
|
1007
|
-
|
|
1008
|
-
|
|
1009
|
-
|
|
1010
|
-
|
|
1011
|
-
|
|
1012
|
-
|
|
1013
|
-
|
|
1014
|
-
|
|
1015
|
-
|
|
1016
|
-
|
|
1017
|
-
bottom: y,
|
|
1018
|
-
};
|
|
1019
|
-
},
|
|
1020
|
-
};
|
|
1238
|
+
virtualElementRef.current.getBoundingClientRect = () => ({
|
|
1239
|
+
x,
|
|
1240
|
+
y,
|
|
1241
|
+
width: 0,
|
|
1242
|
+
height: 0,
|
|
1243
|
+
top: y,
|
|
1244
|
+
left: x,
|
|
1245
|
+
right: x,
|
|
1246
|
+
bottom: y,
|
|
1247
|
+
});
|
|
1021
1248
|
computeTooltipPosition({
|
|
1022
1249
|
place: (_a = imperativeOptions === null || imperativeOptions === void 0 ? void 0 : imperativeOptions.place) !== null && _a !== void 0 ? _a : place,
|
|
1023
1250
|
offset,
|
|
1024
|
-
elementReference:
|
|
1251
|
+
elementReference: virtualElementRef.current,
|
|
1025
1252
|
tooltipReference: tooltipRef.current,
|
|
1026
1253
|
tooltipArrowReference: tooltipArrowRef.current,
|
|
1027
1254
|
strategy: positionStrategy,
|
|
@@ -1106,17 +1333,24 @@ content, contentWrapperRef, isOpen, defaultIsOpen = false, setIsOpen, previousAc
|
|
|
1106
1333
|
clearTimeoutRef(tooltipHideDelayTimerRef);
|
|
1107
1334
|
clearTimeoutRef(tooltipAutoCloseTimerRef);
|
|
1108
1335
|
}, [handleShow, setActiveAnchor]);
|
|
1109
|
-
const
|
|
1336
|
+
const shouldTrackAnchors = rendered ||
|
|
1337
|
+
defaultIsOpen ||
|
|
1338
|
+
Boolean(isOpen) ||
|
|
1339
|
+
Boolean(activeAnchor) ||
|
|
1340
|
+
Boolean(imperativeOptions === null || imperativeOptions === void 0 ? void 0 : imperativeOptions.anchorSelect);
|
|
1341
|
+
const { anchorElements, selector: anchorSelector } = useTooltipAnchors({
|
|
1110
1342
|
id,
|
|
1111
1343
|
anchorSelect,
|
|
1112
1344
|
imperativeAnchorSelect: imperativeOptions === null || imperativeOptions === void 0 ? void 0 : imperativeOptions.anchorSelect,
|
|
1113
1345
|
activeAnchor,
|
|
1114
1346
|
disableTooltip,
|
|
1115
1347
|
onActiveAnchorRemoved: handleActiveAnchorRemoved,
|
|
1348
|
+
trackAnchors: shouldTrackAnchors,
|
|
1116
1349
|
});
|
|
1117
1350
|
useTooltipEvents({
|
|
1118
1351
|
activeAnchor,
|
|
1119
1352
|
anchorElements,
|
|
1353
|
+
anchorSelector,
|
|
1120
1354
|
clickable,
|
|
1121
1355
|
closeEvents,
|
|
1122
1356
|
delayHide,
|
|
@@ -1133,6 +1367,7 @@ content, contentWrapperRef, isOpen, defaultIsOpen = false, setIsOpen, previousAc
|
|
|
1133
1367
|
lastFloatPosition,
|
|
1134
1368
|
openEvents,
|
|
1135
1369
|
openOnClick,
|
|
1370
|
+
rendered,
|
|
1136
1371
|
setActiveAnchor,
|
|
1137
1372
|
show,
|
|
1138
1373
|
tooltipHideDelayTimerRef,
|
|
@@ -1140,11 +1375,16 @@ content, contentWrapperRef, isOpen, defaultIsOpen = false, setIsOpen, previousAc
|
|
|
1140
1375
|
tooltipShowDelayTimerRef,
|
|
1141
1376
|
updateTooltipPosition,
|
|
1142
1377
|
});
|
|
1378
|
+
const updateTooltipPositionRef = React.useRef(updateTooltipPosition);
|
|
1379
|
+
updateTooltipPositionRef.current = updateTooltipPosition;
|
|
1143
1380
|
React.useEffect(() => {
|
|
1381
|
+
if (!rendered) {
|
|
1382
|
+
return;
|
|
1383
|
+
}
|
|
1144
1384
|
updateTooltipPosition();
|
|
1145
|
-
}, [updateTooltipPosition]);
|
|
1385
|
+
}, [rendered, updateTooltipPosition]);
|
|
1146
1386
|
React.useEffect(() => {
|
|
1147
|
-
if (!(contentWrapperRef === null || contentWrapperRef === void 0 ? void 0 : contentWrapperRef.current)) {
|
|
1387
|
+
if (!rendered || !(contentWrapperRef === null || contentWrapperRef === void 0 ? void 0 : contentWrapperRef.current)) {
|
|
1148
1388
|
return () => null;
|
|
1149
1389
|
}
|
|
1150
1390
|
let timeoutId = null;
|
|
@@ -1155,7 +1395,7 @@ content, contentWrapperRef, isOpen, defaultIsOpen = false, setIsOpen, previousAc
|
|
|
1155
1395
|
}
|
|
1156
1396
|
timeoutId = setTimeout(() => {
|
|
1157
1397
|
if (mounted.current) {
|
|
1158
|
-
|
|
1398
|
+
updateTooltipPositionRef.current();
|
|
1159
1399
|
}
|
|
1160
1400
|
timeoutId = null;
|
|
1161
1401
|
}, 0);
|
|
@@ -1167,9 +1407,13 @@ content, contentWrapperRef, isOpen, defaultIsOpen = false, setIsOpen, previousAc
|
|
|
1167
1407
|
clearTimeout(timeoutId);
|
|
1168
1408
|
}
|
|
1169
1409
|
};
|
|
1170
|
-
}, [content, contentWrapperRef,
|
|
1410
|
+
}, [content, contentWrapperRef, rendered]);
|
|
1171
1411
|
React.useEffect(() => {
|
|
1172
1412
|
var _a;
|
|
1413
|
+
const shouldResolveInitialActiveAnchor = rendered || defaultIsOpen || Boolean(isOpen);
|
|
1414
|
+
if (!shouldResolveInitialActiveAnchor) {
|
|
1415
|
+
return;
|
|
1416
|
+
}
|
|
1173
1417
|
const activeAnchorMatchesImperativeSelector = (() => {
|
|
1174
1418
|
if (!activeAnchor || !(imperativeOptions === null || imperativeOptions === void 0 ? void 0 : imperativeOptions.anchorSelect)) {
|
|
1175
1419
|
return false;
|
|
@@ -1192,7 +1436,15 @@ content, contentWrapperRef, isOpen, defaultIsOpen = false, setIsOpen, previousAc
|
|
|
1192
1436
|
}
|
|
1193
1437
|
setActiveAnchor((_a = anchorElements[0]) !== null && _a !== void 0 ? _a : null);
|
|
1194
1438
|
}
|
|
1195
|
-
}, [
|
|
1439
|
+
}, [
|
|
1440
|
+
activeAnchor,
|
|
1441
|
+
anchorElements,
|
|
1442
|
+
defaultIsOpen,
|
|
1443
|
+
imperativeOptions === null || imperativeOptions === void 0 ? void 0 : imperativeOptions.anchorSelect,
|
|
1444
|
+
isOpen,
|
|
1445
|
+
rendered,
|
|
1446
|
+
setActiveAnchor,
|
|
1447
|
+
]);
|
|
1196
1448
|
React.useEffect(() => {
|
|
1197
1449
|
if (defaultIsOpen) {
|
|
1198
1450
|
handleShow(true);
|
|
@@ -1216,7 +1468,20 @@ content, contentWrapperRef, isOpen, defaultIsOpen = false, setIsOpen, previousAc
|
|
|
1216
1468
|
}, [delayShow, handleShowTooltipDelayed]);
|
|
1217
1469
|
const actualContent = (_a = imperativeOptions === null || imperativeOptions === void 0 ? void 0 : imperativeOptions.content) !== null && _a !== void 0 ? _a : content;
|
|
1218
1470
|
const hasContent = actualContent !== null && actualContent !== undefined;
|
|
1219
|
-
const canShow = show &&
|
|
1471
|
+
const canShow = show && computedPosition.tooltipStyles.left !== undefined;
|
|
1472
|
+
const tooltipStyle = React.useMemo(() => ({
|
|
1473
|
+
...externalStyles,
|
|
1474
|
+
...computedPosition.tooltipStyles,
|
|
1475
|
+
opacity: opacity !== undefined && canShow ? opacity : undefined,
|
|
1476
|
+
}), [externalStyles, computedPosition.tooltipStyles, opacity, canShow]);
|
|
1477
|
+
const arrowBackground = React.useMemo(() => arrowColor
|
|
1478
|
+
? `linear-gradient(to right bottom, transparent 50%, ${arrowColor} 50%)`
|
|
1479
|
+
: undefined, [arrowColor]);
|
|
1480
|
+
const arrowStyle = React.useMemo(() => ({
|
|
1481
|
+
...computedPosition.tooltipArrowStyles,
|
|
1482
|
+
background: arrowBackground,
|
|
1483
|
+
'--rt-arrow-size': `${arrowSize}px`,
|
|
1484
|
+
}), [computedPosition.tooltipArrowStyles, arrowBackground, arrowSize]);
|
|
1220
1485
|
React.useImperativeHandle(forwardRef, () => ({
|
|
1221
1486
|
open: (options) => {
|
|
1222
1487
|
let imperativeAnchor = null;
|
|
@@ -1271,19 +1536,9 @@ content, contentWrapperRef, isOpen, defaultIsOpen = false, setIsOpen, previousAc
|
|
|
1271
1536
|
setRendered(false);
|
|
1272
1537
|
setImperativeOptions(null);
|
|
1273
1538
|
afterHide === null || afterHide === void 0 ? void 0 : afterHide();
|
|
1274
|
-
}, style:
|
|
1275
|
-
...externalStyles,
|
|
1276
|
-
...computedPosition.tooltipStyles,
|
|
1277
|
-
opacity: opacity !== undefined && canShow ? opacity : undefined,
|
|
1278
|
-
}, ref: tooltipRef },
|
|
1539
|
+
}, style: tooltipStyle, ref: tooltipRef },
|
|
1279
1540
|
React.createElement(WrapperElement, { className: clsx('react-tooltip-content-wrapper', coreStyles['content'], styles['content']) }, actualContent),
|
|
1280
|
-
React.createElement(WrapperElement, { className: clsx('react-tooltip-arrow', coreStyles['arrow'], styles['arrow'], classNameArrow, noArrow && coreStyles['noArrow']), style:
|
|
1281
|
-
...computedPosition.tooltipArrowStyles,
|
|
1282
|
-
background: arrowColor
|
|
1283
|
-
? `linear-gradient(to right bottom, transparent 50%, ${arrowColor} 50%)`
|
|
1284
|
-
: undefined,
|
|
1285
|
-
'--rt-arrow-size': `${arrowSize}px`,
|
|
1286
|
-
}, ref: tooltipArrowRef }))) : null;
|
|
1541
|
+
React.createElement(WrapperElement, { className: clsx('react-tooltip-arrow', coreStyles['arrow'], styles['arrow'], classNameArrow, noArrow && coreStyles['noArrow']), style: arrowStyle, ref: tooltipArrowRef }))) : null;
|
|
1287
1542
|
if (!tooltipNode) {
|
|
1288
1543
|
return null;
|
|
1289
1544
|
}
|
|
@@ -1294,12 +1549,82 @@ content, contentWrapperRef, isOpen, defaultIsOpen = false, setIsOpen, previousAc
|
|
|
1294
1549
|
};
|
|
1295
1550
|
var Tooltip$1 = React.memo(Tooltip);
|
|
1296
1551
|
|
|
1552
|
+
/**
|
|
1553
|
+
* Shared MutationObserver for data-tooltip-* attribute changes.
|
|
1554
|
+
* Instead of N observers (one per tooltip), a single observer watches
|
|
1555
|
+
* all active anchors and dispatches changes to registered callbacks.
|
|
1556
|
+
*/
|
|
1557
|
+
const observedElements = new Map();
|
|
1558
|
+
let sharedObserver = null;
|
|
1559
|
+
const observerConfig = {
|
|
1560
|
+
attributes: true,
|
|
1561
|
+
childList: false,
|
|
1562
|
+
subtree: false,
|
|
1563
|
+
};
|
|
1564
|
+
function getObserver() {
|
|
1565
|
+
if (!sharedObserver) {
|
|
1566
|
+
sharedObserver = new MutationObserver((mutationList) => {
|
|
1567
|
+
var _a;
|
|
1568
|
+
for (const mutation of mutationList) {
|
|
1569
|
+
if (mutation.type !== 'attributes' ||
|
|
1570
|
+
!((_a = mutation.attributeName) === null || _a === void 0 ? void 0 : _a.startsWith('data-tooltip-'))) {
|
|
1571
|
+
continue;
|
|
1572
|
+
}
|
|
1573
|
+
const target = mutation.target;
|
|
1574
|
+
const callbacks = observedElements.get(target);
|
|
1575
|
+
if (callbacks) {
|
|
1576
|
+
callbacks.forEach((cb) => cb(target));
|
|
1577
|
+
}
|
|
1578
|
+
}
|
|
1579
|
+
});
|
|
1580
|
+
}
|
|
1581
|
+
return sharedObserver;
|
|
1582
|
+
}
|
|
1583
|
+
function observeAnchorAttributes(element, callback) {
|
|
1584
|
+
const observer = getObserver();
|
|
1585
|
+
let callbacks = observedElements.get(element);
|
|
1586
|
+
if (!callbacks) {
|
|
1587
|
+
callbacks = new Set();
|
|
1588
|
+
observedElements.set(element, callbacks);
|
|
1589
|
+
observer.observe(element, observerConfig);
|
|
1590
|
+
}
|
|
1591
|
+
callbacks.add(callback);
|
|
1592
|
+
return () => {
|
|
1593
|
+
const cbs = observedElements.get(element);
|
|
1594
|
+
if (cbs) {
|
|
1595
|
+
cbs.delete(callback);
|
|
1596
|
+
if (cbs.size === 0) {
|
|
1597
|
+
observedElements.delete(element);
|
|
1598
|
+
// MutationObserver doesn't have unobserve — if no elements left, disconnect & reset
|
|
1599
|
+
if (observedElements.size === 0) {
|
|
1600
|
+
observer.disconnect();
|
|
1601
|
+
}
|
|
1602
|
+
else {
|
|
1603
|
+
// Re-observe remaining elements (MutationObserver has no per-target unobserve)
|
|
1604
|
+
observer.disconnect();
|
|
1605
|
+
observedElements.forEach((_cbs, el) => {
|
|
1606
|
+
observer.observe(el, observerConfig);
|
|
1607
|
+
});
|
|
1608
|
+
}
|
|
1609
|
+
}
|
|
1610
|
+
}
|
|
1611
|
+
};
|
|
1612
|
+
}
|
|
1613
|
+
|
|
1297
1614
|
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) => {
|
|
1298
1615
|
var _a, _b, _c, _d, _e, _f, _g, _h;
|
|
1299
1616
|
const [activeAnchor, setActiveAnchor] = React.useState(null);
|
|
1300
1617
|
const [anchorDataAttributes, setAnchorDataAttributes] = React.useState({});
|
|
1301
1618
|
const previousActiveAnchorRef = React.useRef(null);
|
|
1302
1619
|
const styleInjectionRef = React.useRef(disableStyleInjection);
|
|
1620
|
+
const handleSetActiveAnchor = React.useCallback((anchor) => {
|
|
1621
|
+
setActiveAnchor((prev) => {
|
|
1622
|
+
if (!(anchor === null || anchor === void 0 ? void 0 : anchor.isSameNode(prev))) {
|
|
1623
|
+
previousActiveAnchorRef.current = prev;
|
|
1624
|
+
}
|
|
1625
|
+
return anchor;
|
|
1626
|
+
});
|
|
1627
|
+
}, []);
|
|
1303
1628
|
/* c8 ignore start */
|
|
1304
1629
|
const getDataAttributesFromAnchorElement = (elementReference) => {
|
|
1305
1630
|
const dataAttributes = elementReference === null || elementReference === void 0 ? void 0 : elementReference.getAttributeNames().reduce((acc, name) => {
|
|
@@ -1331,37 +1656,24 @@ const TooltipController = React.forwardRef(({ id, anchorSelect, content, render,
|
|
|
1331
1656
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
1332
1657
|
}, []);
|
|
1333
1658
|
React.useEffect(() => {
|
|
1334
|
-
|
|
1335
|
-
mutationList.forEach((mutation) => {
|
|
1336
|
-
var _a;
|
|
1337
|
-
if (!activeAnchor ||
|
|
1338
|
-
mutation.type !== 'attributes' ||
|
|
1339
|
-
!((_a = mutation.attributeName) === null || _a === void 0 ? void 0 : _a.startsWith('data-tooltip-'))) {
|
|
1340
|
-
return;
|
|
1341
|
-
}
|
|
1342
|
-
// make sure to get all set attributes, since all unset attributes are reset
|
|
1343
|
-
const dataAttributes = getDataAttributesFromAnchorElement(activeAnchor);
|
|
1344
|
-
setAnchorDataAttributes(dataAttributes);
|
|
1345
|
-
});
|
|
1346
|
-
};
|
|
1347
|
-
// Create an observer instance linked to the callback function
|
|
1348
|
-
const observer = new MutationObserver(observerCallback);
|
|
1349
|
-
// do not check for subtree and childrens, we only want to know attribute changes
|
|
1350
|
-
// to stay watching `data-attributes-*` from anchor element
|
|
1351
|
-
const observerConfig = { attributes: true, childList: false, subtree: false };
|
|
1352
|
-
if (activeAnchor) {
|
|
1353
|
-
const dataAttributes = getDataAttributesFromAnchorElement(activeAnchor);
|
|
1354
|
-
setAnchorDataAttributes(dataAttributes);
|
|
1355
|
-
// Start observing the target node for configured mutations
|
|
1356
|
-
observer.observe(activeAnchor, observerConfig);
|
|
1357
|
-
}
|
|
1358
|
-
else {
|
|
1659
|
+
if (!activeAnchor) {
|
|
1359
1660
|
setAnchorDataAttributes({});
|
|
1661
|
+
return () => { };
|
|
1360
1662
|
}
|
|
1361
|
-
|
|
1362
|
-
|
|
1363
|
-
|
|
1663
|
+
const updateAttributes = (element) => {
|
|
1664
|
+
const attrs = getDataAttributesFromAnchorElement(element);
|
|
1665
|
+
setAnchorDataAttributes((prev) => {
|
|
1666
|
+
const keys = Object.keys(attrs);
|
|
1667
|
+
const prevKeys = Object.keys(prev);
|
|
1668
|
+
if (keys.length === prevKeys.length && keys.every((key) => attrs[key] === prev[key])) {
|
|
1669
|
+
return prev;
|
|
1670
|
+
}
|
|
1671
|
+
return attrs;
|
|
1672
|
+
});
|
|
1364
1673
|
};
|
|
1674
|
+
updateAttributes(activeAnchor);
|
|
1675
|
+
const unsubscribe = observeAnchorAttributes(activeAnchor, updateAttributes);
|
|
1676
|
+
return unsubscribe;
|
|
1365
1677
|
}, [activeAnchor, anchorSelect]);
|
|
1366
1678
|
React.useEffect(() => {
|
|
1367
1679
|
/* c8 ignore start */
|
|
@@ -1442,19 +1754,12 @@ const TooltipController = React.forwardRef(({ id, anchorSelect, content, render,
|
|
|
1442
1754
|
disableTooltip,
|
|
1443
1755
|
activeAnchor,
|
|
1444
1756
|
previousActiveAnchor: previousActiveAnchorRef.current,
|
|
1445
|
-
setActiveAnchor:
|
|
1446
|
-
setActiveAnchor((prev) => {
|
|
1447
|
-
if (!(anchor === null || anchor === void 0 ? void 0 : anchor.isSameNode(prev))) {
|
|
1448
|
-
previousActiveAnchorRef.current = prev;
|
|
1449
|
-
}
|
|
1450
|
-
return anchor;
|
|
1451
|
-
});
|
|
1452
|
-
},
|
|
1757
|
+
setActiveAnchor: handleSetActiveAnchor,
|
|
1453
1758
|
role,
|
|
1454
1759
|
};
|
|
1455
1760
|
return React.createElement(Tooltip$1, { ...props });
|
|
1456
1761
|
});
|
|
1457
|
-
var
|
|
1762
|
+
var TooltipController_default = React.memo(TooltipController);
|
|
1458
1763
|
|
|
1459
1764
|
// those content will be replaced in build time with the `react-tooltip.css` builded content
|
|
1460
1765
|
const TooltipCoreStyles = `:root {
|
|
@@ -1476,7 +1781,6 @@ const TooltipCoreStyles = `:root {
|
|
|
1476
1781
|
left: 0;
|
|
1477
1782
|
pointer-events: none;
|
|
1478
1783
|
opacity: 0;
|
|
1479
|
-
will-change: opacity;
|
|
1480
1784
|
}
|
|
1481
1785
|
|
|
1482
1786
|
.core-styles-module_fixed__pcSol {
|
|
@@ -1507,11 +1811,13 @@ const TooltipCoreStyles = `:root {
|
|
|
1507
1811
|
.core-styles-module_show__Nt9eE {
|
|
1508
1812
|
opacity: var(--rt-opacity);
|
|
1509
1813
|
transition: opacity var(--rt-transition-show-delay) ease-out;
|
|
1814
|
+
will-change: opacity;
|
|
1510
1815
|
}
|
|
1511
1816
|
|
|
1512
1817
|
.core-styles-module_closing__sGnxF {
|
|
1513
1818
|
opacity: 0;
|
|
1514
1819
|
transition: opacity var(--rt-transition-closing-delay) ease-in;
|
|
1820
|
+
will-change: opacity;
|
|
1515
1821
|
}
|
|
1516
1822
|
|
|
1517
1823
|
`;
|
|
@@ -1592,5 +1898,5 @@ if (typeof window !== 'undefined') {
|
|
|
1592
1898
|
}));
|
|
1593
1899
|
}
|
|
1594
1900
|
|
|
1595
|
-
exports.Tooltip =
|
|
1901
|
+
exports.Tooltip = TooltipController_default;
|
|
1596
1902
|
//# sourceMappingURL=react-tooltip.cjs.map
|