Skip to content

Watch for added nodes and include in anchors list #952

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Feb 22, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
57 changes: 45 additions & 12 deletions src/components/Tooltip/Tooltip.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -352,21 +352,45 @@ const Tooltip = ({
})

const parentObserverCallback: MutationCallback = (mutationList) => {
if (!activeAnchor) {
return
let selector = anchorSelect ?? ''
if (!selector && id) {
selector = `[data-tooltip-id='${id}']`
}
mutationList.some((mutation) => {
mutationList.forEach((mutation) => {
if (mutation.type !== 'childList') {
return false
return
}
if (activeAnchor) {
;[...mutation.removedNodes].some((node) => {
if (node.contains(activeAnchor)) {
setRendered(false)
handleShow(false)
setActiveAnchor(null)
return true
}
return false
})
}
if (!selector) {
return
}
return [...mutation.removedNodes].some((node) => {
if (node.contains(activeAnchor)) {
handleShow(false)
setActiveAnchor(null)
return true
try {
const elements = [...mutation.addedNodes].filter((node) => node.nodeType === 1)
const newAnchors = [
...elements.filter((element) => (element as HTMLElement).matches(selector)),
...elements.flatMap((element) => [
...(element as HTMLElement).querySelectorAll(selector),
]),
] as HTMLElement[]
if (newAnchors.length) {
setAnchorsBySelect((anchors) => [...anchors, ...newAnchors])
}
return false
})
} catch {
/**
* invalid CSS selector.
* already warned on tooltip controller
*/
}
})
}

Expand Down Expand Up @@ -397,7 +421,16 @@ const Tooltip = ({
* rendered is also a dependency to ensure anchor observers are re-registered
* since `tooltipRef` becomes stale after removing/adding the tooltip to the DOM
*/
}, [rendered, anchorRefs, activeAnchor, closeOnEsc, events, delayHide, delayShow])
}, [
rendered,
anchorRefs,
activeAnchor,
anchorsBySelect,
closeOnEsc,
events,
delayHide,
delayShow,
])

useEffect(() => {
if (position) {
Expand Down
2 changes: 0 additions & 2 deletions src/test/tooltip-props.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,6 @@ describe('tooltip props', () => {
},
)

await userEvent.unhover(anchorElement)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How was this test passing before? 🤔


await waitFor(() => {
tooltip = screen.getByRole('tooltip')
})
Expand Down