Extract shell state into separate context (#1824)
* WIP * Add shell state * Integrate new shell state for drawer and minimal shell mode * Replace isDrawerSwipeDisabled * Split shell state into separate contexts to avoid needless re-renders * Fix typo --------- Co-authored-by: Paul Frazee <pfrazee@gmail.com>
This commit is contained in:
parent
7158157f5f
commit
bfe196bac5
51 changed files with 368 additions and 238 deletions
|
@ -1,8 +1,8 @@
|
|||
import {useState, useCallback, useRef} from 'react'
|
||||
import {NativeSyntheticEvent, NativeScrollEvent} from 'react-native'
|
||||
import {RootStoreModel} from 'state/index'
|
||||
import {s} from 'lib/styles'
|
||||
import {useWebMediaQueries} from './useWebMediaQueries'
|
||||
import {useSetMinimalShellMode, useMinimalShellMode} from '#/state/shell'
|
||||
|
||||
const Y_LIMIT = 10
|
||||
|
||||
|
@ -19,12 +19,12 @@ export type OnScrollCb = (
|
|||
) => void
|
||||
export type ResetCb = () => void
|
||||
|
||||
export function useOnMainScroll(
|
||||
store: RootStoreModel,
|
||||
): [OnScrollCb, boolean, ResetCb] {
|
||||
export function useOnMainScroll(): [OnScrollCb, boolean, ResetCb] {
|
||||
let lastY = useRef(0)
|
||||
let [isScrolledDown, setIsScrolledDown] = useState(false)
|
||||
const {dyLimitUp, dyLimitDown} = useDeviceLimits()
|
||||
const minimalShellMode = useMinimalShellMode()
|
||||
const setMinimalShellMode = useSetMinimalShellMode()
|
||||
|
||||
return [
|
||||
useCallback(
|
||||
|
@ -33,13 +33,10 @@ export function useOnMainScroll(
|
|||
const dy = y - (lastY.current || 0)
|
||||
lastY.current = y
|
||||
|
||||
if (!store.shell.minimalShellMode && dy > dyLimitDown && y > Y_LIMIT) {
|
||||
store.shell.setMinimalShellMode(true)
|
||||
} else if (
|
||||
store.shell.minimalShellMode &&
|
||||
(dy < dyLimitUp * -1 || y <= Y_LIMIT)
|
||||
) {
|
||||
store.shell.setMinimalShellMode(false)
|
||||
if (!minimalShellMode && dy > dyLimitDown && y > Y_LIMIT) {
|
||||
setMinimalShellMode(true)
|
||||
} else if (minimalShellMode && (dy < dyLimitUp * -1 || y <= Y_LIMIT)) {
|
||||
setMinimalShellMode(false)
|
||||
}
|
||||
|
||||
if (
|
||||
|
@ -54,13 +51,19 @@ export function useOnMainScroll(
|
|||
setIsScrolledDown(false)
|
||||
}
|
||||
},
|
||||
[store.shell, dyLimitDown, dyLimitUp, isScrolledDown],
|
||||
[
|
||||
dyLimitDown,
|
||||
dyLimitUp,
|
||||
isScrolledDown,
|
||||
minimalShellMode,
|
||||
setMinimalShellMode,
|
||||
],
|
||||
),
|
||||
isScrolledDown,
|
||||
useCallback(() => {
|
||||
setIsScrolledDown(false)
|
||||
store.shell.setMinimalShellMode(false)
|
||||
setMinimalShellMode(false)
|
||||
lastY.current = 1e8 // NOTE we set this very high so that the onScroll logic works right -prf
|
||||
}, [store, setIsScrolledDown]),
|
||||
}, [setIsScrolledDown, setMinimalShellMode]),
|
||||
]
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue