Skip to content

Commit 7f9192f

Browse files
author
Marek Rozmus
committed
Merge branch 'fix_for_issues_36'
2 parents 4b3c539 + e36038c commit 7f9192f

File tree

3 files changed

+58
-1
lines changed

3 files changed

+58
-1
lines changed

README.md

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

6464
## SwipeableList Props
6565

66+
### scrollElement
67+
68+
Type: `EventTarget`
69+
70+
Required to block swipe during scroll outside of the `SwipeableList` e.g. set to `window.document` to block swipes during body scroll.
71+
6672
### threshold
6773

6874
Type: `number`

src/SwipeableList.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import PropTypes from 'prop-types';
33

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

6-
const SwipeableList = ({ children, threshold }) => {
6+
const SwipeableList = ({ children, scrollElement, threshold }) => {
77
const [blockSwipe, setBlockSwipe] = useState(false);
88

99
useEffect(() => {
@@ -16,6 +16,18 @@ const SwipeableList = ({ children, threshold }) => {
1616
};
1717
}, []);
1818

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+
1931
const handleDragStart = () => setBlockSwipe(false);
2032

2133
const handleDragEnd = () => setBlockSwipe(false);
@@ -39,6 +51,10 @@ const SwipeableList = ({ children, threshold }) => {
3951

4052
SwipeableList.propTypes = {
4153
children: PropTypes.node,
54+
scrollElement:
55+
typeof EventTarget !== 'undefined'
56+
? PropTypes.instanceOf(EventTarget)
57+
: PropTypes.any,
4258
threshold: PropTypes.number
4359
};
4460

src/__tests__/SwipeableList.test.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,38 @@ test('blocking swipe on scroll', () => {
6666
swipeLeftMouse(listItem);
6767
expect(callbackLeft).toHaveBeenCalledTimes(2);
6868
});
69+
70+
test('blocking swipe on scrollElement scroll', () => {
71+
const callbackLeft = jest.fn();
72+
73+
const { queryAllByTestId } = render(
74+
<SwipeableList scrollElement={window.document}>
75+
<SwipeableListItem
76+
swipeLeft={{
77+
content: <span>Left swipe content</span>,
78+
action: callbackLeft
79+
}}
80+
>
81+
<span>Item content 1</span>
82+
</SwipeableListItem>
83+
</SwipeableList>
84+
);
85+
86+
const listItem = queryAllByTestId('content')[0];
87+
88+
// try to swipe - should work
89+
swipeLeftMouse(listItem);
90+
expect(callbackLeft).toHaveBeenCalledTimes(1);
91+
92+
fireEvent.scroll(window.document, { target: { scrollY: 100 } });
93+
94+
// block swipe should be on
95+
swipeLeftMouse(listItem);
96+
expect(callbackLeft).toHaveBeenCalledTimes(1);
97+
98+
fireEvent.mouseUp(window.document);
99+
100+
// swiping should be possible again
101+
swipeLeftMouse(listItem);
102+
expect(callbackLeft).toHaveBeenCalledTimes(2);
103+
});

0 commit comments

Comments
 (0)