A few `containWeb` List nits (#3877)

* use getters for returned values

* pass ref

* add log to `onScroll` in tester

* improve expect error
zio/stable
Hailey 2024-05-06 13:45:14 -07:00 committed by GitHub
parent 0b6ace990e
commit 99f3f10fe7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 60 additions and 24 deletions

View File

@ -92,12 +92,24 @@ function ListImpl<ItemT>(
if (!element) return if (!element) return
return { return {
scrollWidth: element.scrollWidth, get scrollWidth() {
scrollHeight: element.scrollHeight, return element.scrollWidth
clientWidth: element.clientWidth, },
clientHeight: element.clientHeight, get scrollHeight() {
scrollY: element.scrollTop, return element.scrollHeight
scrollX: element.scrollLeft, },
get clientWidth() {
return element.clientWidth
},
get clientHeight() {
return element.clientHeight
},
get scrollY() {
return element.scrollTop
},
get scrollX() {
return element.scrollLeft
},
scrollTo(options?: ScrollToOptions) { scrollTo(options?: ScrollToOptions) {
element.scrollTo(options) element.scrollTo(options)
}, },
@ -113,12 +125,24 @@ function ListImpl<ItemT>(
} }
} else { } else {
return { return {
scrollWidth: document.documentElement.scrollWidth, get scrollWidth() {
scrollHeight: document.documentElement.scrollHeight, return document.documentElement.scrollWidth
clientWidth: window.innerWidth, },
clientHeight: window.innerHeight, get scrollHeight() {
scrollY: window.scrollY, return document.documentElement.scrollHeight
scrollX: window.scrollX, },
get clientWidth() {
return window.innerWidth
},
get clientHeight() {
return window.innerHeight
},
get scrollY() {
return window.scrollY
},
get scrollX() {
return window.scrollX
},
scrollTo(options: ScrollToOptions) { scrollTo(options: ScrollToOptions) {
window.scrollTo(options) window.scrollTo(options)
}, },
@ -135,7 +159,7 @@ function ListImpl<ItemT>(
} }
}, [containWeb]) }, [containWeb])
const nativeRef = React.useRef(null) const nativeRef = React.useRef<HTMLDivElement>(null)
React.useImperativeHandle( React.useImperativeHandle(
ref, ref,
() => () =>
@ -257,9 +281,15 @@ function ListImpl<ItemT>(
return ( return (
<View <View
{...props} {...props}
// @ts-ignore web only style={[
style={[style, containWeb && {flex: 1, 'overflow-y': 'scroll'}]} style,
ref={nativeRef}> containWeb && {
flex: 1,
// @ts-expect-error web only
'overflow-y': 'scroll',
},
]}
ref={nativeRef as any}>
<Visibility <Visibility
onVisibleChange={setIsInsideVisibleTree} onVisibleChange={setIsInsideVisibleTree}
style={ style={
@ -277,13 +307,13 @@ function ListImpl<ItemT>(
pal.border, pal.border,
]}> ]}>
<Visibility <Visibility
root={containWeb ? nativeRef.current : null} root={containWeb ? nativeRef : null}
onVisibleChange={handleAboveTheFoldVisibleChange} onVisibleChange={handleAboveTheFoldVisibleChange}
style={[styles.aboveTheFoldDetector, {height: headerOffset}]} style={[styles.aboveTheFoldDetector, {height: headerOffset}]}
/> />
{onStartReached && ( {onStartReached && (
<Visibility <Visibility
root={containWeb ? nativeRef.current : null} root={containWeb ? nativeRef : null}
onVisibleChange={onHeadVisibilityChange} onVisibleChange={onHeadVisibilityChange}
topMargin={(onStartReachedThreshold ?? 0) * 100 + '%'} topMargin={(onStartReachedThreshold ?? 0) * 100 + '%'}
/> />
@ -300,7 +330,7 @@ function ListImpl<ItemT>(
))} ))}
{onEndReached && ( {onEndReached && (
<Visibility <Visibility
root={containWeb ? nativeRef.current : null} root={containWeb ? nativeRef : null}
onVisibleChange={onTailVisibilityChange} onVisibleChange={onTailVisibilityChange}
bottomMargin={(onEndReachedThreshold ?? 0) * 100 + '%'} bottomMargin={(onEndReachedThreshold ?? 0) * 100 + '%'}
/> />
@ -363,13 +393,13 @@ let Row = function RowImpl<ItemT>({
Row = React.memo(Row) Row = React.memo(Row)
let Visibility = ({ let Visibility = ({
root = null, root,
topMargin = '0px', topMargin = '0px',
bottomMargin = '0px', bottomMargin = '0px',
onVisibleChange, onVisibleChange,
style, style,
}: { }: {
root?: Element | null root?: React.RefObject<HTMLDivElement> | null
topMargin?: string topMargin?: string
bottomMargin?: string bottomMargin?: string
onVisibleChange: (isVisible: boolean) => void onVisibleChange: (isVisible: boolean) => void
@ -393,7 +423,7 @@ let Visibility = ({
React.useEffect(() => { React.useEffect(() => {
const observer = new IntersectionObserver(handleIntersection, { const observer = new IntersectionObserver(handleIntersection, {
root, root: root?.current ?? null,
rootMargin: `${topMargin} 0px ${bottomMargin} 0px`, rootMargin: `${topMargin} 0px ${bottomMargin} 0px`,
}) })
const tail: Element | null = tailRef.current! const tail: Element | null = tailRef.current!

View File

@ -22,9 +22,15 @@ export function ListContained() {
<> <>
<View style={{width: '100%', height: 300}}> <View style={{width: '100%', height: 300}}>
<ScrollProvider <ScrollProvider
onScroll={() => { onScroll={e => {
'worklet' 'worklet'
console.log('onScroll') console.log(
JSON.stringify({
contentOffset: e.contentOffset,
layoutMeasurement: e.layoutMeasurement,
contentSize: e.contentSize,
}),
)
}}> }}>
<List <List
data={data} data={data}