Skip to content

Commit

Permalink
Work around web stale closure bug in Reanimated (#1865)
Browse files Browse the repository at this point in the history
  • Loading branch information
gaearon authored Nov 10, 2023
1 parent 487d871 commit 8d7475c
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 7 deletions.
1 change: 1 addition & 0 deletions src/lib/hooks/useAnimatedScrollHandler_FIXED.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export {useAnimatedScrollHandler} from 'react-native-reanimated'
44 changes: 44 additions & 0 deletions src/lib/hooks/useAnimatedScrollHandler_FIXED.web.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import {useRef, useEffect} from 'react'
import {useAnimatedScrollHandler as useAnimatedScrollHandler_BUGGY} from 'react-native-reanimated'

export const useAnimatedScrollHandler: typeof useAnimatedScrollHandler_BUGGY = (
config,
deps,
) => {
const ref = useRef(config)
useEffect(() => {
ref.current = config
})
return useAnimatedScrollHandler_BUGGY(
{
onBeginDrag(e) {
if (typeof ref.current !== 'function' && ref.current.onBeginDrag) {
ref.current.onBeginDrag(e)
}
},
onEndDrag(e) {
if (typeof ref.current !== 'function' && ref.current.onEndDrag) {
ref.current.onEndDrag(e)
}
},
onMomentumBegin(e) {
if (typeof ref.current !== 'function' && ref.current.onMomentumBegin) {
ref.current.onMomentumBegin(e)
}
},
onMomentumEnd(e) {
if (typeof ref.current !== 'function' && ref.current.onMomentumEnd) {
ref.current.onMomentumEnd(e)
}
},
onScroll(e) {
if (typeof ref.current === 'function') {
ref.current(e)
} else if (ref.current.onScroll) {
ref.current.onScroll(e)
}
},
},
deps,
)
}
8 changes: 2 additions & 6 deletions src/lib/hooks/useOnMainScroll.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,8 @@ import {useSetMinimalShellMode, useMinimalShellMode} from '#/state/shell'
import {useShellLayout} from '#/state/shell/shell-layout'
import {s} from 'lib/styles'
import {isWeb} from 'platform/detection'
import {
useAnimatedScrollHandler,
useSharedValue,
interpolate,
runOnJS,
} from 'react-native-reanimated'
import {useSharedValue, interpolate, runOnJS} from 'react-native-reanimated'
import {useAnimatedScrollHandler} from './useAnimatedScrollHandler_FIXED'

function clamp(num: number, min: number, max: number) {
'worklet'
Expand Down
2 changes: 1 addition & 1 deletion src/view/com/pager/PagerWithHeader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import {LayoutChangeEvent, StyleSheet, View} from 'react-native'
import Animated, {
Easing,
useAnimatedReaction,
useAnimatedScrollHandler,
useAnimatedStyle,
useSharedValue,
withTiming,
Expand All @@ -13,6 +12,7 @@ import {Pager, PagerRef, RenderTabBarFnProps} from 'view/com/pager/Pager'
import {TabBar} from './TabBar'
import {useWebMediaQueries} from 'lib/hooks/useWebMediaQueries'
import {OnScrollCb} from 'lib/hooks/useOnMainScroll'
import {useAnimatedScrollHandler} from 'lib/hooks/useAnimatedScrollHandler_FIXED'

const SCROLLED_DOWN_LIMIT = 200

Expand Down

0 comments on commit 8d7475c

Please sign in to comment.