@@ -486,6 +486,7 @@ const Tooltip = ({
486486 }
487487 const documentObserverCallback : MutationCallback = ( mutationList ) => {
488488 const newAnchors : HTMLElement [ ] = [ ]
489+ const removedAnchors : HTMLElement [ ] = [ ]
489490 mutationList . forEach ( ( mutation ) => {
490491 if ( mutation . type === 'attributes' && mutation . attributeName === 'data-tooltip-id' ) {
491492 const newId = ( mutation . target as HTMLElement ) . getAttribute ( 'data-tooltip-id' )
@@ -497,7 +498,23 @@ const Tooltip = ({
497498 return
498499 }
499500 if ( activeAnchor ) {
500- ; [ ...mutation . removedNodes ] . some ( ( node ) => {
501+ const elements = [ ...mutation . removedNodes ] . filter ( ( node ) => node . nodeType === 1 )
502+ if ( selector ) {
503+ removedAnchors . push (
504+ // the element itself is an anchor
505+ ...( elements . filter ( ( element ) =>
506+ ( element as HTMLElement ) . matches ( selector ) ,
507+ ) as HTMLElement [ ] ) ,
508+ )
509+ removedAnchors . push (
510+ // the element has children which are anchors
511+ ...elements . flatMap (
512+ ( element ) =>
513+ [ ...( element as HTMLElement ) . querySelectorAll ( selector ) ] as HTMLElement [ ] ,
514+ ) ,
515+ )
516+ }
517+ elements . some ( ( node ) => {
501518 if ( node ?. contains ?.( activeAnchor ) ) {
502519 setRendered ( false )
503520 handleShow ( false )
@@ -539,7 +556,10 @@ const Tooltip = ({
539556 }
540557 } )
541558 if ( newAnchors . length ) {
542- setAnchorsBySelect ( ( anchors ) => [ ...anchors , ...newAnchors ] )
559+ setAnchorsBySelect ( ( anchors ) => [
560+ ...anchors . filter ( ( anchor ) => removedAnchors . includes ( anchor ) ) ,
561+ ...newAnchors ,
562+ ] )
543563 }
544564 }
545565 const documentObserver = new MutationObserver ( documentObserverCallback )
0 commit comments