@@ -30,7 +30,7 @@ class SwipeableListItem extends PureComponent {
30
30
31
31
this . startTime = null ;
32
32
33
- this . prevSwipeDistancePercent = 0 ;
33
+ this . previousSwipeDistancePercent = 0 ;
34
34
35
35
this . resetState ( ) ;
36
36
}
@@ -39,7 +39,7 @@ class SwipeableListItem extends PureComponent {
39
39
this . dragStartPoint = { x : - 1 , y : - 1 } ;
40
40
this . dragDirection = DragDirection . UNKNOWN ;
41
41
this . left = 0 ;
42
- this . prevSwipeDistancePercent = 0 ;
42
+ this . previousSwipeDistancePercent = 0 ;
43
43
} ;
44
44
45
45
get dragHorizontalDirectionThreshold ( ) {
@@ -62,7 +62,11 @@ class SwipeableListItem extends PureComponent {
62
62
}
63
63
64
64
componentWillUnmount ( ) {
65
- cancelAnimationFrame ( this . requestedAnimationFrame ) ;
65
+ if ( this . requestedAnimationFrame ) {
66
+ cancelAnimationFrame ( this . requestedAnimationFrame ) ;
67
+
68
+ this . requestedAnimationFrame = null ;
69
+ }
66
70
67
71
this . wrapper . removeEventListener ( 'mousedown' , this . handleDragStartMouse ) ;
68
72
@@ -96,16 +100,16 @@ class SwipeableListItem extends PureComponent {
96
100
this . dragStartPoint = { x : clientX , y : clientY } ;
97
101
98
102
this . listElement . className = styles . content ;
99
- if ( this . contentLeft ) {
103
+ if ( this . contentLeft !== null ) {
100
104
this . contentLeft . className = styles . contentLeft ;
101
105
}
102
106
103
- if ( this . contentRight ) {
107
+ if ( this . contentRight !== null ) {
104
108
this . contentRight . className = styles . contentRight ;
105
109
}
106
110
107
111
this . startTime = Date . now ( ) ;
108
- this . requestedAnimationFrame = requestAnimationFrame ( this . updatePosition ) ;
112
+ this . scheduleUpdatePosition ( ) ;
109
113
} ;
110
114
111
115
handleMouseMove = event => {
@@ -119,10 +123,7 @@ class SwipeableListItem extends PureComponent {
119
123
event . preventDefault ( ) ;
120
124
121
125
this . left = clientX - this . dragStartPoint . x ;
122
-
123
- this . requestedAnimationFrame = requestAnimationFrame (
124
- this . updatePosition
125
- ) ;
126
+ this . scheduleUpdatePosition ( ) ;
126
127
}
127
128
}
128
129
} ;
@@ -142,10 +143,7 @@ class SwipeableListItem extends PureComponent {
142
143
event . preventDefault ( ) ;
143
144
144
145
this . left = clientX - this . dragStartPoint . x ;
145
-
146
- this . requestedAnimationFrame = requestAnimationFrame (
147
- this . updatePosition
148
- ) ;
146
+ this . scheduleUpdatePosition ( ) ;
149
147
}
150
148
}
151
149
} ;
@@ -191,12 +189,12 @@ class SwipeableListItem extends PureComponent {
191
189
}
192
190
193
191
// hide backgrounds
194
- if ( this . contentLeft ) {
192
+ if ( this . contentLeft !== null ) {
195
193
this . contentLeft . style . opacity = 0 ;
196
194
this . contentLeft . className = styles . contentLeftReturn ;
197
195
}
198
196
199
- if ( this . contentRight ) {
197
+ if ( this . contentRight !== null ) {
200
198
this . contentRight . style . opacity = 0 ;
201
199
this . contentRight . className = styles . contentRightReturn ;
202
200
}
@@ -227,7 +225,7 @@ class SwipeableListItem extends PureComponent {
227
225
switch ( octant ) {
228
226
case 0 :
229
227
if (
230
- this . contentRight &&
228
+ this . contentRight !== null &&
231
229
horizontalDistance > this . dragHorizontalDirectionThreshold
232
230
) {
233
231
this . dragDirection = DragDirection . RIGHT ;
@@ -242,7 +240,7 @@ class SwipeableListItem extends PureComponent {
242
240
break ;
243
241
case 4 :
244
242
if (
245
- this . contentLeft &&
243
+ this . contentLeft !== null &&
246
244
horizontalDistance > this . dragHorizontalDirectionThreshold
247
245
) {
248
246
this . dragDirection = DragDirection . LEFT ;
@@ -265,12 +263,23 @@ class SwipeableListItem extends PureComponent {
265
263
266
264
isSwiping = ( ) => {
267
265
const { blockSwipe } = this . props ;
268
- return (
269
- ! blockSwipe &&
270
- this . dragStartedWithinItem ( ) &&
271
- ( this . dragDirection === DragDirection . LEFT ||
272
- this . dragDirection === DragDirection . RIGHT )
273
- ) ;
266
+ const horizontalDrag =
267
+ this . dragDirection === DragDirection . LEFT ||
268
+ this . dragDirection === DragDirection . RIGHT ;
269
+
270
+ return ! blockSwipe && this . dragStartedWithinItem ( ) && horizontalDrag ;
271
+ } ;
272
+
273
+ scheduleUpdatePosition = ( ) => {
274
+ if ( this . requestedAnimationFrame ) {
275
+ return ;
276
+ }
277
+
278
+ this . requestedAnimationFrame = requestAnimationFrame ( ( ) => {
279
+ this . requestedAnimationFrame = null ;
280
+
281
+ this . updatePosition ( ) ;
282
+ } ) ;
274
283
} ;
275
284
276
285
updatePosition = ( ) => {
@@ -288,19 +297,23 @@ class SwipeableListItem extends PureComponent {
288
297
289
298
const opacity = ( Math . abs ( this . left ) / 100 ) . toFixed ( 2 ) ;
290
299
291
- if ( this . props . onSwipeProgress ) {
292
- const swipeDistance = Math . max (
293
- 0 ,
294
- this . listElement . offsetWidth - Math . abs ( this . left )
295
- ) ;
300
+ if ( this . props . onSwipeProgress && this . listElement !== null ) {
301
+ const listElementWidth = this . listElement . offsetWidth ;
302
+ let swipeDistancePercent = this . previousSwipeDistancePercent ;
303
+
304
+ if ( listElementWidth !== 0 ) {
305
+ const swipeDistance = Math . max (
306
+ 0 ,
307
+ listElementWidth - Math . abs ( this . left )
308
+ ) ;
296
309
297
- const swipeDistancePercent =
298
- 100 -
299
- Math . round ( ( 100 * swipeDistance ) / this . listElement . offsetWidth ) ;
310
+ swipeDistancePercent =
311
+ 100 - Math . round ( ( 100 * swipeDistance ) / listElementWidth ) ;
312
+ }
300
313
301
- if ( this . prevSwipeDistancePercent !== swipeDistancePercent ) {
314
+ if ( this . previousSwipeDistancePercent !== swipeDistancePercent ) {
302
315
this . props . onSwipeProgress ( swipeDistancePercent ) ;
303
- this . prevSwipeDistancePercent = swipeDistancePercent ;
316
+ this . previousSwipeDistancePercent = swipeDistancePercent ;
304
317
}
305
318
}
306
319
0 commit comments