forked from jkomyno/react-native-animated-hide-view
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
38 lines (33 loc) · 998 Bytes
/
index.js
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
import React, { useEffect, useRef, useState } from 'react';
import { Animated } from 'react-native';
const AnimatedHideView = ({ visible, unmountOnHide, style, children, ...otherProps }) => {
const fadeAnim = useRef(new Animated.Value(visible ? 1 : 0)).current;
const [pointerEvents, setPointerEvents] = useState(visible ? 'auto' : 'none');
useEffect(() => {
if (visible) {
setPointerEvents('auto');
}
Animated.timing(fadeAnim, {
toValue: visible ? 1 : 0,
duration: 300,
useNativeDriver: true,
}).start(() => {
if (!visible && unmountOnHide) {
setPointerEvents('none');
}
});
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [visible]);
if (unmountOnHide && !visible) {
return null;
}
return (
<Animated.View
{...otherProps}
pointerEvents={pointerEvents}
style={{ ...style, opacity: fadeAnim }}>
{children}
</Animated.View>
);
};
export default AnimatedHideView;