-
Notifications
You must be signed in to change notification settings - Fork 373
Expand file tree
/
Copy pathBottomSheetModal.tsx
More file actions
186 lines (169 loc) · 4.64 KB
/
BottomSheetModal.tsx
File metadata and controls
186 lines (169 loc) · 4.64 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
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import React, { PropsWithChildren, useEffect, useMemo } from 'react';
import {
Animated,
Keyboard,
KeyboardEvent,
Modal,
StyleSheet,
TouchableWithoutFeedback,
useWindowDimensions,
View,
} from 'react-native';
import {
Gesture,
GestureDetector,
GestureHandlerRootView,
GestureUpdateEvent,
PanGestureHandlerEventPayload,
} from 'react-native-gesture-handler';
import { runOnJS } from 'react-native-reanimated';
import { useTheme } from '../../contexts/themeContext/ThemeContext';
export type BottomSheetModalProps = {
/**
* Function to call when the modal is closed.
* @returns void
*/
onClose: () => void;
/**
* Whether the modal is visible.
*/
visible: boolean;
/**
* The height of the modal.
*/
height?: number;
};
/**
* A modal that slides up from the bottom of the screen.
*/
export const BottomSheetModal = (props: PropsWithChildren<BottomSheetModalProps>) => {
const { height: windowHeight, width: windowWidth } = useWindowDimensions();
const { children, height = windowHeight / 2, onClose, visible } = props;
const {
theme: {
bottomSheetModal: { container, contentContainer, handle, overlay: overlayTheme, wrapper },
colors: { grey, overlay, white_snow },
},
} = useTheme();
const translateY = useMemo(() => new Animated.Value(height), [height]);
const openAnimation = useMemo(
() =>
Animated.timing(translateY, {
duration: 200,
toValue: 0,
useNativeDriver: true,
}),
[translateY],
);
const closeAnimation = Animated.timing(translateY, {
duration: 50,
toValue: height,
useNativeDriver: true,
});
const handleDismiss = () => {
closeAnimation.start(() => onClose());
};
useEffect(() => {
if (visible) {
openAnimation.start();
}
}, [visible, openAnimation]);
useEffect(() => {
const keyboardDidShowListener = Keyboard.addListener('keyboardDidShow', keyboardDidShow);
const keyboardDidHideListener = Keyboard.addListener('keyboardDidHide', keyboardDidHide);
return () => {
keyboardDidShowListener.remove();
keyboardDidHideListener.remove();
};
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
const keyboardDidShow = (event: KeyboardEvent) => {
Animated.timing(translateY, {
duration: 250,
toValue: -event.endCoordinates.height,
useNativeDriver: true,
}).start();
};
const keyboardDidHide = () => {
Animated.timing(translateY, {
duration: 250,
toValue: 0,
useNativeDriver: true,
}).start();
};
const handleUpdate = (event: GestureUpdateEvent<PanGestureHandlerEventPayload>) => {
const translationY = Math.max(event.translationY, 0);
translateY.setValue(translationY);
};
const gesture = Gesture.Pan()
.onUpdate((event) => {
runOnJS(handleUpdate)(event);
})
.onEnd((event) => {
if (event.velocityY > 500 || event.translationY > height / 2) {
runOnJS(handleDismiss)();
} else {
runOnJS(openAnimation.start)();
}
});
return (
<View style={[styles.wrapper, wrapper]}>
<Modal onRequestClose={onClose} transparent visible={visible}>
<GestureHandlerRootView style={{ flex: 1 }}>
<GestureDetector gesture={gesture}>
<View style={[styles.overlay, { backgroundColor: overlay }, overlayTheme]}>
<TouchableWithoutFeedback onPress={onClose} style={{ flex: 1 }}>
<View style={{ flex: 1 }} />
</TouchableWithoutFeedback>
<Animated.View
style={[
styles.container,
{
backgroundColor: white_snow,
height,
transform: [{ translateY }],
},
container,
]}
>
<View
style={[styles.handle, { backgroundColor: grey, width: windowWidth / 4 }, handle]}
/>
<View style={[styles.contentContainer, contentContainer]}>{children}</View>
</Animated.View>
</View>
</GestureDetector>
</GestureHandlerRootView>
</Modal>
</View>
);
};
const styles = StyleSheet.create({
container: {
borderTopLeftRadius: 16,
borderTopRightRadius: 16,
},
content: {
flex: 1,
padding: 16,
},
contentContainer: {
flex: 1,
marginTop: 8,
},
handle: {
alignSelf: 'center',
borderRadius: 4,
height: 4,
marginVertical: 8,
},
overlay: {
flex: 1,
justifyContent: 'flex-end',
},
wrapper: {
alignItems: 'center',
flex: 1,
justifyContent: 'center',
},
});