Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Account for momentum when hiding minimal shell #3740

Merged
merged 4 commits into from
Apr 28, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/lib/ScrollContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ const ScrollContext = createContext<ScrollHandlers<any>>({
onBeginDrag: undefined,
onEndDrag: undefined,
onScroll: undefined,
onMomentumBegin: undefined,
onMomentumEnd: undefined,
})

export function useScrollHandlers(): ScrollHandlers<any> {
Expand All @@ -20,14 +22,18 @@ export function ScrollProvider({
onBeginDrag,
onEndDrag,
onScroll,
onMomentumBegin,
onMomentumEnd,
}: ProviderProps) {
const handlers = useMemo(
() => ({
onBeginDrag,
onEndDrag,
onScroll,
onMomentumBegin,
onMomentumEnd,
}),
[onBeginDrag, onEndDrag, onScroll],
[onBeginDrag, onEndDrag, onScroll, onMomentumBegin, onMomentumEnd],
)
return (
<ScrollContext.Provider value={handlers}>{children}</ScrollContext.Provider>
Expand Down
6 changes: 6 additions & 0 deletions src/screens/Profile/Sections/Labels.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,12 @@ export function ProfileLabelsSectionInner({
onScroll(e, ctx) {
contextScrollHandlers.onScroll?.(e, ctx)
},
onMomentumBegin(e, ctx) {
contextScrollHandlers.onMomentumBegin?.(e, ctx)
},
onMomentumEnd(e, ctx) {
contextScrollHandlers.onMomentumEnd?.(e, ctx)
},
})

const {labelValues} = labelerInfo.policies
Expand Down
6 changes: 6 additions & 0 deletions src/view/com/util/List.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,12 @@ function ListImpl<ItemT>(
}
}
},
onMomentumBegin(e, ctx) {
contextScrollHandlers.onMomentumBegin?.(e, ctx)
},
onMomentumEnd(e, ctx) {
contextScrollHandlers.onMomentumEnd?.(e, ctx)
},
})

let refreshControl
Expand Down
59 changes: 46 additions & 13 deletions src/view/com/util/MainScrollProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import React, {useCallback, useEffect} from 'react'
import {NativeScrollEvent} from 'react-native'
import {interpolate, useSharedValue} from 'react-native-reanimated'
import EventEmitter from 'eventemitter3'

import {ScrollProvider} from '#/lib/ScrollContext'
import {NativeScrollEvent} from 'react-native'
import {useSetMinimalShellMode, useMinimalShellMode} from '#/state/shell'
import {useMinimalShellMode, useSetMinimalShellMode} from '#/state/shell'
import {useShellLayout} from '#/state/shell/shell-layout'
import {isNative, isWeb} from 'platform/detection'
import {useSharedValue, interpolate} from 'react-native-reanimated'

const WEB_HIDE_SHELL_THRESHOLD = 200

Expand All @@ -32,6 +33,31 @@ export function MainScrollProvider({children}: {children: React.ReactNode}) {
}
})

const snapToClosestState = useCallback(
(e: NativeScrollEvent) => {
'worklet'
if (isNative) {
if (startDragOffset.value === null) {
return
}
const didScrollDown = e.contentOffset.y > startDragOffset.value
startDragOffset.value = null
startMode.value = null
if (e.contentOffset.y < headerHeight.value) {
// If we're close to the top, show the shell.
setMode(false)
} else if (didScrollDown) {
// Showing the bar again on scroll down feels annoying, so don't.
setMode(true)
} else {
// Snap to whichever state is the closest.
setMode(Math.round(mode.value) === 1)
}
}
},
[startDragOffset, startMode, setMode, mode, headerHeight],
)

const onBeginDrag = useCallback(
(e: NativeScrollEvent) => {
'worklet'
Expand All @@ -47,18 +73,24 @@ export function MainScrollProvider({children}: {children: React.ReactNode}) {
(e: NativeScrollEvent) => {
'worklet'
if (isNative) {
startDragOffset.value = null
startMode.value = null
if (e.contentOffset.y < headerHeight.value / 2) {
// If we're close to the top, show the shell.
setMode(false)
} else {
// Snap to whichever state is the closest.
setMode(Math.round(mode.value) === 1)
if (e.velocity && e.velocity.y !== 0) {
// If we detect a velocity, wait for onMomentumEnd to snap.
return
}
snapToClosestState(e)
}
},
[startDragOffset, startMode, setMode, mode, headerHeight],
[snapToClosestState],
)

const onMomentumEnd = useCallback(
(e: NativeScrollEvent) => {
'worklet'
if (isNative) {
snapToClosestState(e)
}
},
[snapToClosestState],
)

const onScroll = useCallback(
Expand Down Expand Up @@ -119,7 +151,8 @@ export function MainScrollProvider({children}: {children: React.ReactNode}) {
<ScrollProvider
onBeginDrag={onBeginDrag}
onEndDrag={onEndDrag}
onScroll={onScroll}>
onScroll={onScroll}
onMomentumEnd={onMomentumEnd}>
{children}
</ScrollProvider>
)
Expand Down
Loading