Skip to content

Commit cd84871

Browse files
author
Marek Rozmus
committed
Merge branch 'fix_for_issue_40'
2 parents d0a74da + 74197b3 commit cd84871

10 files changed

+460
-139
lines changed

.all-contributorsrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
},
1919
{
2020
"login": "marekrozmus",
21-
"name": "marekrozmus",
21+
"name": "Marek Rozmus",
2222
"avatar_url": "https://avatars3.githubusercontent.com/u/26272040?v=4",
2323
"profile": "https://github.com/marekrozmus",
2424
"contributions": [

README.md

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -63,25 +63,37 @@ NOTE: `SwipeableListItem` can be used without `SwipeableList` but swipe blocking
6363

6464
## SwipeableList Props
6565

66-
### scrollElement
66+
### scrollStartThreshold
6767

68-
Type: `EventTarget`
68+
Type: `number` (default: `10`)
6969

70-
Required to block swipe during scroll outside of the `SwipeableList` e.g. set to `window.document` to block swipes during body scroll.
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.
71+
72+
It can be set for the whole list or for every item. See `scrollStartThreshold` for `SwipeableListItem`. Value from the `SwipeableListItem` takes precedence.
73+
74+
### swipeStartThreshold
75+
76+
Type: `number` (default: `10`)
77+
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.
79+
80+
It can be set for the whole list or for every item. See `swipeStartThreshold` for `SwipeableListItem`. Value from the `SwipeableListItem` takes precedence.
7181

7282
### threshold
7383

74-
Type: `number`
84+
Type: `number` (default: `0.5`)
7585

7686
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.
7787

88+
It can be set for the whole list or for every item. See `threshold` for `SwipeableListItem`. Value from the `SwipeableListItem` takes precedence.
89+
7890
## SwipeableListItem Props
7991

8092
### blockSwipe
8193

8294
Type: `boolean` (default: `false`)
8395

84-
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.
8597

8698
### swipeLeft
8799

@@ -102,11 +114,25 @@ Type: `Object`
102114

103115
Same as `swipeLeft` but to right. :wink:
104116

117+
### scrollStartThreshold
118+
119+
Type: `number` (default: `10`)
120+
121+
It can be set for the whole list or for every item. See `scrollStartThreshold` for `SwipeableList`. Value from the `SwipeableListItem` takes precedence.
122+
123+
### swipeStartThreshold
124+
125+
Type: `number` (default: `10`)
126+
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.
128+
129+
It can be set for the whole list or for every item. See `swipeStartThreshold` for `SwipeableList`. Value from the `SwipeableListItem` takes precedence.
130+
105131
### threshold
106132

107133
Type: `number` (default: `0.5`)
108134

109-
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.
110136

111137
## Contributors ✨
112138

@@ -117,7 +143,7 @@ Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/d
117143
<table>
118144
<tr>
119145
<td align="center"><a href="https://github.com/sandstreamdevelopment"><img src="https://avatars2.githubusercontent.com/u/44231396?v=4" width="100px;" alt="sandstreamdevelopment"/><br /><sub><b>sandstreamdevelopment</b></sub></a><br /><a href="#business-sandstreamdevelopment" title="Business development">💼</a> <a href="#financial-sandstreamdevelopment" title="Financial">💵</a> <a href="#ideas-sandstreamdevelopment" title="Ideas, Planning, & Feedback">🤔</a></td>
120-
<td align="center"><a href="https://github.com/marekrozmus"><img src="https://avatars3.githubusercontent.com/u/26272040?v=4" width="100px;" alt="marekrozmus"/><br /><sub><b>marekrozmus</b></sub></a><br /><a href="https://github.com/sandstreamdev/react-swipeable-list/commits?author=marekrozmus" title="Code">💻</a> <a href="https://github.com/sandstreamdev/react-swipeable-list/commits?author=marekrozmus" title="Documentation">📖</a> <a href="https://github.com/sandstreamdev/react-swipeable-list/commits?author=marekrozmus" title="Tests">⚠️</a> <a href="#example-marekrozmus" title="Examples">💡</a> <a href="#ideas-marekrozmus" title="Ideas, Planning, & Feedback">🤔</a></td>
146+
<td align="center"><a href="https://github.com/marekrozmus"><img src="https://avatars3.githubusercontent.com/u/26272040?v=4" width="100px;" alt="Marek Rozmus"/><br /><sub><b>Marek Rozmus</b></sub></a><br /><a href="https://github.com/sandstreamdev/react-swipeable-list/commits?author=marekrozmus" title="Code">💻</a> <a href="https://github.com/sandstreamdev/react-swipeable-list/commits?author=marekrozmus" title="Documentation">📖</a> <a href="https://github.com/sandstreamdev/react-swipeable-list/commits?author=marekrozmus" title="Tests">⚠️</a> <a href="#example-marekrozmus" title="Examples">💡</a> <a href="#ideas-marekrozmus" title="Ideas, Planning, & Feedback">🤔</a></td>
121147
<td align="center"><a href="https://github.com/jakubbogacz"><img src="https://avatars3.githubusercontent.com/u/26272028?v=4" width="100px;" alt="jakubbogacz"/><br /><sub><b>jakubbogacz</b></sub></a><br /><a href="#review-jakubbogacz" title="Reviewed Pull Requests">👀</a> <a href="#ideas-jakubbogacz" title="Ideas, Planning, & Feedback">🤔</a> <a href="https://github.com/sandstreamdev/react-swipeable-list/commits?author=jakubbogacz" title="Documentation">📖</a></td>
122148
<td align="center"><a href="https://github.com/LukasMarx"><img src="https://avatars0.githubusercontent.com/u/8080408?v=4" width="100px;" alt="Lukas Marx"/><br /><sub><b>Lukas Marx</b></sub></a><br /><a href="#ideas-LukasMarx" title="Ideas, Planning, & Feedback">🤔</a></td>
123149
<td align="center"><a href="https://www.linkedin.com/in/luis-filipe42/"><img src="https://avatars1.githubusercontent.com/u/29440533?v=4" width="100px;" alt="Luis Filipe Pedroso"/><br /><sub><b>Luis Filipe Pedroso</b></sub></a><br /><a href="https://github.com/sandstreamdev/react-swipeable-list/issues?q=author%3ALuisFilipePedroso" title="Bug reports">🐛</a></td>

babel.config.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ const presets = [
2121
const plugins = ['@babel/plugin-proposal-class-properties'];
2222

2323
module.exports = function(api) {
24-
api.cache.invalidate(() => process.env.NODE_ENV);
2524
const isTest = api.env('test');
2625

2726
if (isTest) {

examples/.prettierrc

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
{
2-
"tabWidth": 2,
32
"singleQuote": true
43
}

examples/src/App.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ function App() {
9696
<h3>
9797
More complex items and scroll (with 0.25 action trigger threshold)
9898
</h3>
99+
<h3>List in smaller container</h3>
99100
<span className={styles.actionInfo}>{triggeredComplexItemAction}</span>
100101
<div className={styles.complexListContainer}>
101102
<SwipeableList threshold={0.25}>
@@ -141,6 +142,53 @@ function App() {
141142
</SwipeableListItem>
142143
</SwipeableList>
143144
</div>
145+
<h3>List in size to content container</h3>
146+
<span className={styles.actionInfo}>{triggeredComplexItemAction}</span>
147+
<div className={styles.listContainer}>
148+
<SwipeableList threshold={0.25}>
149+
<SwipeableListItem
150+
swipeLeft={swipeLeftDataComplex('First')}
151+
swipeRight={swipeRightDataComplex('First')}
152+
>
153+
<ListItem
154+
icon={<MailIcon />}
155+
name="first"
156+
description="first description"
157+
/>
158+
</SwipeableListItem>
159+
<SwipeableListItem
160+
swipeLeft={swipeLeftDataComplex('Second')}
161+
swipeRight={swipeRightDataComplex('Second')}
162+
>
163+
<ListItem
164+
icon={<MailIcon />}
165+
name="second"
166+
description="second description"
167+
/>
168+
</SwipeableListItem>
169+
<SwipeableListItem
170+
swipeLeft={swipeLeftDataComplex('Second')}
171+
swipeRight={swipeRightDataComplex('Second')}
172+
>
173+
<ListItem
174+
icon={<MailIcon />}
175+
name="third"
176+
description="third description"
177+
/>
178+
</SwipeableListItem>
179+
<SwipeableListItem
180+
swipeLeft={swipeLeftDataComplex('Second')}
181+
swipeRight={swipeRightDataComplex('Second')}
182+
>
183+
<ListItem
184+
icon={<MailIcon />}
185+
name="fourth"
186+
description="fourth description"
187+
/>
188+
</SwipeableListItem>
189+
</SwipeableList>
190+
</div>
191+
<div>THE END</div>
144192
</div>
145193
);
146194
}

examples/src/app.module.css

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ h5 {
2323
flex-direction: column;
2424
align-items: center;
2525
height: 100%;
26+
min-height: 100%;
27+
overflow-y: auto;
2628
}
2729

2830
.listContainer {
@@ -65,6 +67,7 @@ h5 {
6567
.complexListContainer {
6668
width: 100%;
6769
height: 180px;
70+
min-height: 180px;
6871
border-top: 1px solid gray;
6972
border-bottom: 1px solid gray;
7073
}

src/SwipeableList.js

Lines changed: 19 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,29 @@
1-
import React, { useEffect, useState } from 'react';
1+
import React from 'react';
22
import PropTypes from 'prop-types';
33

44
import styles from './SwipeableList.css';
55

6-
const SwipeableList = ({ children, scrollElement, threshold }) => {
7-
const [blockSwipe, setBlockSwipe] = useState(false);
8-
9-
useEffect(() => {
10-
window.addEventListener('mouseup', handleDragEnd);
11-
window.addEventListener('touchend', handleDragEnd);
12-
13-
return () => {
14-
window.removeEventListener('mouseup', handleDragEnd);
15-
window.removeEventListener('touchend', handleDragEnd);
16-
};
17-
}, []);
18-
19-
useEffect(() => {
20-
if (scrollElement) {
21-
scrollElement.addEventListener('scroll', handleScroll);
22-
}
23-
24-
return () => {
25-
if (scrollElement) {
26-
scrollElement.removeEventListener('scroll', handleScroll);
27-
}
28-
};
29-
}, [scrollElement]);
30-
31-
const handleDragStart = () => setBlockSwipe(false);
32-
33-
const handleDragEnd = () => setBlockSwipe(false);
34-
35-
const handleScroll = () => setBlockSwipe(true);
36-
37-
return (
38-
<div
39-
className={styles.swipeableList}
40-
onMouseDown={handleDragStart}
41-
onTouchStart={handleDragStart}
42-
onScroll={handleScroll}
43-
data-testid="list-wrapper"
44-
>
45-
{React.Children.map(children, child =>
46-
React.cloneElement(child, { blockSwipe, threshold })
47-
)}
48-
</div>
49-
);
50-
};
6+
const SwipeableList = ({
7+
children,
8+
scrollStartThreshold,
9+
swipeStartThreshold,
10+
threshold
11+
}) => (
12+
<div className={styles.swipeableList} data-testid="list-wrapper">
13+
{React.Children.map(children, child =>
14+
React.cloneElement(child, {
15+
scrollStartThreshold,
16+
swipeStartThreshold,
17+
threshold
18+
})
19+
)}
20+
</div>
21+
);
5122

5223
SwipeableList.propTypes = {
5324
children: PropTypes.node,
54-
scrollElement:
55-
typeof EventTarget !== 'undefined'
56-
? PropTypes.instanceOf(EventTarget)
57-
: PropTypes.any,
25+
scrollStartThreshold: PropTypes.number,
26+
swipeStartThreshold: PropTypes.number,
5827
threshold: PropTypes.number
5928
};
6029

0 commit comments

Comments
 (0)