@@ -84,17 +84,14 @@ export function VirtualElement({
8484 }
8585 } , [ ] )
8686
87+ const placeholderHeightPx = measurements ?. clientHeight ?? placeholderHeight ?? ref ?. clientHeight ?? 0
88+
8789 const styleObj = useMemo < React . CSSProperties > (
8890 ( ) => ( {
8991 width : width ?? 'auto' ,
90- height : ( ( placeholderHeight || ref ?. clientHeight ) ?? '0' ) + 'px' ,
91- // These properties are used to ensure that if a prior element is changed from
92- // placeHolder to element, the position of visible elements are not affected.
93- contentVisibility : 'auto' ,
94- containIntrinsicSize : `0 ${ ( placeholderHeight || ref ?. clientHeight ) ?? '0' } px` ,
95- contain : 'size layout' ,
92+ height : `${ placeholderHeightPx } px` ,
9693 } ) ,
97- [ width , placeholderHeight ]
94+ [ width , placeholderHeightPx ]
9895 )
9996
10097 const handleResize = useCallback ( ( ) => {
@@ -420,6 +417,13 @@ export class ElementObserverManager {
420417 private observedElements : Map < HTMLElement , ( ) => void >
421418 private isMutationObserverActive = false
422419
420+ private hasConnectedObservedElements ( ) : boolean {
421+ for ( const observedElement of this . observedElements . keys ( ) ) {
422+ if ( document . contains ( observedElement ) ) return true
423+ }
424+ return false
425+ }
426+
423427 private pruneDetachedObservedElements ( ) : void {
424428 for ( const observedElement of Array . from ( this . observedElements . keys ( ) ) ) {
425429 if ( ! document . contains ( observedElement ) ) {
@@ -448,6 +452,10 @@ export class ElementObserverManager {
448452 if ( this . observedElements . size === 0 ) return
449453
450454 this . pruneDetachedObservedElements ( )
455+ if ( this . observedElements . size === 0 ) {
456+ this . disconnectMutationObserver ( )
457+ return
458+ }
451459 const targets = new Set < HTMLElement > ( )
452460
453461 mutations . forEach ( ( mutation ) => {
@@ -479,13 +487,12 @@ export class ElementObserverManager {
479487 private ensureMutationObserverConnected ( ) : void {
480488 if ( this . isMutationObserverActive ) return
481489 if ( this . observedElements . size === 0 ) return
490+ if ( ! this . hasConnectedObservedElements ( ) ) return
482491 if ( ! document . body ) return
483492
484493 this . mutationObserver . observe ( document . body , {
485494 childList : true ,
486495 subtree : true ,
487- attributes : true ,
488- characterData : true ,
489496 } )
490497 this . isMutationObserverActive = true
491498 }
@@ -522,6 +529,14 @@ export class ElementObserverManager {
522529 if ( this . observedElements . size === 0 ) {
523530 this . resizeObserver . disconnect ( )
524531 this . disconnectMutationObserver ( )
532+ return
525533 }
534+
535+ if ( ! this . hasConnectedObservedElements ( ) ) {
536+ this . disconnectMutationObserver ( )
537+ return
538+ }
539+
540+ this . ensureMutationObserverConnected ( )
526541 }
527542}
0 commit comments