-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathindex.tsx
More file actions
156 lines (139 loc) · 4.6 KB
/
index.tsx
File metadata and controls
156 lines (139 loc) · 4.6 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
import React, { useRef, useState } from 'react';
import {
Dimensions,
NativeScrollEvent,
NativeSyntheticEvent,
StyleSheet,
View,
} from 'react-native';
import {
Gesture,
GestureDetector,
PanGestureHandlerEventPayload,
} from 'react-native-gesture-handler';
import Animated, {
runOnJS,
useAnimatedStyle,
useSharedValue,
withSpring,
} from 'react-native-reanimated';
import { LoremIpsum } from '../../../src/common';
const HEADER_HEIGTH = 50;
const windowHeight = Dimensions.get('window').height;
const SNAP_POINTS_FROM_TOP = [50, windowHeight * 0.4, windowHeight * 0.8];
const FULLY_OPEN_SNAP_POINT = SNAP_POINTS_FROM_TOP[0];
const CLOSED_SNAP_POINT = SNAP_POINTS_FROM_TOP[SNAP_POINTS_FROM_TOP.length - 1];
function Example() {
const panGestureRef = useRef(Gesture.Pan());
const blockScrollUntilAtTheTopRef = useRef(Gesture.Tap());
const [snapPoint, setSnapPoint] = useState(CLOSED_SNAP_POINT);
const translationY = useSharedValue(0);
const scrollOffset = useSharedValue(0);
const bottomSheetTranslateY = useSharedValue(CLOSED_SNAP_POINT);
const onHandlerEndOnJS = (point: number) => {
setSnapPoint(point);
};
const onHandlerEnd = ({ velocityY }: PanGestureHandlerEventPayload) => {
'worklet';
const dragToss = 0.05;
const endOffsetY =
bottomSheetTranslateY.value + translationY.value + velocityY * dragToss;
// calculate nearest snap point
let destSnapPoint = FULLY_OPEN_SNAP_POINT;
if (
snapPoint === FULLY_OPEN_SNAP_POINT &&
endOffsetY < FULLY_OPEN_SNAP_POINT
) {
return;
}
for (const snapPoint of SNAP_POINTS_FROM_TOP) {
const distFromSnap = Math.abs(snapPoint - endOffsetY);
if (distFromSnap < Math.abs(destSnapPoint - endOffsetY)) {
destSnapPoint = snapPoint;
}
}
// update current translation to be able to animate withSpring to snapPoint
bottomSheetTranslateY.value =
bottomSheetTranslateY.value + translationY.value;
translationY.value = 0;
bottomSheetTranslateY.value = withSpring(destSnapPoint, {
mass: 0.5,
});
runOnJS(onHandlerEndOnJS)(destSnapPoint);
};
const panGesture = Gesture.Pan()
.onUpdate((e) => {
// when bottom sheet is not fully opened scroll offset should not influence
// its position (prevents random snapping when opening bottom sheet when
// the content is already scrolled)
if (snapPoint === FULLY_OPEN_SNAP_POINT) {
translationY.value = e.translationY - scrollOffset.value;
} else {
translationY.value = e.translationY;
}
})
.onEnd(onHandlerEnd)
.withRef(panGestureRef);
const blockScrollUntilAtTheTop = Gesture.Tap()
.maxDeltaY(snapPoint - FULLY_OPEN_SNAP_POINT)
.maxDuration(100000)
.simultaneousWithExternalGesture(panGesture)
.withRef(blockScrollUntilAtTheTopRef);
const headerGesture = Gesture.Pan()
.onUpdate((e) => {
translationY.value = e.translationY;
})
.onEnd(onHandlerEnd);
const scrollViewGesture = Gesture.Native().requireExternalGestureToFail(
blockScrollUntilAtTheTop
);
const bottomSheetAnimatedStyle = useAnimatedStyle(() => {
const translateY = bottomSheetTranslateY.value + translationY.value;
const minTranslateY = Math.max(FULLY_OPEN_SNAP_POINT, translateY);
const clampedTranslateY = Math.min(CLOSED_SNAP_POINT, minTranslateY);
return {
transform: [{ translateY: clampedTranslateY }],
};
});
return (
<View style={styles.container}>
<LoremIpsum words={200} />
<GestureDetector gesture={blockScrollUntilAtTheTop}>
<Animated.View style={[styles.bottomSheet, bottomSheetAnimatedStyle]}>
<GestureDetector gesture={headerGesture}>
<View style={styles.header} />
</GestureDetector>
<GestureDetector
gesture={Gesture.Simultaneous(panGesture, scrollViewGesture)}>
<Animated.ScrollView
bounces={false}
scrollEventThrottle={1}
onScrollBeginDrag={(
e: NativeSyntheticEvent<NativeScrollEvent>
) => {
scrollOffset.value = e.nativeEvent.contentOffset.y;
}}>
<LoremIpsum />
<LoremIpsum />
<LoremIpsum />
</Animated.ScrollView>
</GestureDetector>
</Animated.View>
</GestureDetector>
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
},
header: {
height: HEADER_HEIGTH,
backgroundColor: 'coral',
},
bottomSheet: {
...StyleSheet.absoluteFillObject,
backgroundColor: '#ff9f7A',
},
});
export default Example;