File tree Expand file tree Collapse file tree 3 files changed +58
-1
lines changed Expand file tree Collapse file tree 3 files changed +58
-1
lines changed Original file line number Diff line number Diff line change @@ -63,6 +63,12 @@ NOTE: `SwipeableListItem` can be used without `SwipeableList` but swipe blocking
63
63
64
64
## SwipeableList Props
65
65
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
+
66
72
### threshold
67
73
68
74
Type: ` number `
Original file line number Diff line number Diff line change @@ -3,7 +3,7 @@ import PropTypes from 'prop-types';
3
3
4
4
import styles from './SwipeableList.css' ;
5
5
6
- const SwipeableList = ( { children, threshold } ) => {
6
+ const SwipeableList = ( { children, scrollElement , threshold } ) => {
7
7
const [ blockSwipe , setBlockSwipe ] = useState ( false ) ;
8
8
9
9
useEffect ( ( ) => {
@@ -16,6 +16,18 @@ const SwipeableList = ({ children, threshold }) => {
16
16
} ;
17
17
} , [ ] ) ;
18
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
+
19
31
const handleDragStart = ( ) => setBlockSwipe ( false ) ;
20
32
21
33
const handleDragEnd = ( ) => setBlockSwipe ( false ) ;
@@ -39,6 +51,10 @@ const SwipeableList = ({ children, threshold }) => {
39
51
40
52
SwipeableList . propTypes = {
41
53
children : PropTypes . node ,
54
+ scrollElement :
55
+ typeof EventTarget !== 'undefined'
56
+ ? PropTypes . instanceOf ( EventTarget )
57
+ : PropTypes . any ,
42
58
threshold : PropTypes . number
43
59
} ;
44
60
Original file line number Diff line number Diff line change @@ -66,3 +66,38 @@ test('blocking swipe on scroll', () => {
66
66
swipeLeftMouse ( listItem ) ;
67
67
expect ( callbackLeft ) . toHaveBeenCalledTimes ( 2 ) ;
68
68
} ) ;
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
+ } ) ;
You can’t perform that action at this time.
0 commit comments