|
@@ -1,7 +1,7 @@
|
1
|
1
|
// @flow
|
2
|
2
|
|
3
|
3
|
import React, { PureComponent, type Node } from 'react';
|
4
|
|
-import { SafeAreaView, ScrollView, View } from 'react-native';
|
|
4
|
+import { PanResponder, SafeAreaView, ScrollView, View } from 'react-native';
|
5
|
5
|
|
6
|
6
|
import { ColorSchemeRegistry } from '../../../color-scheme';
|
7
|
7
|
import { SlidingView } from '../../../react';
|
|
@@ -10,6 +10,16 @@ import { StyleType } from '../../../styles';
|
10
|
10
|
|
11
|
11
|
import { bottomSheetStyles as styles } from './styles';
|
12
|
12
|
|
|
13
|
+/**
|
|
14
|
+ * Minimal distance that needs to be moved by the finger to consider it a swipe.
|
|
15
|
+ */
|
|
16
|
+const GESTURE_DISTANCE_THRESHOLD = 5;
|
|
17
|
+
|
|
18
|
+/**
|
|
19
|
+ * The minimal speed needed to be achieved by the finger to consider it as a swipe.
|
|
20
|
+ */
|
|
21
|
+const GESTURE_SPEED_THRESHOLD = 0.2;
|
|
22
|
+
|
13
|
23
|
/**
|
14
|
24
|
* The type of {@code BottomSheet}'s React {@code Component} prop types.
|
15
|
25
|
*/
|
|
@@ -31,6 +41,11 @@ type Props = {
|
31
|
41
|
*/
|
32
|
42
|
onCancel: ?Function,
|
33
|
43
|
|
|
44
|
+ /**
|
|
45
|
+ * Callback to be attached to the custom swipe event of the BottomSheet.
|
|
46
|
+ */
|
|
47
|
+ onSwipe?: Function,
|
|
48
|
+
|
34
|
49
|
/**
|
35
|
50
|
* Function to render a bottom sheet header element, if necessary.
|
36
|
51
|
*/
|
|
@@ -41,6 +56,23 @@ type Props = {
|
41
|
56
|
* A component emulating Android's BottomSheet.
|
42
|
57
|
*/
|
43
|
58
|
class BottomSheet extends PureComponent<Props> {
|
|
59
|
+ panResponder: Object;
|
|
60
|
+
|
|
61
|
+ /**
|
|
62
|
+ * Instantiates a new component.
|
|
63
|
+ *
|
|
64
|
+ * @inheritdoc
|
|
65
|
+ */
|
|
66
|
+ constructor(props: Props) {
|
|
67
|
+ super(props);
|
|
68
|
+
|
|
69
|
+ this.panResponder = PanResponder.create({
|
|
70
|
+ onStartShouldSetPanResponder: this._onShouldSetResponder.bind(this),
|
|
71
|
+ onMoveShouldSetPanResponder: this._onShouldSetResponder.bind(this),
|
|
72
|
+ onPanResponderRelease: this._onGestureEnd.bind(this)
|
|
73
|
+ });
|
|
74
|
+ }
|
|
75
|
+
|
44
|
76
|
/**
|
45
|
77
|
* Implements React's {@link Component#render()}.
|
46
|
78
|
*
|
|
@@ -66,7 +98,8 @@ class BottomSheet extends PureComponent<Props> {
|
66
|
98
|
style = { [
|
67
|
99
|
styles.sheetItemContainer,
|
68
|
100
|
_styles.sheet
|
69
|
|
- ] }>
|
|
101
|
+ ] }
|
|
102
|
+ { ...this.panResponder.panHandlers }>
|
70
|
103
|
<ScrollView
|
71
|
104
|
bounces = { false }
|
72
|
105
|
showsVerticalScrollIndicator = { false }
|
|
@@ -78,6 +111,48 @@ class BottomSheet extends PureComponent<Props> {
|
78
|
111
|
</SlidingView>
|
79
|
112
|
);
|
80
|
113
|
}
|
|
114
|
+
|
|
115
|
+ /**
|
|
116
|
+ * Callback to handle a gesture end event.
|
|
117
|
+ *
|
|
118
|
+ * @param {Object} evt - The native gesture event.
|
|
119
|
+ * @param {Object} gestureState - The gesture state.
|
|
120
|
+ * @returns {void}
|
|
121
|
+ */
|
|
122
|
+ _onGestureEnd(evt, gestureState) {
|
|
123
|
+ const verticalSwipe = Math.abs(gestureState.vy) > Math.abs(gestureState.vx)
|
|
124
|
+ && Math.abs(gestureState.vy) > GESTURE_SPEED_THRESHOLD;
|
|
125
|
+
|
|
126
|
+ if (verticalSwipe) {
|
|
127
|
+ const direction = gestureState.vy > 0 ? 'down' : 'up';
|
|
128
|
+ const { onCancel, onSwipe } = this.props;
|
|
129
|
+ let isSwipeHandled = false;
|
|
130
|
+
|
|
131
|
+ if (onSwipe) {
|
|
132
|
+ isSwipeHandled = onSwipe(direction);
|
|
133
|
+ }
|
|
134
|
+
|
|
135
|
+ if (direction === 'down' && !isSwipeHandled) {
|
|
136
|
+ // Swipe down is a special gesture that can be used to close the
|
|
137
|
+ // BottomSheet, so if the swipe is not handled by the parent
|
|
138
|
+ // component, we consider it as a request to close.
|
|
139
|
+ onCancel && onCancel();
|
|
140
|
+ }
|
|
141
|
+ }
|
|
142
|
+ }
|
|
143
|
+
|
|
144
|
+ /**
|
|
145
|
+ * Returns true if the pan responder should activate, false otherwise.
|
|
146
|
+ *
|
|
147
|
+ * @param {Object} evt - The native gesture event.
|
|
148
|
+ * @param {Object} gestureState - The gesture state.
|
|
149
|
+ * @returns {boolean}
|
|
150
|
+ */
|
|
151
|
+ _onShouldSetResponder({ nativeEvent }, gestureState) {
|
|
152
|
+ return nativeEvent.touches.length === 1
|
|
153
|
+ && Math.abs(gestureState.dx) > GESTURE_DISTANCE_THRESHOLD
|
|
154
|
+ && Math.abs(gestureState.dy) > GESTURE_DISTANCE_THRESHOLD;
|
|
155
|
+ }
|
81
|
156
|
}
|
82
|
157
|
|
83
|
158
|
/**
|