-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathDismissableView.tsx
37 lines (32 loc) · 1010 Bytes
/
DismissableView.tsx
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
import * as React from 'react'
import {Animated, ViewStyle} from 'react-native'
type Props = {
isVisible: boolean
children: React.ReactNode
duration?: number
style?: Exclude<ViewStyle, 'opacity'>
}
export const DismissibleView = ({isVisible, children, style, duration = 300}: Props) => {
const fadeAnim = React.useRef(new Animated.Value(0)).current
const [shouldRender, setShouldRender] = React.useState(isVisible)
React.useEffect(() => {
if (isVisible) {
fadeAnim.setValue(0)
setShouldRender(true)
Animated.timing(fadeAnim, {
toValue: 1,
duration,
useNativeDriver: true,
}).start()
} else {
fadeAnim.setValue(1)
Animated.timing(fadeAnim, {
toValue: 0,
duration,
useNativeDriver: true,
}).start(() => setShouldRender(false))
}
}, [isVisible, fadeAnim, duration])
if (!shouldRender) return null
return <Animated.View style={{...style, opacity: fadeAnim}}>{children}</Animated.View>
}