/
Overlay.tsx
82 lines (73 loc) · 2.06 KB
/
Overlay.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
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
78
79
80
81
82
import * as React from 'react';
import { Platform, Pressable, StyleSheet } from 'react-native';
import Animated, {
useAnimatedProps,
useAnimatedStyle,
} from 'react-native-reanimated';
const PROGRESS_EPSILON = 0.05;
type Props = React.ComponentProps<typeof Animated.View> & {
progress: Animated.SharedValue<number>;
onPress: () => void;
accessibilityLabel?: string;
};
const Overlay = React.forwardRef(function Overlay(
{
progress,
onPress,
style,
accessibilityLabel = 'Close drawer',
...props
}: Props,
ref: React.Ref<Animated.View>
) {
const animatedStyle = useAnimatedStyle(() => {
return {
opacity: progress.value,
// We don't want the user to be able to press through the overlay when drawer is open
// We can send the overlay behind the screen to avoid it
zIndex: progress.value > PROGRESS_EPSILON ? 0 : -1,
};
});
const animatedProps = useAnimatedProps(() => {
const active = progress.value > PROGRESS_EPSILON;
return {
pointerEvents: active ? 'auto' : 'none',
accessibilityElementsHidden: !active,
importantForAccessibility: active ? 'auto' : 'no-hide-descendants',
} as const;
});
return (
<Animated.View
{...props}
ref={ref}
style={[styles.overlay, overlayStyle, animatedStyle, style]}
animatedProps={animatedProps}
>
<Pressable
onPress={onPress}
style={styles.pressable}
accessibilityRole="button"
accessibilityLabel={accessibilityLabel}
/>
</Animated.View>
);
});
const overlayStyle = Platform.select<Record<string, string>>({
web: {
// Disable touch highlight on mobile Safari.
// WebkitTapHighlightColor must be used outside of StyleSheet.create because react-native-web will omit the property.
WebkitTapHighlightColor: 'transparent',
},
default: {},
});
const styles = StyleSheet.create({
overlay: {
...StyleSheet.absoluteFillObject,
backgroundColor: 'rgba(0, 0, 0, 0.5)',
},
pressable: {
flex: 1,
pointerEvents: 'auto',
},
});
export default Overlay;