Skip to content

Commit 5a47e6a

Browse files
fix: watch added nodes
1 parent 69fd525 commit 5a47e6a

File tree

1 file changed

+45
-12
lines changed

1 file changed

+45
-12
lines changed

src/components/Tooltip/Tooltip.tsx

Lines changed: 45 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -352,21 +352,45 @@ const Tooltip = ({
352352
})
353353

354354
const parentObserverCallback: MutationCallback = (mutationList) => {
355-
if (!activeAnchor) {
356-
return
355+
let selector = anchorSelect ?? ''
356+
if (!selector && id) {
357+
selector = `[data-tooltip-id='${id}']`
357358
}
358-
mutationList.some((mutation) => {
359+
mutationList.forEach((mutation) => {
359360
if (mutation.type !== 'childList') {
360-
return false
361+
return
362+
}
363+
if (activeAnchor) {
364+
;[...mutation.removedNodes].some((node) => {
365+
if (node.contains(activeAnchor)) {
366+
setRendered(false)
367+
handleShow(false)
368+
setActiveAnchor(null)
369+
return true
370+
}
371+
return false
372+
})
373+
}
374+
if (!selector) {
375+
return
361376
}
362-
return [...mutation.removedNodes].some((node) => {
363-
if (node.contains(activeAnchor)) {
364-
handleShow(false)
365-
setActiveAnchor(null)
366-
return true
377+
try {
378+
const elements = [...mutation.addedNodes].filter((node) => node.nodeType === 1)
379+
const newAnchors = [
380+
...elements.filter((element) => (element as HTMLElement).matches(selector)),
381+
...elements.flatMap((element) => [
382+
...(element as HTMLElement).querySelectorAll(selector),
383+
]),
384+
] as HTMLElement[]
385+
if (newAnchors.length) {
386+
setAnchorsBySelect((anchors) => [...anchors, ...newAnchors])
367387
}
368-
return false
369-
})
388+
} catch {
389+
/**
390+
* invalid CSS selector.
391+
* already warned on tooltip controller
392+
*/
393+
}
370394
})
371395
}
372396

@@ -397,7 +421,16 @@ const Tooltip = ({
397421
* rendered is also a dependency to ensure anchor observers are re-registered
398422
* since `tooltipRef` becomes stale after removing/adding the tooltip to the DOM
399423
*/
400-
}, [rendered, anchorRefs, activeAnchor, closeOnEsc, events, delayHide, delayShow])
424+
}, [
425+
rendered,
426+
anchorRefs,
427+
activeAnchor,
428+
anchorsBySelect,
429+
closeOnEsc,
430+
events,
431+
delayHide,
432+
delayShow,
433+
])
401434

402435
useEffect(() => {
403436
if (position) {

0 commit comments

Comments
 (0)