Skip to content

Commit bfbf26a

Browse files
author
Marek Rozmus
committed
Move event registration and center example app content
1 parent a915814 commit bfbf26a

File tree

3 files changed

+34
-17
lines changed

3 files changed

+34
-17
lines changed

examples/src/app.css

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,17 @@ h5 {
1111
margin: 0;
1212
}
1313

14+
.testApp {
15+
display: flex;
16+
flex-direction: column;
17+
height: 100vh;
18+
}
19+
1420
.example {
1521
display: flex;
1622
flex-direction: column;
1723
align-items: center;
24+
height: 100%;
1825
}
1926

2027
.listContainer {

examples/src/index.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@ import styles from './app.css';
55
import App from './App';
66

77
ReactDOM.render(
8-
<div className={styles.smartphone}>
9-
<div className={styles.content}>
10-
<App />
8+
<div className={styles.testApp}>
9+
<div className={styles.smartphone}>
10+
<div className={styles.content}>
11+
<App />
12+
</div>
1113
</div>
1214
</div>,
1315
document.getElementById('root')

src/SwipeableListItem.js

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,38 +26,37 @@ class SwipeableListItem extends PureComponent {
2626
}
2727

2828
componentDidMount() {
29-
window.addEventListener('mouseup', this.handleDragEndMouse);
30-
window.addEventListener('touchend', this.handleDragEndTouch);
31-
window.addEventListener('mousemove', this.handleMouseMove);
32-
3329
this.wrapper.addEventListener('mousedown', this.handleDragStartMouse);
3430
this.wrapper.addEventListener('touchstart', this.handleDragStartTouch);
35-
this.wrapper.addEventListener('mouseup', this.handleDragEndMouse);
36-
this.wrapper.addEventListener('touchend', this.handleDragEndTouch);
3731
}
3832

3933
componentWillUnmount() {
40-
window.removeEventListener('mouseup', this.handleDragEndMouse);
41-
window.removeEventListener('touchend', this.handleDragEndTouch);
42-
window.removeEventListener('mousemove', this.handleMouseMove);
43-
4434
this.wrapper.removeEventListener('mousedown', this.handleDragStartMouse);
4535
this.wrapper.removeEventListener('touchstart', this.handleDragStartTouch);
46-
this.wrapper.removeEventListener('mouseup', this.handleDragEndMouse);
47-
this.wrapper.removeEventListener('touchend', this.handleDragEndTouch);
4836
}
4937

5038
handleDragStartMouse = event => {
5139
event.stopPropagation();
52-
this.handleDragStart(event);
40+
41+
window.addEventListener('mouseup', this.handleDragEndMouse);
42+
window.addEventListener('mousemove', this.handleMouseMove);
43+
44+
this.wrapper.addEventListener('mouseup', this.handleDragEndMouse);
5345
this.wrapper.addEventListener('mousemove', this.handleMouseMove);
46+
47+
this.handleDragStart(event);
5448
};
5549

5650
handleDragStartTouch = event => {
5751
// do not stop propagation here as it can be handled by parent to start scrolling
52+
53+
window.addEventListener('touchend', this.handleDragEndTouch);
54+
55+
this.wrapper.addEventListener('touchend', this.handleDragEndTouch);
56+
this.wrapper.addEventListener('touchmove', this.handleTouchMove);
57+
5858
const touch = event.targetTouches[0];
5959
this.handleDragStart(touch);
60-
this.wrapper.addEventListener('touchmove', this.handleTouchMove);
6160
};
6261

6362
handleDragStart = ({ clientX }) => {
@@ -75,12 +74,21 @@ class SwipeableListItem extends PureComponent {
7574
};
7675

7776
handleDragEndMouse = () => {
77+
window.removeEventListener('mouseup', this.handleDragEndMouse);
78+
window.removeEventListener('mousemove', this.handleMouseMove);
79+
80+
this.wrapper.removeEventListener('mouseup', this.handleDragEndMouse);
7881
this.wrapper.removeEventListener('mousemove', this.handleMouseMove);
82+
7983
this.handleDragEnd();
8084
};
8185

8286
handleDragEndTouch = () => {
87+
window.removeEventListener('touchend', this.handleDragEndTouch);
88+
89+
this.wrapper.removeEventListener('touchend', this.handleDragEndTouch);
8390
this.wrapper.removeEventListener('touchmove', this.handleTouchMove);
91+
8492
this.handleDragEnd();
8593
};
8694

0 commit comments

Comments
 (0)