blob: 6f47689d1d1f838eb102e6e1c6d3c38a00738469 (
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
42
43
44
|
import {createContext, useContext, useEffect, useState} from 'react'
import {isWeb} from '#/platform/detection'
const LightStatusBarRefCountContext = createContext<boolean>(false)
const SetLightStatusBarRefCountContext = createContext<React.Dispatch<
React.SetStateAction<number>
> | null>(null)
export function useLightStatusBar() {
return useContext(LightStatusBarRefCountContext)
}
export function useSetLightStatusBar(enabled: boolean) {
const setRefCount = useContext(SetLightStatusBarRefCountContext)
useEffect(() => {
// noop on web -sfn
if (isWeb) return
if (!setRefCount) {
if (__DEV__)
console.error(
'useLightStatusBar was used without a SetLightStatusBarRefCountContext provider',
)
return
}
if (enabled) {
setRefCount(prev => prev + 1)
return () => setRefCount(prev => prev - 1)
}
}, [enabled, setRefCount])
}
export function Provider({children}: React.PropsWithChildren<{}>) {
const [refCount, setRefCount] = useState(0)
return (
<SetLightStatusBarRefCountContext.Provider value={setRefCount}>
<LightStatusBarRefCountContext.Provider value={refCount > 0}>
{children}
</LightStatusBarRefCountContext.Provider>
</SetLightStatusBarRefCountContext.Provider>
)
}
|