forked from Sefaria/Sefaria-Mobile
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SwipeableRow.js
352 lines (311 loc) · 10.1 KB
/
SwipeableRow.js
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
'use strict';
import React from 'react';
import {
Animated,
I18nManager,
PanResponder,
View,
StyleSheet,
} from 'react-native';
const IS_RTL = I18nManager.isRTL;
// NOTE: Eventually convert these consts to an input object of configurations
// Position of the left of the swipable item when closed
const CLOSED_LEFT_POSITION = 0;
// Minimum swipe distance before we recognize it as such
const HORIZONTAL_SWIPE_DISTANCE_THRESHOLD = 10;
// Minimum swipe speed before we fully animate the user's action (open/close)
const HORIZONTAL_FULL_SWIPE_SPEED_THRESHOLD = 0.3;
// Factor to divide by to get slow speed; i.e. 4 means 1/4 of full speed
const SLOW_SPEED_SWIPE_FACTOR = 4;
// Time, in milliseconds, of how long the animated swipe should be
const SWIPE_DURATION = 300;
/**
* On SwipeableListView mount, the 1st item will bounce to show users it's
* possible to swipe
*/
const ON_MOUNT_BOUNCE_DELAY = 700;
const ON_MOUNT_BOUNCE_DURATION = 400;
// Distance left of closed position to bounce back when right-swiping from closed
const RIGHT_SWIPE_BOUNCE_BACK_DISTANCE = 30;
const RIGHT_SWIPE_BOUNCE_BACK_DURATION = 300;
/**
* Max distance of right swipe to allow (right swipes do functionally nothing).
* Must be multiplied by SLOW_SPEED_SWIPE_FACTOR because gestureState.dx tracks
* how far the finger swipes, and not the actual animation distance.
*/
const RIGHT_SWIPE_THRESHOLD = 30 * SLOW_SPEED_SWIPE_FACTOR;
const DEFAULT_SWIPE_THRESHOLD = 30;
const emptyFunction = () => {};
/**
* Creates a swipable row that allows taps on the main item and a custom View
* on the item hidden behind the row. Typically this should be used in
* conjunction with SwipeableListView for additional functionality, but can be
* used in a normal ListView. See the renderRow for SwipeableListView to see how
* to use this component separately.
*/
class SwipeableRow extends React.Component {
_handleMoveShouldSetPanResponderCapture = (
event,
gestureState,
) => {
// Decides whether a swipe is responded to by this component or its child
return gestureState.dy < 10 && this._isValidSwipe(gestureState);
};
_handlePanResponderGrant = (
event,
gestureState,
) => {};
_handlePanResponderMove = (
event,
gestureState,
) => {
if (this._isSwipingExcessivelyRightFromClosedPosition(gestureState)) {
return;
}
this.props.onSwipeStart && this.props.onSwipeStart();
if (this._isSwipingRightFromClosed(gestureState)) {
this._swipeSlowSpeed(gestureState);
} else {
this._swipeFullSpeed(gestureState);
}
};
_onPanResponderTerminationRequest = (
event,
gestureState,
) => {
return false;
};
_handlePanResponderEnd = (
event,
gestureState,
) => {
const horizontalDistance = IS_RTL ? -gestureState.dx : gestureState.dx;
if (this._isSwipingRightFromClosed(gestureState)) {
this.props.onOpen && this.props.onOpen();
this._animateBounceBack(RIGHT_SWIPE_BOUNCE_BACK_DURATION);
} else if (this._shouldAnimateRemainder(gestureState)) {
if (horizontalDistance < 0) {
// Swiped left
this.props.onOpen && this.props.onOpen();
this._animateToOpenPositionWith(gestureState.vx, horizontalDistance);
} else {
// Swiped right
this.props.onClose && this.props.onClose();
this._animateToClosedPosition();
}
} else {
if (this._previousLeft === CLOSED_LEFT_POSITION) {
this._animateToClosedPosition();
} else {
this._animateToOpenPosition();
}
}
this.props.onSwipeEnd && this.props.onSwipeEnd();
};
_panResponder = PanResponder.create({
onMoveShouldSetPanResponderCapture: this
._handleMoveShouldSetPanResponderCapture,
onPanResponderGrant: this._handlePanResponderGrant,
onPanResponderMove: this._handlePanResponderMove,
onPanResponderRelease: this._handlePanResponderEnd,
onPanResponderTerminationRequest: this._onPanResponderTerminationRequest,
onPanResponderTerminate: this._handlePanResponderEnd,
onShouldBlockNativeResponder: (event, gestureState) => false,
});
_previousLeft = CLOSED_LEFT_POSITION;
_timeoutID = null;
state = {
currentLeft: new Animated.Value(this._previousLeft),
/**
* In order to render component A beneath component B, A must be rendered
* before B. However, this will cause "flickering", aka we see A briefly
* then B. To counter this, _isSwipeableViewRendered flag is used to set
* component A to be transparent until component B is loaded.
*/
isSwipeableViewRendered: false,
rowHeight: null,
};
componentDidMount() {
if (this.props.shouldBounceOnMount) {
/**
* Do the on mount bounce after a delay because if we animate when other
* components are loading, the animation will be laggy
*/
this._timeoutID = setTimeout(() => {
this._animateBounceBack(ON_MOUNT_BOUNCE_DURATION);
}, ON_MOUNT_BOUNCE_DELAY);
}
}
UNSAFE_componentWillReceiveProps(nextProps) {
/**
* We do not need an "animateOpen(noCallback)" because this animation is
* handled internally by this component.
*/
const isOpen = this.props.isOpen ?? false;
const nextIsOpen = nextProps.isOpen ?? false;
if (isOpen && !nextIsOpen) {
this._animateToClosedPosition();
}
}
componentWillUnmount() {
if (this._timeoutID != null) {
clearTimeout(this._timeoutID);
}
}
render() {
// The view hidden behind the main view
let slideOutView;
if (this.state.isSwipeableViewRendered && this.state.rowHeight) {
slideOutView = (
<View
style={[styles.slideOutContainer, {height: this.state.rowHeight}]}>
{this.props.slideoutView}
</View>
);
}
// The swipeable item
const swipeableView = (
<Animated.View
onLayout={this._onSwipeableViewLayout}
style={{transform: [{translateX: this.state.currentLeft}]}}>
{this.props.children}
</Animated.View>
);
return (
<View {...this._panResponder.panHandlers}>
{slideOutView}
{swipeableView}
</View>
);
}
close() {
this.props.onClose && this.props.onClose();
this._animateToClosedPosition();
}
_onSwipeableViewLayout = (event) => {
this.setState({
isSwipeableViewRendered: true,
rowHeight: event.nativeEvent.layout.height,
});
};
_isSwipingRightFromClosed(gestureState) {
const gestureStateDx = IS_RTL ? -gestureState.dx : gestureState.dx;
return this._previousLeft === CLOSED_LEFT_POSITION && gestureStateDx > 0;
}
_swipeFullSpeed(gestureState) {
this.state.currentLeft.setValue(this._previousLeft + gestureState.dx);
}
_swipeSlowSpeed(gestureState) {
this.state.currentLeft.setValue(
this._previousLeft + gestureState.dx / SLOW_SPEED_SWIPE_FACTOR,
);
}
_isSwipingExcessivelyRightFromClosedPosition(
gestureState,
) {
/**
* We want to allow a BIT of right swipe, to allow users to know that
* swiping is available, but swiping right does not do anything
* functionally.
*/
const gestureStateDx = IS_RTL ? -gestureState.dx : gestureState.dx;
return (
this._isSwipingRightFromClosed(gestureState) &&
gestureStateDx > RIGHT_SWIPE_THRESHOLD
);
}
_animateTo(
toValue,
duration = SWIPE_DURATION,
callback = emptyFunction,
) {
Animated.timing(this.state.currentLeft, {
duration,
toValue,
useNativeDriver: true,
}).start(() => {
this._previousLeft = toValue;
callback();
});
}
_animateToOpenPosition() {
const maxSwipeDistance = this.props.maxSwipeDistance ?? 0;
const directionAwareMaxSwipeDistance = IS_RTL
? -maxSwipeDistance
: maxSwipeDistance;
this._animateTo(-directionAwareMaxSwipeDistance);
}
_animateToOpenPositionWith(speed, distMoved) {
/**
* Ensure the speed is at least the set speed threshold to prevent a slow
* swiping animation
*/
speed =
speed > HORIZONTAL_FULL_SWIPE_SPEED_THRESHOLD
? speed
: HORIZONTAL_FULL_SWIPE_SPEED_THRESHOLD;
const maxSwipeDistance = this.props.maxSwipeDistance ?? 0;
/**
* Calculate the duration the row should take to swipe the remaining distance
* at the same speed the user swiped (or the speed threshold)
*/
const duration = Math.abs((maxSwipeDistance - Math.abs(distMoved)) / speed);
const directionAwareMaxSwipeDistance = IS_RTL
? -maxSwipeDistance
: maxSwipeDistance;
this._animateTo(-directionAwareMaxSwipeDistance, duration);
}
_animateToClosedPosition(duration = SWIPE_DURATION) {
this._animateTo(CLOSED_LEFT_POSITION, duration);
}
_animateToClosedPositionDuringBounce = () => {
this._animateToClosedPosition(RIGHT_SWIPE_BOUNCE_BACK_DURATION);
};
_animateBounceBack(duration) {
/**
* When swiping right, we want to bounce back past closed position on release
* so users know they should swipe right to get content.
*/
const swipeBounceBackDistance = IS_RTL
? -RIGHT_SWIPE_BOUNCE_BACK_DISTANCE
: RIGHT_SWIPE_BOUNCE_BACK_DISTANCE;
this._animateTo(
-swipeBounceBackDistance,
duration,
this._animateToClosedPositionDuringBounce,
);
}
// Ignore swipes due to user's finger moving slightly when tapping
_isValidSwipe(gestureState) {
const preventSwipeRight = this.props.preventSwipeRight ?? false;
if (
preventSwipeRight &&
this._previousLeft === CLOSED_LEFT_POSITION &&
gestureState.dx > 0
) {
return false;
}
return Math.abs(gestureState.dx) > HORIZONTAL_SWIPE_DISTANCE_THRESHOLD;
}
_shouldAnimateRemainder(gestureState) {
/**
* If user has swiped past a certain distance, animate the rest of the way
* if they let go
*/
const swipeThreshold = this.props.swipeThreshold ?? DEFAULT_SWIPE_THRESHOLD;
return (
Math.abs(gestureState.dx) > swipeThreshold ||
gestureState.vx > HORIZONTAL_FULL_SWIPE_SPEED_THRESHOLD
);
}
}
const styles = StyleSheet.create({
slideOutContainer: {
bottom: 0,
left: 0,
position: 'absolute',
right: 0,
top: 0,
},
});
export default SwipeableRow;