-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathBottomSheet.tsx
181 lines (163 loc) · 4.27 KB
/
BottomSheet.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
169
170
171
172
173
174
175
176
177
178
179
180
181
import React, { useCallback } from 'react';
import { View, StyleSheet, Button, Dimensions } from 'react-native';
import Animated, {
useSharedValue,
useAnimatedStyle,
withSpring,
interpolate,
} from 'react-native-reanimated';
import {
GestureHandlerRootView,
PanGestureHandler,
} from 'react-native-gesture-handler';
import { BlurView } from '@react-native-community/blur';
const { height: SCREEN_HEIGHT } = Dimensions.get('window');
const MAX_TRANSLATE_Y = -SCREEN_HEIGHT * 0.7;
const MIN_TRANSLATE_Y = 0;
const BottomSheet = () => {
const translateY = useSharedValue(0);
const active = useSharedValue(false);
const springConfig = {
damping: 10,
mass: 0.5,
stiffness: 100,
overshootClamping: false,
restDisplacementThreshold: 0.01,
restSpeedThreshold: 2,
};
const animatedStyle = useAnimatedStyle(() => {
return {
transform: [
{
translateY: translateY.value,
},
],
};
});
const animatedBlurStyle = useAnimatedStyle(() => {
const opacity = interpolate(
translateY.value,
[0, MAX_TRANSLATE_Y / 2],
[0, 1],
'clamp'
);
return {
opacity,
display: opacity === 0 ? 'none' : 'flex',
};
});
const toggleSheet = () => {
if (active.value) {
translateY.value = withSpring(0, springConfig);
} else {
translateY.value = withSpring(MAX_TRANSLATE_Y / 2, springConfig);
}
active.value = !active.value;
};
const gestureHandler = useCallback(
(event) => {
'worklet';
const { translationY, velocityY, state } = event.nativeEvent;
if (state === 4) {
const projectedY = translateY.value + velocityY * 0.2;
const snapPoints = [
MAX_TRANSLATE_Y,
MAX_TRANSLATE_Y / 2,
MIN_TRANSLATE_Y,
];
let nearestPoint = snapPoints[0];
let minDistance = Math.abs(projectedY - snapPoints[0]);
snapPoints.forEach((point) => {
const distance = Math.abs(projectedY - point);
if (distance < minDistance) {
minDistance = distance;
nearestPoint = point;
}
});
translateY.value = withSpring(nearestPoint, {
...springConfig,
velocity: velocityY,
});
active.value = nearestPoint !== 0;
} else {
const newTranslateY = translateY.value + translationY;
translateY.value = Math.max(MAX_TRANSLATE_Y, Math.min(MIN_TRANSLATE_Y, newTranslateY));
}
},
[]
);
return (
<GestureHandlerRootView style={styles.container}>
<Button title="Toggle Bottom Sheet" onPress={toggleSheet} />
<Animated.View style={[styles.blurContainer, animatedBlurStyle]}>
<BlurView
style={styles.absolute}
blurType="light"
blurAmount={3}
reducedTransparencyFallbackColor="rgba(0, 0, 0, 0.5)"
/>
</Animated.View>
<PanGestureHandler onGestureEvent={gestureHandler}>
<Animated.View style={[styles.bottomSheet, animatedStyle]}>
<View style={styles.handleContainer}>
<View style={styles.handle} />
</View>
</Animated.View>
</PanGestureHandler>
</GestureHandlerRootView>
);
};
export default BottomSheet;
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#f5f5f5',
},
bottomSheet: {
position: 'absolute',
height: SCREEN_HEIGHT,
width: '100%',
backgroundColor: '#fff',
borderTopLeftRadius: 40,
borderTopRightRadius: 40,
padding: 16,
alignItems: 'center',
bottom: -SCREEN_HEIGHT + 0,
elevation: 15,
shadowColor: '#000',
shadowOffset: { width: 0, height: -4 },
shadowOpacity: 0.25,
shadowRadius: 4,
zIndex: 2,
},
handleContainer: {
width: '100%',
alignItems: 'center',
paddingVertical: 8,
},
handle: {
width: 40,
height: 4,
backgroundColor: '#00000040',
borderRadius: 2,
marginBottom: 8,
},
blurContainer: {
position: 'absolute',
top: 0,
left: 0,
right: 0,
bottom: 0,
zIndex: 1,
backgroundColor: 'rgba(0,0,0,0.1)',
},
absolute: {
position: 'absolute',
top: 0,
left: 0,
bottom: 0,
right: 0,
},
});