Skip to content

Commit 9eb142b

Browse files
committed
fix: add position interval check to ensure that all elements in view are visible
1 parent 4b73344 commit 9eb142b

File tree

1 file changed

+19
-24
lines changed

1 file changed

+19
-24
lines changed

packages/webui/src/client/lib/VirtualElement.tsx

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,6 @@ export function VirtualElement({
7575
const skipInitialrunRef = useRef<boolean>(true)
7676
const isTransitioning = useRef(false)
7777

78-
const showPlaceholder = !isShowingChildren && !initialShow
79-
8078
const isCurrentlyObserving = useRef(false)
8179

8280
const styleObj = useMemo<React.CSSProperties>(
@@ -119,17 +117,27 @@ export function VirtualElement({
119117

120118
// failsafe to ensure visible elements if resizing happens while scrolling
121119
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)
127135

128136
return () => {
129-
clearTimeout(forceShowTimeout)
137+
clearInterval(positionCheckInterval)
130138
}
131139
}
132-
}, [inView, isShowingChildren])
140+
}, [ref, isShowingChildren])
133141

134142
// Ensure elements are visible after a fast scroll:
135143
useEffect(() => {
@@ -147,7 +155,7 @@ export function VirtualElement({
147155
if (inView && !isShowingChildren) {
148156
setIsShowingChildren(true)
149157
}
150-
}, 400)
158+
}, 200)
151159
}
152160

153161
window.addEventListener('scroll', checkVisibilityOnScroll, { passive: true })
@@ -160,19 +168,6 @@ export function VirtualElement({
160168
}
161169
}, [inView, isShowingChildren])
162170

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-
176171
useEffect(() => {
177172
if (inView) {
178173
setIsShowingChildren(true)
@@ -352,7 +347,7 @@ export function VirtualElement({
352347
: undefined
353348
}
354349
>
355-
{showPlaceholder ? (
350+
{!isShowingChildren ? (
356351
<div
357352
id={measurements?.id ?? id}
358353
className={`virtual-element-placeholder ${placeholderClassName}`}

0 commit comments

Comments
 (0)