[Web] Retrigger onEndReached if needed when content height changes (#4859)

* Extract EdgeVisibility

* Key Visibility by container height instead of item count
zio/stable
dan 2024-07-31 19:10:24 +01:00 committed by GitHub
parent c75bb65bef
commit 576cef88b5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 32 additions and 3 deletions

View File

@ -344,10 +344,11 @@ function ListImpl<ItemT>(
style={[styles.aboveTheFoldDetector, {height: headerOffset}]}
/>
{onStartReached && !isEmpty && (
<Visibility
<EdgeVisibility
root={disableFullWindowScroll ? nativeRef : null}
onVisibleChange={onHeadVisibilityChange}
topMargin={(onStartReachedThreshold ?? 0) * 100 + '%'}
containerRef={containerRef}
/>
)}
{headerComponent}
@ -368,11 +369,11 @@ function ListImpl<ItemT>(
)
})}
{onEndReached && !isEmpty && (
<Visibility
<EdgeVisibility
root={disableFullWindowScroll ? nativeRef : null}
onVisibleChange={onTailVisibilityChange}
bottomMargin={(onEndReachedThreshold ?? 0) * 100 + '%'}
key={data?.length}
containerRef={containerRef}
/>
)}
{footerComponent}
@ -381,6 +382,34 @@ function ListImpl<ItemT>(
)
}
function EdgeVisibility({
root,
topMargin,
bottomMargin,
containerRef,
onVisibleChange,
}: {
root?: React.RefObject<HTMLDivElement> | null
topMargin?: string
bottomMargin?: string
containerRef: React.RefObject<Element>
onVisibleChange: (isVisible: boolean) => void
}) {
const [containerHeight, setContainerHeight] = React.useState(0)
useResizeObserver(containerRef, (w, h) => {
setContainerHeight(h)
})
return (
<Visibility
key={containerHeight}
root={root}
topMargin={topMargin}
bottomMargin={bottomMargin}
onVisibleChange={onVisibleChange}
/>
)
}
function useResizeObserver(
ref: React.RefObject<Element>,
onResize: undefined | ((w: number, h: number) => void),