Pull useAnimatedScrollHandler back up (#1858)

* Revert "Pull animated scroll handler down from pager"

This reverts commit ecebb78e40148b9160f832d26ada1d366551b645.

* Only handle onScroll for current page
zio/stable
dan 2023-11-09 23:47:54 +00:00 committed by GitHub
parent fb4f5709c4
commit 487d871cfd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 26 additions and 47 deletions

View File

@ -1,13 +1,9 @@
import * as React from 'react' import * as React from 'react'
import { import {LayoutChangeEvent, StyleSheet, View} from 'react-native'
LayoutChangeEvent,
NativeScrollEvent,
StyleSheet,
View,
} from 'react-native'
import Animated, { import Animated, {
Easing, Easing,
useAnimatedReaction, useAnimatedReaction,
useAnimatedScrollHandler,
useAnimatedStyle, useAnimatedStyle,
useSharedValue, useSharedValue,
withTiming, withTiming,
@ -16,12 +12,13 @@ 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: (e: NativeScrollEvent) => void onScroll: OnScrollCb
isScrolledDown: boolean isScrolledDown: boolean
} }
@ -143,25 +140,12 @@ export const PagerWithHeader = React.forwardRef<PagerRef, PagerWithHeaderProps>(
], ],
) )
// Ideally we'd call useAnimatedScrollHandler here but we can't safely do that // props to pass into children render functions
// due to https://github.com/software-mansion/react-native-reanimated/issues/5345. const onScroll = useAnimatedScrollHandler({
// So instead we pass down a worklet, and individual pages will have to call it. onScroll(e) {
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>(() => {
return {
headerHeight,
onScroll,
isScrolledDown,
}
}, [headerHeight, onScroll, isScrolledDown])
const onPageSelectedInner = React.useCallback( const onPageSelectedInner = React.useCallback(
(index: number) => { (index: number) => {
@ -205,7 +189,11 @@ export const PagerWithHeader = React.forwardRef<PagerRef, PagerWithHeaderProps>(
headerOnlyHeight > 0 && headerOnlyHeight > 0 &&
tabBarHeight > 0 tabBarHeight > 0
) { ) {
output = child(childProps) output = child({
headerHeight,
isScrolledDown,
onScroll: i === currentPage ? onScroll : noop,
})
} }
// Pager children must be noncollapsible plain <View>s. // Pager children must be noncollapsible plain <View>s.
return ( return (
@ -237,6 +225,8 @@ const styles = StyleSheet.create({
}, },
}) })
function noop() {}
function toArray<T>(v: T | T[]): T[] { function toArray<T>(v: T | T[]): T[] {
if (Array.isArray(v)) { if (Array.isArray(v)) {
return v return v

View File

@ -1,14 +1,7 @@
import React, {useMemo, useCallback} from 'react' import React, {useMemo, useCallback} from 'react'
import { import {FlatList, StyleSheet, View, ActivityIndicator} from 'react-native'
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'
@ -33,6 +26,7 @@ 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'
@ -389,7 +383,7 @@ export const ProfileFeedScreenInner = observer(
interface FeedSectionProps { interface FeedSectionProps {
feed: PostsFeedModel feed: PostsFeedModel
onScroll: (e: NativeScrollEvent) => void onScroll: OnScrollCb
headerHeight: number headerHeight: number
isScrolledDown: boolean isScrolledDown: boolean
} }
@ -414,13 +408,12 @@ 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={scrollHandler} onScroll={onScroll}
scrollEventThrottle={5} scrollEventThrottle={5}
renderEmptyState={renderPostsEmpty} renderEmptyState={renderPostsEmpty}
headerOffset={headerHeight} headerOffset={headerHeight}
@ -450,11 +443,10 @@ const AboutSection = observer(function AboutPageImpl({
feedInfo: FeedSourceModel | undefined feedInfo: FeedSourceModel | undefined
headerHeight: number headerHeight: number
onToggleLiked: () => void onToggleLiked: () => void
onScroll: (e: NativeScrollEvent) => void onScroll: OnScrollCb
}) { }) {
const pal = usePalette('default') const pal = usePalette('default')
const {_} = useLingui() const {_} = useLingui()
const scrollHandler = useAnimatedScrollHandler({onScroll})
if (!feedInfo) { if (!feedInfo) {
return <View /> return <View />
@ -464,7 +456,7 @@ const AboutSection = observer(function AboutPageImpl({
<ScrollView <ScrollView
scrollEventThrottle={1} scrollEventThrottle={1}
contentContainerStyle={{paddingTop: headerHeight}} contentContainerStyle={{paddingTop: headerHeight}}
onScroll={scrollHandler}> onScroll={onScroll}>
<View <View
style={[ style={[
{ {

View File

@ -2,7 +2,6 @@ import React, {useCallback, useMemo} from 'react'
import { import {
ActivityIndicator, ActivityIndicator,
FlatList, FlatList,
NativeScrollEvent,
Pressable, Pressable,
StyleSheet, StyleSheet,
View, View,
@ -11,7 +10,6 @@ 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'
@ -35,6 +33,7 @@ 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'
@ -555,7 +554,7 @@ const Header = observer(function HeaderImpl({
interface FeedSectionProps { interface FeedSectionProps {
feed: PostsFeedModel feed: PostsFeedModel
onScroll: (e: NativeScrollEvent) => void onScroll: OnScrollCb
headerHeight: number headerHeight: number
isScrolledDown: boolean isScrolledDown: boolean
} }
@ -579,14 +578,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
testID="listFeed" testID="listFeed"
feed={feed} feed={feed}
scrollElRef={scrollElRef} scrollElRef={scrollElRef}
onScroll={scrollHandler} onScroll={onScroll}
scrollEventThrottle={1} scrollEventThrottle={1}
renderEmptyState={renderPostsEmpty} renderEmptyState={renderPostsEmpty}
headerOffset={headerHeight} headerOffset={headerHeight}
@ -610,7 +608,7 @@ interface AboutSectionProps {
isCurateList: boolean | undefined isCurateList: boolean | undefined
isOwner: boolean | undefined isOwner: boolean | undefined
onPressAddUser: () => void onPressAddUser: () => void
onScroll: (e: NativeScrollEvent) => void onScroll: OnScrollCb
headerHeight: number headerHeight: number
isScrolledDown: boolean isScrolledDown: boolean
} }
@ -741,7 +739,6 @@ const AboutSection = React.forwardRef<SectionRef, AboutSectionProps>(
) )
}, []) }, [])
const scrollHandler = useAnimatedScrollHandler({onScroll})
return ( return (
<View> <View>
<ListItems <ListItems
@ -751,7 +748,7 @@ const AboutSection = React.forwardRef<SectionRef, AboutSectionProps>(
renderEmptyState={renderEmptyState} renderEmptyState={renderEmptyState}
list={list} list={list}
headerOffset={headerHeight} headerOffset={headerHeight}
onScroll={scrollHandler} onScroll={onScroll}
scrollEventThrottle={1} scrollEventThrottle={1}
/> />
{isScrolledDown && ( {isScrolledDown && (