Skip to content

Commit a008075

Browse files
Tanmay Shettygabrieljablonski
authored andcommitted
feat: add update tooltip on resize
1 parent 67889d0 commit a008075

File tree

2 files changed

+51
-47
lines changed

2 files changed

+51
-47
lines changed

src/components/Tooltip/Tooltip.tsx

Lines changed: 50 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,52 @@ const Tooltip = ({
290290
// mouse enter and focus events being triggered toggether
291291
const debouncedHandleShowTooltip = debounce(handleShowTooltip, 50, true)
292292
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)
293339

294340
useEffect(() => {
295341
const elementRefs = new Set(anchorRefs)
@@ -317,6 +363,8 @@ const Tooltip = ({
317363
}
318364
if (closeOnResize) {
319365
window.addEventListener('resize', handleScrollResize)
366+
} else {
367+
window.addEventListener('resize', debouncedUpdateTooltipPosition)
320368
}
321369

322370
const handleEsc = (event: KeyboardEvent) => {
@@ -377,6 +425,8 @@ const Tooltip = ({
377425
}
378426
if (closeOnResize) {
379427
window.removeEventListener('resize', handleScrollResize)
428+
} else {
429+
window.removeEventListener('resize', debouncedUpdateTooltipPosition)
380430
}
381431
if (shouldOpenOnClick) {
382432
window.removeEventListener('click', handleClickOutsideAnchors)
@@ -476,52 +526,6 @@ const Tooltip = ({
476526
}
477527
}, [id, anchorSelect, activeAnchor])
478528

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-
525529
useEffect(() => {
526530
updateTooltipPosition()
527531
}, [show, activeAnchor, content, externalStyles, place, offset, positionStrategy, position])

src/utils/debounce.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* @param { number } wait Time to wait before execut the function
66
* @param { boolean } immediate Param to define if the function will be executed immediately
77
*/
8-
const debounce = (func: (...args: any[]) => void, wait?: number, immediate?: true) => {
8+
const debounce = (func: (...args: any[]) => void, wait?: number, immediate?: boolean) => {
99
let timeout: NodeJS.Timeout | null = null
1010

1111
return function debounced(this: typeof func, ...args: any[]) {

0 commit comments

Comments
 (0)