forked from software-mansion/react-native-gesture-handler
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
174 lines (162 loc) · 5.4 KB
/
index.js
File metadata and controls
174 lines (162 loc) · 5.4 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
import React, { Component } from 'react';
import { Animated, StyleSheet, Text, View } from 'react-native';
import {
PanGestureHandler,
NativeViewGestureHandler,
State,
TapGestureHandler,
} from 'react-native-gesture-handler';
import { LoremIpsum } from '../common';
import { USE_NATIVE_DRIVER } from '../config';
const HEADER_HEIGHT = 50;
const SNAP_POINTS_FROM_TOP = [50, 300, 550];
export class BottomSheet extends Component {
masterdrawer = React.createRef();
drawer = React.createRef();
drawerheader = React.createRef();
scroll = React.createRef();
constructor(props) {
super(props);
const START = SNAP_POINTS_FROM_TOP[0];
const END = SNAP_POINTS_FROM_TOP[SNAP_POINTS_FROM_TOP.length - 1];
this.state = {
lastSnap: END,
};
this._lastScrollYValue = 0;
this._lastScrollY = new Animated.Value(0);
this._onRegisterLastScroll = Animated.event(
[{ nativeEvent: { contentOffset: { y: this._lastScrollY } } }],
{ useNativeDriver: USE_NATIVE_DRIVER }
);
this._lastScrollY.addListener(({ value }) => {
this._lastScrollYValue = value;
});
this._dragY = new Animated.Value(0);
this._onGestureEvent = Animated.event(
[{ nativeEvent: { translationY: this._dragY } }],
{ useNativeDriver: USE_NATIVE_DRIVER }
);
this._reverseLastScrollY = Animated.multiply(
new Animated.Value(-1),
this._lastScrollY
);
this._translateYOffset = new Animated.Value(END);
this._translateY = Animated.add(
this._translateYOffset,
Animated.add(this._dragY, this._reverseLastScrollY)
).interpolate({
inputRange: [START, END],
outputRange: [START, END],
extrapolate: 'clamp',
});
}
_onHeaderHandlerStateChange = ({ nativeEvent }) => {
if (nativeEvent.oldState === State.BEGAN) {
this._lastScrollY.setValue(0);
}
this._onHandlerStateChange({ nativeEvent });
};
_onHandlerStateChange = ({ nativeEvent }) => {
if (nativeEvent.oldState === State.ACTIVE) {
let { velocityY, translationY } = nativeEvent;
translationY -= this._lastScrollYValue;
const dragToss = 0.05;
const endOffsetY =
this.state.lastSnap + translationY + dragToss * velocityY;
let destSnapPoint = SNAP_POINTS_FROM_TOP[0];
for (let i = 0; i < SNAP_POINTS_FROM_TOP.length; i++) {
const snapPoint = SNAP_POINTS_FROM_TOP[i];
const distFromSnap = Math.abs(snapPoint - endOffsetY);
if (distFromSnap < Math.abs(destSnapPoint - endOffsetY)) {
destSnapPoint = snapPoint;
}
}
this.setState({ lastSnap: destSnapPoint });
this._translateYOffset.extractOffset();
this._translateYOffset.setValue(translationY);
this._translateYOffset.flattenOffset();
this._dragY.setValue(0);
Animated.spring(this._translateYOffset, {
velocity: velocityY,
tension: 68,
friction: 12,
toValue: destSnapPoint,
useNativeDriver: USE_NATIVE_DRIVER,
}).start();
}
};
render() {
return (
<TapGestureHandler
maxDurationMs={100000}
ref={this.masterdrawer}
maxDeltaY={this.state.lastSnap - SNAP_POINTS_FROM_TOP[0]}
maxDist={
this.state.lastSnap - SNAP_POINTS_FROM_TOP[0] === 0 ? -1 : 10000
}>
<View style={StyleSheet.absoluteFillObject} pointerEvents="box-none">
<Animated.View
style={[
StyleSheet.absoluteFillObject,
{
transform: [{ translateY: this._translateY }],
},
]}>
<PanGestureHandler
ref={this.drawerheader}
simultaneousHandlers={[this.scroll, this.masterdrawer]}
shouldCancelWhenOutside={false}
onGestureEvent={this._onGestureEvent}
onHandlerStateChange={this._onHeaderHandlerStateChange}>
<Animated.View style={styles.header} />
</PanGestureHandler>
<PanGestureHandler
ref={this.drawer}
simultaneousHandlers={[this.scroll, this.masterdrawer]}
shouldCancelWhenOutside={false}
onGestureEvent={this._onGestureEvent}
onHandlerStateChange={this._onHandlerStateChange}>
<Animated.View style={styles.container}>
<NativeViewGestureHandler
ref={this.scroll}
waitFor={this.masterdrawer}
simultaneousHandlers={this.drawer}>
<Animated.ScrollView
style={[
styles.scrollView,
{ marginBottom: SNAP_POINTS_FROM_TOP[0] },
]}
bounces={false}
onScrollBeginDrag={this._onRegisterLastScroll}
scrollEventThrottle={1}>
<LoremIpsum />
<LoremIpsum />
<LoremIpsum />
</Animated.ScrollView>
</NativeViewGestureHandler>
</Animated.View>
</PanGestureHandler>
</Animated.View>
</View>
</TapGestureHandler>
);
}
}
export default class Example extends Component {
render() {
return (
<View style={styles.container}>
<BottomSheet />
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
},
header: {
height: HEADER_HEIGHT,
backgroundColor: 'red',
},
});