Skip to content

Commit 7068643

Browse files
Copilotdanielbarion
andcommitted
Fix memory leak in ResizeObserver for tooltip children cleanup
Co-authored-by: danielbarion <[email protected]>
1 parent 8e34a51 commit 7068643

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

src/components/Tooltip/Tooltip.tsx

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -759,17 +759,29 @@ const Tooltip = ({
759759
}, [updateTooltipPosition])
760760

761761
useEffect(() => {
762-
if (!contentWrapperRef?.current) {
762+
if (!rendered || !contentWrapperRef?.current) {
763763
return () => null
764764
}
765+
const timeoutIds: Set<NodeJS.Timeout> = new Set()
765766
const contentObserver = new ResizeObserver(() => {
766-
setTimeout(() => updateTooltipPosition())
767+
const timeoutId = setTimeout(() => {
768+
timeoutIds.delete(timeoutId)
769+
if (!mounted.current) {
770+
return
771+
}
772+
updateTooltipPosition()
773+
})
774+
timeoutIds.add(timeoutId)
767775
})
768776
contentObserver.observe(contentWrapperRef.current)
769777
return () => {
770778
contentObserver.disconnect()
779+
timeoutIds.forEach((timeoutId) => {
780+
clearTimeout(timeoutId)
781+
})
782+
timeoutIds.clear()
771783
}
772-
}, [content, contentWrapperRef?.current])
784+
}, [rendered, contentWrapperRef?.current])
773785

774786
useEffect(() => {
775787
const anchorById = document.querySelector<HTMLElement>(`[id='${anchorId}']`)

0 commit comments

Comments
 (0)