Skip to content

Commit eb69cd0

Browse files
author
Marek Rozmus
committed
Fix code review issues and code cleanup
1 parent 0de6373 commit eb69cd0

File tree

3 files changed

+34
-30
lines changed

3 files changed

+34
-30
lines changed

README.md

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -67,33 +67,33 @@ NOTE: `SwipeableListItem` can be used without `SwipeableList` but swipe blocking
6767

6868
Type: `number` (default: `10`)
6969

70-
How far (in pixels) scroll needs to be done to enable block list item swiping. When scrolling is started swiping cannot be done.
70+
How far in pixels scroll needs to be done to block swiping. After scrolling is started and goes beyond the threshold, swiping is blocked.
7171

72-
Can be set for whole list or for every item. See `scrollStartThreshold` for `SwipeableListItem`.
72+
It can be set for the whole list or for every item. See `scrollStartThreshold` for `SwipeableListItem`. Value from the `SwipeableListItem` takes precedence.
7373

7474
### swipeStartThreshold
7575

7676
Type: `number` (default: `10`)
7777

78-
How far (in pixels) swipe needs to be done to enable start swiping on list item. When swiping is started scrolling cannot be done.
78+
How far in pixels swipe needs to be done to start swiping on list item. After a swipe is started and goes beyond the threshold, scrolling is blocked.
7979

80-
Can be set for whole list or for every item. See `swipeStartThreshold` for `SwipeableListItem`.
80+
It can be set for the whole list or for every item. See `swipeStartThreshold` for `SwipeableListItem`. Value from the `SwipeableListItem` takes precedence.
8181

8282
### threshold
8383

8484
Type: `number` (default: `0.5`)
8585

8686
How far swipe needs to be done to trigger attached action. `0.5` means that item needs to be swiped to half of its width, `0.25` - one-quarter of width.
8787

88-
Can be set for whole list or for every item. See `threshold` for `SwipeableListItem`.
88+
It can be set for the whole list or for every item. See `threshold` for `SwipeableListItem`. Value from the `SwipeableListItem` takes precedence.
8989

9090
## SwipeableListItem Props
9191

9292
### blockSwipe
9393

9494
Type: `boolean` (default: `false`)
9595

96-
If set to `true` all defined swipe actions are blocked. This is done by `SwipeableList` during scroll to prevent mouse move events to cause accidental swiping acitions.
96+
If set to `true` all defined swipe actions are blocked.
9797

9898
### swipeLeft
9999

@@ -118,21 +118,21 @@ Same as `swipeLeft` but to right. :wink:
118118

119119
Type: `number` (default: `10`)
120120

121-
Can be set for whole list or for every item. See `scrollStartThreshold` for `SwipeableList`.
121+
It can be set for the whole list or for every item. See `scrollStartThreshold` for `SwipeableList`. Value from the `SwipeableListItem` takes precedence.
122122

123123
### swipeStartThreshold
124124

125125
Type: `number` (default: `10`)
126126

127-
How far swipe needs to be done to enable start swiping on list item. When swiping is started scrolling cannot be done.
127+
How far in pixels swipe needs to be done to start swiping on list item. After a swipe is started and goes beyond the threshold, scrolling is blocked.
128128

129-
Can be set for whole list or for every item. See `swipeStartThreshold` for `SwipeableList`.
129+
It can be set for the whole list or for every item. See `swipeStartThreshold` for `SwipeableList`. Value from the `SwipeableListItem` takes precedence.
130130

131131
### threshold
132132

133133
Type: `number` (default: `0.5`)
134134

135-
Can be set for whole list or for every item. See `threshold` for `SwipeableList`.
135+
It can be set for the whole list or for every item. See `threshold` for `SwipeableList`. Value from the `SwipeableListItem` takes precedence.
136136

137137
## Contributors ✨
138138

examples/src/app.module.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ h5 {
2424
align-items: center;
2525
height: 100%;
2626
min-height: 100%;
27-
overflow-y: scroll;
27+
overflow-y: auto;
2828
}
2929

3030
.listContainer {

src/SwipeableListItem.js

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,6 @@ class SwipeableListItem extends PureComponent {
2222
constructor(props) {
2323
super(props);
2424

25-
this.dragHorizontalDirectionThreshold = props.swipeStartThreshold || 10;
26-
this.dragVerticalDirectionThreshold = props.scrollStartThreshold || 10;
27-
2825
this.contentLeft = null;
2926
this.contentRight = null;
3027
this.listElement = null;
@@ -41,6 +38,14 @@ class SwipeableListItem extends PureComponent {
4138
this.left = 0;
4239
};
4340

41+
get dragHorizontalDirectionThreshold() {
42+
return this.props.swipeStartThreshold || 10;
43+
}
44+
45+
get dragVerticalDirectionThreshold() {
46+
return this.props.scrollStartThreshold || 10;
47+
}
48+
4449
componentDidMount() {
4550
this.wrapper.addEventListener('mousedown', this.handleDragStartMouse);
4651

@@ -220,13 +225,12 @@ class SwipeableListItem extends PureComponent {
220225
break;
221226
}
222227
}
223-
224-
return this.dragDirection;
225228
};
226229

227230
isSwiping = () =>
228-
this.dragDirection === DragDirection.LEFT ||
229-
this.dragDirection === DragDirection.RIGHT;
231+
this.dragStartedWithinItem() &&
232+
(this.dragDirection === DragDirection.LEFT ||
233+
this.dragDirection === DragDirection.RIGHT);
230234

231235
updatePosition = () => {
232236
const { blockSwipe } = this.props;
@@ -238,30 +242,30 @@ class SwipeableListItem extends PureComponent {
238242
const now = Date.now();
239243
const elapsed = now - this.startTime;
240244

241-
if (this.dragStartedWithinItem() && elapsed > FPS_INTERVAL) {
245+
if (elapsed > FPS_INTERVAL && this.isSwiping()) {
242246
let contentToShow = this.left < 0 ? this.contentLeft : this.contentRight;
243-
let contentToHide = this.left < 0 ? this.contentRight : this.contentLeft;
244247

245248
if (!contentToShow) {
246249
return;
247250
}
248251

252+
this.listElement.style.transform = `translateX(${this.left}px)`;
253+
249254
const opacity = (Math.abs(this.left) / 100).toFixed(2);
250255

251-
if (this.isSwiping()) {
252-
this.listElement.style.transform = `translateX(${this.left}px)`;
256+
if (opacity < 1 && opacity.toString() !== contentToShow.style.opacity) {
257+
contentToShow.style.opacity = opacity.toString();
253258

254-
if (opacity < 1 && opacity.toString() !== contentToShow.style.opacity) {
255-
contentToShow.style.opacity = opacity.toString();
259+
let contentToHide =
260+
this.left < 0 ? this.contentRight : this.contentLeft;
256261

257-
if (contentToHide) {
258-
contentToHide.style.opacity = '0';
259-
}
262+
if (contentToHide) {
263+
contentToHide.style.opacity = '0';
260264
}
265+
}
261266

262-
if (opacity >= 1) {
263-
contentToShow.style.opacity = '1';
264-
}
267+
if (opacity >= 1) {
268+
contentToShow.style.opacity = '1';
265269
}
266270

267271
this.startTime = Date.now();

0 commit comments

Comments
 (0)