about summary refs log tree commit diff
path: root/src/state/shell/minimal-mode.tsx
blob: 6e608daf4456c0f9f6d8eb5198838371347b33c2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import React from 'react'
import {cancelAnimation, SharedValue, useSharedValue, withSpring} from 'react-native-reanimated'

type StateContext = SharedValue<number>
type SetContext = (v: boolean) => void

const stateContext = React.createContext<StateContext>({
  value: 0,
  addListener() {},
  removeListener() {},
  modify() {},
})
const setContext = React.createContext<SetContext>((_: boolean) => {})

export function Provider({children}: React.PropsWithChildren<{}>) {
  const mode = useSharedValue(0)
  const setMode = React.useCallback(
    (v: boolean) => {
      'worklet'
      // Cancel any existing animation
      cancelAnimation(mode)
      mode.value = withSpring(v ? 1 : 0, {
        overshootClamping: true,
      })
    },
    [mode],
  )
  return (
    <stateContext.Provider value={mode}>
      <setContext.Provider value={setMode}>{children}</setContext.Provider>
    </stateContext.Provider>
  )
}

export function useMinimalShellMode() {
  return React.useContext(stateContext)
}

export function useSetMinimalShellMode() {
  return React.useContext(setContext)
}