-
-
Notifications
You must be signed in to change notification settings - Fork 832
/
Copy pathBottomSheetBackdrop.tsx
168 lines (155 loc) · 4.75 KB
/
BottomSheetBackdrop.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import React, {
memo,
useCallback,
useEffect,
useMemo,
useRef,
useState,
} from 'react';
import type { ViewProps } from 'react-native';
import { Gesture, GestureDetector } from 'react-native-gesture-handler';
import Animated, {
interpolate,
useAnimatedStyle,
useAnimatedReaction,
runOnJS,
Extrapolation,
} from 'react-native-reanimated';
import { useBottomSheet } from '../../hooks';
import {
DEFAULT_ACCESSIBILITY_HINT,
DEFAULT_ACCESSIBILITY_LABEL,
DEFAULT_ACCESSIBILITY_ROLE,
DEFAULT_ACCESSIBLE,
DEFAULT_APPEARS_ON_INDEX,
DEFAULT_DISAPPEARS_ON_INDEX,
DEFAULT_ENABLE_TOUCH_THROUGH,
DEFAULT_OPACITY,
DEFAULT_PRESS_BEHAVIOR,
} from './constants';
import { styles } from './styles';
import type { BottomSheetDefaultBackdropProps } from './types';
const BottomSheetBackdropComponent = ({
animatedIndex,
opacity: _providedOpacity,
appearsOnIndex: _providedAppearsOnIndex,
disappearsOnIndex: _providedDisappearsOnIndex,
enableTouchThrough: _providedEnableTouchThrough,
pressBehavior = DEFAULT_PRESS_BEHAVIOR,
onPress,
style,
children,
accessible: _providedAccessible = DEFAULT_ACCESSIBLE,
accessibilityRole: _providedAccessibilityRole = DEFAULT_ACCESSIBILITY_ROLE,
accessibilityLabel: _providedAccessibilityLabel = DEFAULT_ACCESSIBILITY_LABEL,
accessibilityHint: _providedAccessibilityHint = DEFAULT_ACCESSIBILITY_HINT,
}: BottomSheetDefaultBackdropProps) => {
//#region hooks
const { snapToIndex, close } = useBottomSheet();
const isMounted = useRef(false);
//#endregion
//#region defaults
const opacity = _providedOpacity ?? DEFAULT_OPACITY;
const appearsOnIndex = _providedAppearsOnIndex ?? DEFAULT_APPEARS_ON_INDEX;
const disappearsOnIndex =
_providedDisappearsOnIndex ?? DEFAULT_DISAPPEARS_ON_INDEX;
const enableTouchThrough =
_providedEnableTouchThrough ?? DEFAULT_ENABLE_TOUCH_THROUGH;
//#endregion
//#region variables
const [pointerEvents, setPointerEvents] = useState<
ViewProps['pointerEvents']
>(enableTouchThrough ? 'none' : 'auto');
//#endregion
//#region callbacks
const handleOnPress = useCallback(() => {
onPress?.();
if (pressBehavior === 'close') {
close();
} else if (pressBehavior === 'collapse') {
snapToIndex(disappearsOnIndex as number);
} else if (typeof pressBehavior === 'number') {
snapToIndex(pressBehavior);
}
}, [snapToIndex, close, disappearsOnIndex, pressBehavior, onPress]);
const handleContainerTouchability = useCallback(
(shouldDisableTouchability: boolean) => {
isMounted.current &&
setPointerEvents(shouldDisableTouchability ? 'none' : 'auto');
},
[]
);
//#endregion
//#region tap gesture
const tapHandler = useMemo(() => {
const gesture = Gesture.Tap().onEnd(() => {
runOnJS(handleOnPress)();
});
return gesture;
}, [handleOnPress]);
//#endregion
//#region styles
const containerAnimatedStyle = useAnimatedStyle(
() => ({
opacity: interpolate(
animatedIndex.value,
[-1, disappearsOnIndex, appearsOnIndex],
[0, 0, opacity],
Extrapolation.CLAMP
),
flex: 1,
}),
[animatedIndex, appearsOnIndex, disappearsOnIndex, opacity]
);
const containerStyle = useMemo(
() => [styles.container, style, containerAnimatedStyle],
[style, containerAnimatedStyle]
);
//#endregion
//#region effects
useAnimatedReaction(
() => animatedIndex.value <= disappearsOnIndex,
(shouldDisableTouchability, previous) => {
if (shouldDisableTouchability === previous) {
return;
}
runOnJS(handleContainerTouchability)(shouldDisableTouchability);
},
[disappearsOnIndex]
);
// addressing updating the state after unmounting.
// [link](https://github.com/gorhom/react-native-bottom-sheet/issues/1376)
useEffect(() => {
isMounted.current = true;
return () => {
isMounted.current = false;
};
}, []);
//#endregion
const AnimatedView = (
<Animated.View
style={containerStyle}
pointerEvents={pointerEvents}
accessible={_providedAccessible ?? undefined}
accessibilityRole={_providedAccessibilityRole ?? undefined}
accessibilityLabel={_providedAccessibilityLabel ?? undefined}
accessibilityHint={
_providedAccessibilityHint
? _providedAccessibilityHint
: `Tap to ${
typeof pressBehavior === 'string' ? pressBehavior : 'move'
} the Bottom Sheet`
}
>
{children}
</Animated.View>
);
return pressBehavior !== 'none' ? (
<GestureDetector gesture={tapHandler}>{AnimatedView}</GestureDetector>
) : (
AnimatedView
);
};
const BottomSheetBackdrop = memo(BottomSheetBackdropComponent);
BottomSheetBackdrop.displayName = 'BottomSheetBackdrop';
export default BottomSheetBackdrop;