@@ -486,6 +486,7 @@ const Tooltip = ({
486
486
}
487
487
const documentObserverCallback : MutationCallback = ( mutationList ) => {
488
488
const newAnchors : HTMLElement [ ] = [ ]
489
+ const removedAnchors : HTMLElement [ ] = [ ]
489
490
mutationList . forEach ( ( mutation ) => {
490
491
if ( mutation . type === 'attributes' && mutation . attributeName === 'data-tooltip-id' ) {
491
492
const newId = ( mutation . target as HTMLElement ) . getAttribute ( 'data-tooltip-id' )
@@ -497,7 +498,23 @@ const Tooltip = ({
497
498
return
498
499
}
499
500
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 ) => {
501
518
if ( node ?. contains ?.( activeAnchor ) ) {
502
519
setRendered ( false )
503
520
handleShow ( false )
@@ -539,7 +556,10 @@ const Tooltip = ({
539
556
}
540
557
} )
541
558
if ( newAnchors . length ) {
542
- setAnchorsBySelect ( ( anchors ) => [ ...anchors , ...newAnchors ] )
559
+ setAnchorsBySelect ( ( anchors ) => [
560
+ ...anchors . filter ( ( anchor ) => removedAnchors . includes ( anchor ) ) ,
561
+ ...newAnchors ,
562
+ ] )
543
563
}
544
564
}
545
565
const documentObserver = new MutationObserver ( documentObserverCallback )
0 commit comments