@@ -75,8 +75,6 @@ export function VirtualElement({
75
75
const skipInitialrunRef = useRef < boolean > ( true )
76
76
const isTransitioning = useRef ( false )
77
77
78
- const showPlaceholder = ! isShowingChildren && ! initialShow
79
-
80
78
const isCurrentlyObserving = useRef ( false )
81
79
82
80
const styleObj = useMemo < React . CSSProperties > (
@@ -119,17 +117,27 @@ export function VirtualElement({
119
117
120
118
// failsafe to ensure visible elements if resizing happens while scrolling
121
119
useEffect ( ( ) => {
122
- if ( inView && ! isShowingChildren ) {
123
- // If element is in view but showing placeholder, force show children
124
- const forceShowTimeout = setTimeout ( ( ) => {
125
- setIsShowingChildren ( true )
126
- } , 400 )
120
+ if ( ! isShowingChildren ) {
121
+ const checkVisibilityByPosition = ( ) => {
122
+ if ( ref ) {
123
+ const rect = ref . getBoundingClientRect ( )
124
+ const isInViewport = rect . top < window . innerHeight && rect . bottom > 0
125
+
126
+ if ( isInViewport ) {
127
+ setIsShowingChildren ( true )
128
+ setInView ( true )
129
+ }
130
+ }
131
+ }
132
+
133
+ // Check every second
134
+ const positionCheckInterval = setInterval ( checkVisibilityByPosition , 1000 )
127
135
128
136
return ( ) => {
129
- clearTimeout ( forceShowTimeout )
137
+ clearInterval ( positionCheckInterval )
130
138
}
131
139
}
132
- } , [ inView , isShowingChildren ] )
140
+ } , [ ref , isShowingChildren ] )
133
141
134
142
// Ensure elements are visible after a fast scroll:
135
143
useEffect ( ( ) => {
@@ -147,7 +155,7 @@ export function VirtualElement({
147
155
if ( inView && ! isShowingChildren ) {
148
156
setIsShowingChildren ( true )
149
157
}
150
- } , 400 )
158
+ } , 200 )
151
159
}
152
160
153
161
window . addEventListener ( 'scroll' , checkVisibilityOnScroll , { passive : true } )
@@ -160,19 +168,6 @@ export function VirtualElement({
160
168
}
161
169
} , [ inView , isShowingChildren ] )
162
170
163
- // Periodic check to ensure elements in view are shown
164
- useEffect ( ( ) => {
165
- const periodicCheckInterval = setInterval ( ( ) => {
166
- if ( inView && ! isShowingChildren ) {
167
- setIsShowingChildren ( true )
168
- }
169
- } , 1000 )
170
-
171
- return ( ) => {
172
- clearInterval ( periodicCheckInterval )
173
- }
174
- } , [ inView , isShowingChildren ] )
175
-
176
171
useEffect ( ( ) => {
177
172
if ( inView ) {
178
173
setIsShowingChildren ( true )
@@ -352,7 +347,7 @@ export function VirtualElement({
352
347
: undefined
353
348
}
354
349
>
355
- { showPlaceholder ? (
350
+ { ! isShowingChildren ? (
356
351
< div
357
352
id = { measurements ?. id ?? id }
358
353
className = { `virtual-element-placeholder ${ placeholderClassName } ` }
0 commit comments