Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 34 additions & 3 deletions src/components/Tooltip/Tooltip.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,10 @@ const Tooltip = ({
return
}

if (!activeAnchor?.isConnected) {
return
}

computeTooltipPosition({
place,
offset,
Expand Down Expand Up @@ -486,6 +490,7 @@ const Tooltip = ({
}
const documentObserverCallback: MutationCallback = (mutationList) => {
const newAnchors: HTMLElement[] = []
const removedAnchors: HTMLElement[] = []
mutationList.forEach((mutation) => {
if (mutation.type === 'attributes' && mutation.attributeName === 'data-tooltip-id') {
const newId = (mutation.target as HTMLElement).getAttribute('data-tooltip-id')
Expand All @@ -497,7 +502,30 @@ const Tooltip = ({
return
}
if (activeAnchor) {
;[...mutation.removedNodes].some((node) => {
const elements = [...mutation.removedNodes].filter((node) => node.nodeType === 1)
if (selector) {
try {
removedAnchors.push(
// the element itself is an anchor
...(elements.filter((element) =>
(element as HTMLElement).matches(selector),
) as HTMLElement[]),
)
removedAnchors.push(
// the element has children which are anchors
...elements.flatMap(
(element) =>
[...(element as HTMLElement).querySelectorAll(selector)] as HTMLElement[],
),
)
} catch {
/**
* invalid CSS selector.
* already warned on tooltip controller
*/
}
}
elements.some((node) => {
if (node?.contains?.(activeAnchor)) {
setRendered(false)
handleShow(false)
Expand Down Expand Up @@ -538,8 +566,11 @@ const Tooltip = ({
*/
}
})
if (newAnchors.length) {
setAnchorsBySelect((anchors) => [...anchors, ...newAnchors])
if (newAnchors.length || removedAnchors.length) {
setAnchorsBySelect((anchors) => [
...anchors.filter((anchor) => removedAnchors.includes(anchor)),
...newAnchors,
])
}
}
const documentObserver = new MutationObserver(documentObserverCallback)
Expand Down