@@ -30,7 +30,7 @@ class SwipeableListItem extends PureComponent {
3030
3131 this . startTime = null ;
3232
33- this . prevSwipeDistancePercent = 0 ;
33+ this . previousSwipeDistancePercent = 0 ;
3434
3535 this . resetState ( ) ;
3636 }
@@ -39,7 +39,7 @@ class SwipeableListItem extends PureComponent {
3939 this . dragStartPoint = { x : - 1 , y : - 1 } ;
4040 this . dragDirection = DragDirection . UNKNOWN ;
4141 this . left = 0 ;
42- this . prevSwipeDistancePercent = 0 ;
42+ this . previousSwipeDistancePercent = 0 ;
4343 } ;
4444
4545 get dragHorizontalDirectionThreshold ( ) {
@@ -62,7 +62,11 @@ class SwipeableListItem extends PureComponent {
6262 }
6363
6464 componentWillUnmount ( ) {
65- cancelAnimationFrame ( this . requestedAnimationFrame ) ;
65+ if ( this . requestedAnimationFrame ) {
66+ cancelAnimationFrame ( this . requestedAnimationFrame ) ;
67+
68+ this . requestedAnimationFrame = null ;
69+ }
6670
6771 this . wrapper . removeEventListener ( 'mousedown' , this . handleDragStartMouse ) ;
6872
@@ -96,16 +100,16 @@ class SwipeableListItem extends PureComponent {
96100 this . dragStartPoint = { x : clientX , y : clientY } ;
97101
98102 this . listElement . className = styles . content ;
99- if ( this . contentLeft ) {
103+ if ( this . contentLeft !== null ) {
100104 this . contentLeft . className = styles . contentLeft ;
101105 }
102106
103- if ( this . contentRight ) {
107+ if ( this . contentRight !== null ) {
104108 this . contentRight . className = styles . contentRight ;
105109 }
106110
107111 this . startTime = Date . now ( ) ;
108- this . requestedAnimationFrame = requestAnimationFrame ( this . updatePosition ) ;
112+ this . scheduleUpdatePosition ( ) ;
109113 } ;
110114
111115 handleMouseMove = event => {
@@ -119,10 +123,7 @@ class SwipeableListItem extends PureComponent {
119123 event . preventDefault ( ) ;
120124
121125 this . left = clientX - this . dragStartPoint . x ;
122-
123- this . requestedAnimationFrame = requestAnimationFrame (
124- this . updatePosition
125- ) ;
126+ this . scheduleUpdatePosition ( ) ;
126127 }
127128 }
128129 } ;
@@ -142,10 +143,7 @@ class SwipeableListItem extends PureComponent {
142143 event . preventDefault ( ) ;
143144
144145 this . left = clientX - this . dragStartPoint . x ;
145-
146- this . requestedAnimationFrame = requestAnimationFrame (
147- this . updatePosition
148- ) ;
146+ this . scheduleUpdatePosition ( ) ;
149147 }
150148 }
151149 } ;
@@ -191,12 +189,12 @@ class SwipeableListItem extends PureComponent {
191189 }
192190
193191 // hide backgrounds
194- if ( this . contentLeft ) {
192+ if ( this . contentLeft !== null ) {
195193 this . contentLeft . style . opacity = 0 ;
196194 this . contentLeft . className = styles . contentLeftReturn ;
197195 }
198196
199- if ( this . contentRight ) {
197+ if ( this . contentRight !== null ) {
200198 this . contentRight . style . opacity = 0 ;
201199 this . contentRight . className = styles . contentRightReturn ;
202200 }
@@ -227,7 +225,7 @@ class SwipeableListItem extends PureComponent {
227225 switch ( octant ) {
228226 case 0 :
229227 if (
230- this . contentRight &&
228+ this . contentRight !== null &&
231229 horizontalDistance > this . dragHorizontalDirectionThreshold
232230 ) {
233231 this . dragDirection = DragDirection . RIGHT ;
@@ -242,7 +240,7 @@ class SwipeableListItem extends PureComponent {
242240 break ;
243241 case 4 :
244242 if (
245- this . contentLeft &&
243+ this . contentLeft !== null &&
246244 horizontalDistance > this . dragHorizontalDirectionThreshold
247245 ) {
248246 this . dragDirection = DragDirection . LEFT ;
@@ -265,12 +263,23 @@ class SwipeableListItem extends PureComponent {
265263
266264 isSwiping = ( ) => {
267265 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+ } ) ;
274283 } ;
275284
276285 updatePosition = ( ) => {
@@ -288,19 +297,23 @@ class SwipeableListItem extends PureComponent {
288297
289298 const opacity = ( Math . abs ( this . left ) / 100 ) . toFixed ( 2 ) ;
290299
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+ ) ;
296309
297- const swipeDistancePercent =
298- 100 -
299- Math . round ( ( 100 * swipeDistance ) / this . listElement . offsetWidth ) ;
310+ swipeDistancePercent =
311+ 100 - Math . round ( ( 100 * swipeDistance ) / listElementWidth ) ;
312+ }
300313
301- if ( this . prevSwipeDistancePercent !== swipeDistancePercent ) {
314+ if ( this . previousSwipeDistancePercent !== swipeDistancePercent ) {
302315 this . props . onSwipeProgress ( swipeDistancePercent ) ;
303- this . prevSwipeDistancePercent = swipeDistancePercent ;
316+ this . previousSwipeDistancePercent = swipeDistancePercent ;
304317 }
305318 }
306319
0 commit comments