Skip to content

Commit 5e206f1

Browse files
fix: hide tooltip when anchor is removed from DOM
1 parent 7dec4fc commit 5e206f1

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

src/components/Tooltip/Tooltip.tsx

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,29 @@ const Tooltip = ({
263263
})
264264
})
265265

266+
const anchorElement = anchorById ?? activeAnchor.current
267+
268+
const parentObserverCallback: MutationCallback = (mutationList) => {
269+
if (!anchorElement) {
270+
return
271+
}
272+
mutationList.forEach((mutation) => {
273+
if (mutation.type !== 'childList') {
274+
return
275+
}
276+
mutation.removedNodes.forEach((node) => {
277+
if (node.contains(anchorElement)) {
278+
handleShow(false)
279+
}
280+
})
281+
})
282+
}
283+
284+
const parentObserver = new MutationObserver(parentObserverCallback)
285+
286+
// watch for anchor being removed from the DOM
287+
parentObserver.observe(document.body, { attributes: false, childList: true, subtree: true })
288+
266289
return () => {
267290
if (events.find((event: string) => event === 'click')) {
268291
window.removeEventListener('click', handleClickOutsideAnchor)
@@ -279,6 +302,7 @@ const Tooltip = ({
279302
ref.current?.removeEventListener(event, listener)
280303
})
281304
})
305+
parentObserver.disconnect()
282306
}
283307
}, [anchorRefs, activeAnchor, closeOnEsc, anchorId, events, delayHide, delayShow])
284308

0 commit comments

Comments
 (0)