Skip to content

Commit 21809ba

Browse files
author
Marek Rozmus
committed
Fix code review issues
1 parent 029adac commit 21809ba

File tree

3 files changed

+56
-42
lines changed

3 files changed

+56
-42
lines changed

README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ import '@sandstreamdev/react-swipeable-list/dist/styles.css';
5353
content: <div>Revealed content during swipe</div>,
5454
action: () => console.info('swipe action triggered')
5555
}}
56-
onSwipeProgress={progress => console.info(progress)}
56+
onSwipeProgress={progress => console.info(`Swipe progress: ${progress}%`)}
5757
>
5858
<div>Item name</div>
5959
</SwipeableListItem>
@@ -135,21 +135,21 @@ It can be set for the whole list or for every item. See `threshold` for `Swipeab
135135

136136
### onSwipeStart
137137

138-
Type: () => void
138+
Type: `() => void`
139139

140-
Fired after swipe has started.
140+
Fired after swipe has started (after drag gesture passes the `swipeStartThreshold` distance in pixels).
141141

142142
### onSwipeEnd
143143

144-
Type: () => void
144+
Type: `() => void`
145145

146146
Fired after swipe has ended.
147147

148148
### onSwipeProgress
149149

150-
Type: (progress: number) => void
150+
Type: `(progress: number) => void`
151151

152-
Fired every time swipe progress changes. Progress value is in range 0..100.
152+
Fired every time swipe progress changes. The reported `progress` value is always an integer in range 0 to 100 inclusive.
153153

154154
## Contributors ✨
155155

examples/src/App.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import styles from './app.module.css';
1313
function App() {
1414
const [triggeredSimpleItemAction, triggerSimpleItemAction] = useState('None');
1515
const [triggeredComplexItemAction, triggerComplexItemAction] = useState('');
16-
const [swipeProgress, handleSwipeProgress] = useState('0');
16+
const [swipeProgress, handleSwipeProgress] = useState();
1717
const [swipeAction, handleSwipeAction] = useState('None');
1818

1919
const swipeRightDataSimple = name => ({
@@ -73,6 +73,7 @@ function App() {
7373

7474
const handleSwipeEnd = () => {
7575
handleSwipeAction('Swipe ended');
76+
handleSwipeProgress();
7677
};
7778

7879
return (
@@ -87,7 +88,7 @@ function App() {
8788
Callback swipe action: {swipeAction}
8889
</span>
8990
<span className={styles.actionInfo}>
90-
Callback swipe progress: {swipeProgress}
91+
Callback swipe progress: {swipeProgress || '-'}%
9192
</span>
9293
<div className={styles.listContainer}>
9394
<SwipeableList>

src/SwipeableListItem.js

Lines changed: 47 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)