Skip to content

Commit 0d67262

Browse files
author
Marek Rozmus
committed
Fix code review issues and add css modules
1 parent 05fa2b8 commit 0d67262

File tree

8 files changed

+79
-56
lines changed

8 files changed

+79
-56
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
## Installation
44

55
```bash
6-
npm install @sandstreamdev/react-swipeable-list
6+
npm install sandstreamdev/react-swipeable-list
77
# or via yarn
8-
yarn add @sandstreamdev/react-swipeable-list
8+
yarn add sandstreamdev/react-swipeable-list
99
```
1010

1111
## Usage

package-lock.json

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,23 @@
11
{
22
"name": "react-swipeable-list",
3+
"description": "Swipeable list component for React",
34
"version": "0.0.1",
5+
"author": {
6+
"name": "Sandstream Development",
7+
"url": "https://github.com/sandstreamdev/react-swipeable-list/graphs/contributors"
8+
},
9+
"repository": {
10+
"type": "git",
11+
"url": "https://github.com/sandstreamdev/react-swipeable-list"
12+
},
13+
"license": "MIT",
14+
"keywords": [
15+
"swipe",
16+
"list",
17+
"react",
18+
"component",
19+
"custom"
20+
],
421
"main": "dist/react-swipeable-list.cjs.js",
522
"module": "dist/react-swipeable-list.esm.js",
623
"browser": "dist/react-swipeable-list.umd.js",

rollup.config.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ export default [
3030
}
3131
}),
3232
postcss({
33-
extract: true
33+
extract: true,
34+
modules: true
3435
})
3536
]
3637
},
@@ -46,7 +47,8 @@ export default [
4647
exclude: ['node_modules/**']
4748
}),
4849
postcss({
49-
extract: true
50+
extract: true,
51+
modules: true
5052
})
5153
]
5254
}

src/SwipeableList.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
.swipeable-list {
1+
.swipeableList {
22
flex: 1;
33
background: white;
44
width: 100%;

src/SwipeableList.js

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

4-
import './SwipeableList.css';
4+
import styles from './SwipeableList.css';
55

66
const SwipeableList = ({ children }) => {
77
const [blockSwipe, setBlockSwipe] = useState(false);
@@ -35,7 +35,7 @@ const SwipeableList = ({ children }) => {
3535

3636
return (
3737
<div
38-
className="swipeable-list"
38+
className={styles.swipeableList}
3939
onMouseDown={onDragStart}
4040
onTouchStart={onDragStart}
4141
onScroll={onScroll}

src/SwipeableListItem.css

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
.swipeable-list-item {
1+
.swipeableListItem {
22
position: relative;
33
transition: max-height 0.5s ease;
44
max-height: 1000px;
@@ -8,7 +8,7 @@
88
z-index: 0;
99
}
1010

11-
.swipeable-list-item > .background {
11+
.contentLeft {
1212
position: absolute;
1313
width: 100%;
1414
height: 100%;
@@ -20,15 +20,23 @@
2020
opacity: 0;
2121
}
2222

23-
.swipeable-list-item > .background.right {
23+
.contentRight {
24+
composes: contentLeft;
2425
justify-content: flex-end;
2526
}
2627

27-
.swipeable-list-item > .background.return {
28+
.return {
2829
transition: opacity 0.5s ease-out;
2930
}
3031

31-
.swipeable-list-item > .content {
32+
.contentLeftReturn {
33+
composes: contentLeft return;
34+
}
35+
.contentRightReturn {
36+
composes: contentRight return;
37+
}
38+
39+
.content {
3240
width: 100%;
3341
align-items: center;
3442
box-sizing: border-box;
@@ -37,6 +45,7 @@
3745
display: flex;
3846
}
3947

40-
.swipeable-list-item > .content.return {
48+
.contentReturn {
49+
composes: content;
4150
transition: transform 0.5s ease-out;
4251
}

src/SwipeableListItem.js

Lines changed: 35 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
import React, { PureComponent } from 'react';
22
import PropTypes from 'prop-types';
33

4-
import './SwipeableListItem.css';
4+
import styles from './SwipeableListItem.css';
55

66
const SwipeActionPropType = PropTypes.shape({
77
action: PropTypes.func.isRequired,
8-
background: PropTypes.node.isRequired
8+
content: PropTypes.node.isRequired
99
});
1010

1111
class SwipeableListItem extends PureComponent {
1212
constructor(props) {
1313
super(props);
1414

15-
this.backgroundLeft = null;
16-
this.backgroundRight = null;
15+
this.contentLeft = null;
16+
this.contentRight = null;
1717
this.listElement = null;
1818
this.wrapper = null;
1919

@@ -49,12 +49,12 @@ class SwipeableListItem extends PureComponent {
4949
onDragStart = ({ clientX }) => {
5050
this.dragged = true;
5151
this.dragStartX = clientX;
52-
this.listElement.className = 'content';
53-
if (this.backgroundLeft) {
54-
this.backgroundLeft.className = 'background';
52+
this.listElement.className = styles.content;
53+
if (this.contentLeft) {
54+
this.contentLeft.className = styles.contentLeft;
5555
}
56-
if (this.backgroundRight) {
57-
this.backgroundRight.className = 'background right';
56+
if (this.contentRight) {
57+
this.contentRight.className = styles.contentRight;
5858
}
5959
this.startTime = Date.now();
6060
requestAnimationFrame(this.updatePosition);
@@ -83,34 +83,34 @@ class SwipeableListItem extends PureComponent {
8383
}
8484

8585
this.left = 0;
86-
this.listElement.className = 'content return';
86+
this.listElement.className = styles.contentReturn;
8787
this.listElement.style.transform = `translateX(${this.left}px)`;
8888

8989
// hide backgrounds
90-
if (this.backgroundLeft) {
91-
this.backgroundLeft.style.opacity = 0;
92-
this.backgroundLeft.className = `${this.backgroundLeft.className} return`;
90+
if (this.contentLeft) {
91+
this.contentLeft.style.opacity = 0;
92+
this.contentLeft.className = styles.contentLeftReturn;
9393
}
9494

95-
if (this.backgroundRight) {
96-
this.backgroundRight.style.opacity = 0;
97-
this.backgroundRight.className = `${this.backgroundRight.className} return`;
95+
if (this.contentRight) {
96+
this.contentRight.style.opacity = 0;
97+
this.contentRight.className = styles.contentRightReturn;
9898
}
9999
}
100100
};
101101

102102
shouldMoveItem = delta => {
103103
const {
104-
swipeLeft: { background: backgroundLeft } = {},
105-
swipeRight: { background: backgroundRight } = {},
104+
swipeLeft: { content: contentLeft } = {},
105+
swipeRight: { content: contentRight } = {},
106106
blockSwipe
107107
} = this.props;
108108
const swipingLeft = delta < 0;
109109
const swipingRight = delta > 0;
110110

111111
return (
112112
!blockSwipe &&
113-
((swipingLeft && backgroundLeft) || (swipingRight && backgroundRight))
113+
((swipingLeft && contentLeft) || (swipingRight && contentRight))
114114
);
115115
};
116116

@@ -145,32 +145,27 @@ class SwipeableListItem extends PureComponent {
145145
const elapsed = now - this.startTime;
146146

147147
if (this.dragged && elapsed > this.fpsInterval) {
148-
let backgroundToShow =
149-
this.left < 0 ? this.backgroundLeft : this.backgroundRight;
150-
let backgroundToHide =
151-
this.left < 0 ? this.backgroundRight : this.backgroundLeft;
148+
let contentToShow = this.left < 0 ? this.contentLeft : this.contentRight;
149+
let contentToHide = this.left < 0 ? this.contentRight : this.contentLeft;
152150

153-
if (!backgroundToShow) {
151+
if (!contentToShow) {
154152
return;
155153
}
156154

157155
const opacity = (Math.abs(this.left) / 100).toFixed(2);
158156

159157
this.listElement.style.transform = `translateX(${this.left}px)`;
160158

161-
if (
162-
opacity < 1 &&
163-
opacity.toString() !== backgroundToShow.style.opacity
164-
) {
165-
backgroundToShow.style.opacity = opacity.toString();
159+
if (opacity < 1 && opacity.toString() !== contentToShow.style.opacity) {
160+
contentToShow.style.opacity = opacity.toString();
166161

167-
if (backgroundToHide) {
168-
backgroundToHide.style.opacity = '0';
162+
if (contentToHide) {
163+
contentToHide.style.opacity = '0';
169164
}
170165
}
171166

172167
if (opacity >= 1) {
173-
backgroundToShow.style.opacity = '1';
168+
contentToShow.style.opacity = '1';
174169
}
175170

176171
this.startTime = Date.now();
@@ -193,31 +188,31 @@ class SwipeableListItem extends PureComponent {
193188
}
194189
};
195190

196-
bindBackgroundLeft = ref => (this.backgroundLeft = ref);
197-
bindBackgroundRight = ref => (this.backgroundRight = ref);
191+
bindContentLeft = ref => (this.contentLeft = ref);
192+
bindContentRight = ref => (this.contentRight = ref);
198193
bindListElement = ref => (this.listElement = ref);
199194
bindWrapper = ref => (this.wrapper = ref);
200195

201196
render() {
202197
const { children, swipeLeft, swipeRight } = this.props;
203198

204199
return (
205-
<div className="swipeable-list-item" ref={this.bindWrapper}>
200+
<div className={styles.swipeableListItem} ref={this.bindWrapper}>
206201
{swipeLeft && (
207-
<div ref={this.bindBackgroundLeft} className="background">
208-
{swipeLeft.background}
202+
<div ref={this.bindContentLeft} className={styles.contentLeft}>
203+
{swipeLeft.content}
209204
</div>
210205
)}
211206
{swipeRight && (
212-
<div ref={this.bindBackgroundRight} className="background right">
213-
{swipeRight.background}
207+
<div ref={this.bindContentRight} className={styles.contentRight}>
208+
{swipeRight.content}
214209
</div>
215210
)}
216211
<div
217212
ref={this.bindListElement}
218213
onMouseDown={this.onDragStartMouse}
219214
onTouchStart={this.onDragStartTouch}
220-
className="content"
215+
className={styles.content}
221216
>
222217
{children}
223218
</div>

0 commit comments

Comments
 (0)