@@ -290,6 +290,52 @@ const Tooltip = ({
290
290
// mouse enter and focus events being triggered toggether
291
291
const debouncedHandleShowTooltip = debounce ( handleShowTooltip , 50 , true )
292
292
const debouncedHandleHideTooltip = debounce ( handleHideTooltip , 50 , true )
293
+ const updateTooltipPosition = ( ) => {
294
+ if ( position ) {
295
+ // if `position` is set, override regular and `float` positioning
296
+ handleTooltipPosition ( position )
297
+ return
298
+ }
299
+
300
+ if ( float ) {
301
+ if ( lastFloatPosition . current ) {
302
+ /*
303
+ Without this, changes to `content`, `place`, `offset`, ..., will only
304
+ trigger a position calculation after a `mousemove` event.
305
+
306
+ To see why this matters, comment this line, run `yarn dev` and click the
307
+ "Hover me!" anchor.
308
+ */
309
+ handleTooltipPosition ( lastFloatPosition . current )
310
+ }
311
+ // if `float` is set, override regular positioning
312
+ return
313
+ }
314
+
315
+ computeTooltipPosition ( {
316
+ place,
317
+ offset,
318
+ elementReference : activeAnchor ,
319
+ tooltipReference : tooltipRef . current ,
320
+ tooltipArrowReference : tooltipArrowRef . current ,
321
+ strategy : positionStrategy ,
322
+ middlewares,
323
+ border,
324
+ } ) . then ( ( computedStylesData ) => {
325
+ if ( ! mounted . current ) {
326
+ // invalidate computed positions after remount
327
+ return
328
+ }
329
+ if ( Object . keys ( computedStylesData . tooltipStyles ) . length ) {
330
+ setInlineStyles ( computedStylesData . tooltipStyles )
331
+ }
332
+ if ( Object . keys ( computedStylesData . tooltipArrowStyles ) . length ) {
333
+ setInlineArrowStyles ( computedStylesData . tooltipArrowStyles )
334
+ }
335
+ setActualPlacement ( computedStylesData . place as PlacesType )
336
+ } )
337
+ }
338
+ const debouncedUpdateTooltipPosition = debounce ( updateTooltipPosition , 100 , true )
293
339
294
340
useEffect ( ( ) => {
295
341
const elementRefs = new Set ( anchorRefs )
@@ -317,6 +363,8 @@ const Tooltip = ({
317
363
}
318
364
if ( closeOnResize ) {
319
365
window . addEventListener ( 'resize' , handleScrollResize )
366
+ } else {
367
+ window . addEventListener ( 'resize' , debouncedUpdateTooltipPosition )
320
368
}
321
369
322
370
const handleEsc = ( event : KeyboardEvent ) => {
@@ -377,6 +425,8 @@ const Tooltip = ({
377
425
}
378
426
if ( closeOnResize ) {
379
427
window . removeEventListener ( 'resize' , handleScrollResize )
428
+ } else {
429
+ window . removeEventListener ( 'resize' , debouncedUpdateTooltipPosition )
380
430
}
381
431
if ( shouldOpenOnClick ) {
382
432
window . removeEventListener ( 'click' , handleClickOutsideAnchors )
@@ -476,52 +526,6 @@ const Tooltip = ({
476
526
}
477
527
} , [ id , anchorSelect , activeAnchor ] )
478
528
479
- const updateTooltipPosition = ( ) => {
480
- if ( position ) {
481
- // if `position` is set, override regular and `float` positioning
482
- handleTooltipPosition ( position )
483
- return
484
- }
485
-
486
- if ( float ) {
487
- if ( lastFloatPosition . current ) {
488
- /*
489
- Without this, changes to `content`, `place`, `offset`, ..., will only
490
- trigger a position calculation after a `mousemove` event.
491
-
492
- To see why this matters, comment this line, run `yarn dev` and click the
493
- "Hover me!" anchor.
494
- */
495
- handleTooltipPosition ( lastFloatPosition . current )
496
- }
497
- // if `float` is set, override regular positioning
498
- return
499
- }
500
-
501
- computeTooltipPosition ( {
502
- place,
503
- offset,
504
- elementReference : activeAnchor ,
505
- tooltipReference : tooltipRef . current ,
506
- tooltipArrowReference : tooltipArrowRef . current ,
507
- strategy : positionStrategy ,
508
- middlewares,
509
- border,
510
- } ) . then ( ( computedStylesData ) => {
511
- if ( ! mounted . current ) {
512
- // invalidate computed positions after remount
513
- return
514
- }
515
- if ( Object . keys ( computedStylesData . tooltipStyles ) . length ) {
516
- setInlineStyles ( computedStylesData . tooltipStyles )
517
- }
518
- if ( Object . keys ( computedStylesData . tooltipArrowStyles ) . length ) {
519
- setInlineArrowStyles ( computedStylesData . tooltipArrowStyles )
520
- }
521
- setActualPlacement ( computedStylesData . place as PlacesType )
522
- } )
523
- }
524
-
525
529
useEffect ( ( ) => {
526
530
updateTooltipPosition ( )
527
531
} , [ show , activeAnchor , content , externalStyles , place , offset , positionStrategy , position ] )
0 commit comments