forked from codeherence/react-native-header
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFadingView.tsx
More file actions
77 lines (71 loc) · 2.13 KB
/
FadingView.tsx
File metadata and controls
77 lines (71 loc) · 2.13 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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import React, { forwardRef } from 'react';
import { StyleProp, StyleSheet, ViewStyle } from 'react-native';
import Animated, {
AnimatedStyle,
SharedValue,
useAnimatedProps,
useAnimatedStyle,
} from 'react-native-reanimated';
type AnimatedViewPointerEvents = React.ComponentProps<typeof Animated.View>['pointerEvents'];
type FadingViewProps = {
/**
* Animated props to be passed to the Animated.View
*
* @default undefined
* @type {Animated.AnimateStyle<StyleProp<ViewStyle>>}
*/
style?: StyleProp<AnimatedStyle<StyleProp<ViewStyle>>>;
/**
* The opacity value to be used for the fade animation.
*
* @default undefined
* @type {SharedValue<number>}
*/
opacity: SharedValue<number>;
/**
* The opacity threshold to enable pointer events. If the opacity value is greater
* than or equal to this value, pointer events will be enabled. Otherwise, pointer
* events will be disabled.
*
* @default 1
* @type {number}
*/
opacityThresholdToEnablePointerEvents?: number;
/**
* The children to be rendered inside the FadingView.
*/
children?: React.ReactNode;
} & React.ComponentProps<typeof Animated.View>;
const FadingView = forwardRef<Animated.View, FadingViewProps>(
(
{
children,
style,
opacity,
// Remove animatedProps from rest to avoid passing stale/spread values
animatedProps: _externalAnimatedProps,
opacityThresholdToEnablePointerEvents = 1,
...rest
},
ref
) => {
const animatedProps = useAnimatedProps(() => {
const _pointerEvents: AnimatedViewPointerEvents =
opacity.value >= opacityThresholdToEnablePointerEvents ? 'auto' : 'none';
return { pointerEvents: _pointerEvents };
}, [opacityThresholdToEnablePointerEvents]);
const fadeStyle = useAnimatedStyle(() => ({ opacity: opacity.value }));
return (
<Animated.View
ref={ref}
style={[styles.container, style, fadeStyle]}
animatedProps={animatedProps}
{...rest}
>
{children}
</Animated.View>
);
}
);
export default FadingView;
const styles = StyleSheet.create({ container: { opacity: 0 } });