Skip to content

Commit

Permalink
perf: snap using animated reaction scroll
Browse files Browse the repository at this point in the history
  • Loading branch information
andreialecu committed May 9, 2022
1 parent a429399 commit 5f11c61
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 16 deletions.
35 changes: 20 additions & 15 deletions src/hooks.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,17 @@ export const useScrollHandlerY = (name: TabName) => {

const scrollTo = useScroller()

const scrollAnimation = useSharedValue<number | undefined>(undefined)

useAnimatedReaction(
() => scrollAnimation.value,
(val) => {
if (val !== undefined) {
scrollTo(refMap[name], 0, val, false, '[useAnimatedReaction scroll]')
}
}
)

const onMomentumEnd = () => {
'worklet'
if (!enabled.value) return
Expand All @@ -307,13 +318,9 @@ export const useScrollHandlerY = (name: TabName) => {
accDiffClamp.value = withTiming(headerScrollDistance.value)

if (scrollYCurrent.value < headerScrollDistance.value) {
scrollTo(
refMap[name],
0,
headerScrollDistance.value,
true,
`[${name}] sticky snap up`
)
scrollAnimation.value = scrollYCurrent.value
scrollAnimation.value = withTiming(headerScrollDistance.value)
//console.log('[${name}] sticky snap up')
}
}
} else {
Expand All @@ -327,17 +334,15 @@ export const useScrollHandlerY = (name: TabName) => {
) {
// snap down
snappingTo.value = 0
scrollTo(refMap[name], 0, 0, true, `[${name}] snap down`)
scrollAnimation.value = scrollYCurrent.value
scrollAnimation.value = withTiming(0)
//console.log('[${name}] snap down')
} else if (scrollYCurrent.value <= headerScrollDistance.value) {
// snap up
snappingTo.value = headerScrollDistance.value
scrollTo(
refMap[name],
0,
headerScrollDistance.value,
true,
`[${name}] snap up`
)
scrollAnimation.value = scrollYCurrent.value
scrollAnimation.value = withTiming(headerScrollDistance.value)
//console.log('[${name}] snap up')
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ export type ContextType<T extends TabName = TabName> = {
offset: Animated.SharedValue<number>

/**
* The next snapping value, used only with diffClamp.
* The next snapping value.
*/
snappingTo: Animated.SharedValue<number>

Expand Down

0 comments on commit 5f11c61

Please sign in to comment.