Pull animated scroll handler down from pager (#1827)

zio/stable
dan 2023-11-07 16:46:39 +00:00 committed by GitHub
parent fa821943da
commit 7b2a7db83c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 114 additions and 87 deletions

View File

@ -1,9 +1,13 @@
import * as React from 'react' import * as React from 'react'
import {LayoutChangeEvent, StyleSheet, View} from 'react-native' import {
LayoutChangeEvent,
NativeScrollEvent,
StyleSheet,
View,
} from 'react-native'
import Animated, { import Animated, {
Easing, Easing,
useAnimatedReaction, useAnimatedReaction,
useAnimatedScrollHandler,
useAnimatedStyle, useAnimatedStyle,
useSharedValue, useSharedValue,
withTiming, withTiming,
@ -12,13 +16,12 @@ import Animated, {
import {Pager, PagerRef, RenderTabBarFnProps} from 'view/com/pager/Pager' import {Pager, PagerRef, RenderTabBarFnProps} from 'view/com/pager/Pager'
import {TabBar} from './TabBar' import {TabBar} from './TabBar'
import {useWebMediaQueries} from 'lib/hooks/useWebMediaQueries' import {useWebMediaQueries} from 'lib/hooks/useWebMediaQueries'
import {OnScrollCb} from 'lib/hooks/useOnMainScroll'
const SCROLLED_DOWN_LIMIT = 200 const SCROLLED_DOWN_LIMIT = 200
interface PagerWithHeaderChildParams { interface PagerWithHeaderChildParams {
headerHeight: number headerHeight: number
onScroll: OnScrollCb onScroll: (e: NativeScrollEvent) => void
isScrolledDown: boolean isScrolledDown: boolean
} }
@ -140,12 +143,18 @@ export const PagerWithHeader = React.forwardRef<PagerRef, PagerWithHeaderProps>(
], ],
) )
// props to pass into children render functions // Ideally we'd call useAnimatedScrollHandler here but we can't safely do that
const onScroll = useAnimatedScrollHandler({ // due to https://github.com/software-mansion/react-native-reanimated/issues/5345.
onScroll(e) { // So instead we pass down a worklet, and individual pages will have to call it.
const onScroll = React.useCallback(
(e: NativeScrollEvent) => {
'worklet'
scrollY.value = e.contentOffset.y scrollY.value = e.contentOffset.y
}, },
}) [scrollY],
)
// props to pass into children render functions
const childProps = React.useMemo<PagerWithHeaderChildParams>(() => { const childProps = React.useMemo<PagerWithHeaderChildParams>(() => {
return { return {
headerHeight, headerHeight,

View File

@ -1,7 +1,14 @@
import React, {useMemo, useCallback} from 'react' import React, {useMemo, useCallback} from 'react'
import {FlatList, StyleSheet, View, ActivityIndicator} from 'react-native' import {
FlatList,
NativeScrollEvent,
StyleSheet,
View,
ActivityIndicator,
} from 'react-native'
import {NativeStackScreenProps} from '@react-navigation/native-stack' import {NativeStackScreenProps} from '@react-navigation/native-stack'
import {useNavigation} from '@react-navigation/native' import {useNavigation} from '@react-navigation/native'
import {useAnimatedScrollHandler} from 'react-native-reanimated'
import {usePalette} from 'lib/hooks/usePalette' import {usePalette} from 'lib/hooks/usePalette'
import {HeartIcon, HeartIconSolid} from 'lib/icons' import {HeartIcon, HeartIconSolid} from 'lib/icons'
import {FontAwesomeIcon} from '@fortawesome/react-native-fontawesome' import {FontAwesomeIcon} from '@fortawesome/react-native-fontawesome'
@ -26,7 +33,6 @@ import {EmptyState} from 'view/com/util/EmptyState'
import * as Toast from 'view/com/util/Toast' import * as Toast from 'view/com/util/Toast'
import {useSetTitle} from 'lib/hooks/useSetTitle' import {useSetTitle} from 'lib/hooks/useSetTitle'
import {useCustomFeed} from 'lib/hooks/useCustomFeed' import {useCustomFeed} from 'lib/hooks/useCustomFeed'
import {OnScrollCb} from 'lib/hooks/useOnMainScroll'
import {shareUrl} from 'lib/sharing' import {shareUrl} from 'lib/sharing'
import {toShareUrl} from 'lib/strings/url-helpers' import {toShareUrl} from 'lib/strings/url-helpers'
import {Haptics} from 'lib/haptics' import {Haptics} from 'lib/haptics'
@ -345,17 +351,14 @@ export const ProfileFeedScreenInner = observer(
/> />
)} )}
{({onScroll, headerHeight}) => ( {({onScroll, headerHeight}) => (
<ScrollView
onScroll={onScroll}
scrollEventThrottle={1}
contentContainerStyle={{paddingTop: headerHeight}}>
<AboutSection <AboutSection
feedOwnerDid={feedOwnerDid} feedOwnerDid={feedOwnerDid}
feedRkey={rkey} feedRkey={rkey}
feedInfo={feedInfo} feedInfo={feedInfo}
headerHeight={headerHeight}
onToggleLiked={onToggleLiked} onToggleLiked={onToggleLiked}
onScroll={onScroll}
/> />
</ScrollView>
)} )}
</PagerWithHeader> </PagerWithHeader>
<FAB <FAB
@ -379,7 +382,7 @@ export const ProfileFeedScreenInner = observer(
interface FeedSectionProps { interface FeedSectionProps {
feed: PostsFeedModel feed: PostsFeedModel
onScroll: OnScrollCb onScroll: (e: NativeScrollEvent) => void
headerHeight: number headerHeight: number
isScrolledDown: boolean isScrolledDown: boolean
} }
@ -404,12 +407,13 @@ const FeedSection = React.forwardRef<SectionRef, FeedSectionProps>(
return <EmptyState icon="feed" message="This feed is empty!" /> return <EmptyState icon="feed" message="This feed is empty!" />
}, []) }, [])
const scrollHandler = useAnimatedScrollHandler({onScroll})
return ( return (
<View> <View>
<Feed <Feed
feed={feed} feed={feed}
scrollElRef={scrollElRef} scrollElRef={scrollElRef}
onScroll={onScroll} onScroll={scrollHandler}
scrollEventThrottle={5} scrollEventThrottle={5}
renderEmptyState={renderPostsEmpty} renderEmptyState={renderPostsEmpty}
headerOffset={headerHeight} headerOffset={headerHeight}
@ -430,19 +434,29 @@ const AboutSection = observer(function AboutPageImpl({
feedOwnerDid, feedOwnerDid,
feedRkey, feedRkey,
feedInfo, feedInfo,
headerHeight,
onToggleLiked, onToggleLiked,
onScroll,
}: { }: {
feedOwnerDid: string feedOwnerDid: string
feedRkey: string feedRkey: string
feedInfo: FeedSourceModel | undefined feedInfo: FeedSourceModel | undefined
headerHeight: number
onToggleLiked: () => void onToggleLiked: () => void
onScroll: (e: NativeScrollEvent) => void
}) { }) {
const pal = usePalette('default') const pal = usePalette('default')
const scrollHandler = useAnimatedScrollHandler({onScroll})
if (!feedInfo) { if (!feedInfo) {
return <View /> return <View />
} }
return ( return (
<ScrollView
scrollEventThrottle={1}
contentContainerStyle={{paddingTop: headerHeight}}
onScroll={scrollHandler}>
<View <View
style={[ style={[
{ {
@ -506,6 +520,7 @@ const AboutSection = observer(function AboutPageImpl({
)} )}
</Text> </Text>
</View> </View>
</ScrollView>
) )
}) })

View File

@ -2,6 +2,7 @@ import React, {useCallback, useMemo} from 'react'
import { import {
ActivityIndicator, ActivityIndicator,
FlatList, FlatList,
NativeScrollEvent,
Pressable, Pressable,
StyleSheet, StyleSheet,
View, View,
@ -10,6 +11,7 @@ import {useFocusEffect} from '@react-navigation/native'
import {NativeStackScreenProps, CommonNavigatorParams} from 'lib/routes/types' import {NativeStackScreenProps, CommonNavigatorParams} from 'lib/routes/types'
import {useNavigation} from '@react-navigation/native' import {useNavigation} from '@react-navigation/native'
import {FontAwesomeIcon} from '@fortawesome/react-native-fontawesome' import {FontAwesomeIcon} from '@fortawesome/react-native-fontawesome'
import {useAnimatedScrollHandler} from 'react-native-reanimated'
import {observer} from 'mobx-react-lite' import {observer} from 'mobx-react-lite'
import {RichText as RichTextAPI} from '@atproto/api' import {RichText as RichTextAPI} from '@atproto/api'
import {withAuthRequired} from 'view/com/auth/withAuthRequired' import {withAuthRequired} from 'view/com/auth/withAuthRequired'
@ -33,7 +35,6 @@ import {useStores} from 'state/index'
import {usePalette} from 'lib/hooks/usePalette' import {usePalette} from 'lib/hooks/usePalette'
import {useSetTitle} from 'lib/hooks/useSetTitle' import {useSetTitle} from 'lib/hooks/useSetTitle'
import {useWebMediaQueries} from 'lib/hooks/useWebMediaQueries' import {useWebMediaQueries} from 'lib/hooks/useWebMediaQueries'
import {OnScrollCb} from 'lib/hooks/useOnMainScroll'
import {NavigationProp} from 'lib/routes/types' import {NavigationProp} from 'lib/routes/types'
import {toShareUrl} from 'lib/strings/url-helpers' import {toShareUrl} from 'lib/strings/url-helpers'
import {shareUrl} from 'lib/sharing' import {shareUrl} from 'lib/sharing'
@ -544,7 +545,7 @@ const Header = observer(function HeaderImpl({
interface FeedSectionProps { interface FeedSectionProps {
feed: PostsFeedModel feed: PostsFeedModel
onScroll: OnScrollCb onScroll: (e: NativeScrollEvent) => void
headerHeight: number headerHeight: number
isScrolledDown: boolean isScrolledDown: boolean
} }
@ -568,13 +569,14 @@ const FeedSection = React.forwardRef<SectionRef, FeedSectionProps>(
return <EmptyState icon="feed" message="This feed is empty!" /> return <EmptyState icon="feed" message="This feed is empty!" />
}, []) }, [])
const scrollHandler = useAnimatedScrollHandler({onScroll})
return ( return (
<View> <View>
<Feed <Feed
testID="listFeed" testID="listFeed"
feed={feed} feed={feed}
scrollElRef={scrollElRef} scrollElRef={scrollElRef}
onScroll={onScroll} onScroll={scrollHandler}
scrollEventThrottle={1} scrollEventThrottle={1}
renderEmptyState={renderPostsEmpty} renderEmptyState={renderPostsEmpty}
headerOffset={headerHeight} headerOffset={headerHeight}
@ -598,7 +600,7 @@ interface AboutSectionProps {
isCurateList: boolean | undefined isCurateList: boolean | undefined
isOwner: boolean | undefined isOwner: boolean | undefined
onPressAddUser: () => void onPressAddUser: () => void
onScroll: OnScrollCb onScroll: (e: NativeScrollEvent) => void
headerHeight: number headerHeight: number
isScrolledDown: boolean isScrolledDown: boolean
} }
@ -723,6 +725,7 @@ const AboutSection = React.forwardRef<SectionRef, AboutSectionProps>(
) )
}, []) }, [])
const scrollHandler = useAnimatedScrollHandler({onScroll})
return ( return (
<View> <View>
<ListItems <ListItems
@ -732,7 +735,7 @@ const AboutSection = React.forwardRef<SectionRef, AboutSectionProps>(
renderEmptyState={renderEmptyState} renderEmptyState={renderEmptyState}
list={list} list={list}
headerOffset={headerHeight} headerOffset={headerHeight}
onScroll={onScroll} onScroll={scrollHandler}
scrollEventThrottle={1} scrollEventThrottle={1}
/> />
{isScrolledDown && ( {isScrolledDown && (