-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuseShowBlurView.ts
More file actions
41 lines (33 loc) · 1.23 KB
/
useShowBlurView.ts
File metadata and controls
41 lines (33 loc) · 1.23 KB
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, { useState } from 'react'
import { Platform } from 'react-native'
import { useFocusEffect } from '@react-navigation/native'
const isIOSDevice = Platform.OS === 'ios'
// Fix for TypeScript picking up the incorrect typing (node instead of react-native) for setTimeout
type Timeout = ReturnType<typeof setTimeout>
/**
* On Android devices the BlurView should wait for all interactions to finish.
* This is because it sometimes displays the wrong stack-components in the transparent background if it is displayed immediately.
* Because back-navigation from login-screen crashes the screen, additional timeout is added.
* iOS devices can display the BlurView right away
*/
const useShowBlurView = () => {
const [showBlurView, setShowBlurView] = useState(isIOSDevice)
useFocusEffect(
React.useCallback(() => {
let timer: Timeout
const timeout = isIOSDevice ? 0 : 200
const taskHandle = requestIdleCallback(() => {
timer = setTimeout(() => {
setShowBlurView(true)
}, timeout)
})
return () => {
cancelIdleCallback(taskHandle)
clearTimeout(timer)
setShowBlurView(isIOSDevice)
}
}, []),
)
return showBlurView
}
export default useShowBlurView