Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions assets/index.less
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
@swipeout-prefix-cls: rc-swipeout;

html {
overflow: hidden;
}
.@{swipeout-prefix-cls} {
overflow: hidden;
position: relative;
Expand Down
93 changes: 56 additions & 37 deletions examples/simple.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,45 +5,64 @@ import Swipeout from '../src';
import React from 'react';
import ReactDOM from 'react-dom';

const SwipeDemo = () => (
<Swipeout
style={{ backgroundColor: 'white' }}
autoClose
right={[
{ text: 'more more',
onPress: () => console.log('more more'),
style: { backgroundColor: 'orange', color: 'white' },
},
{ text: 'delete',
onPress: () => console.log('delete'),
style: { backgroundColor: 'red', color: 'white' },
},
]}
left={[
{
text: 'read',
onPress: () => console.log('read'),
style: { backgroundColor: 'blue', color: 'white' },
},
{
text: 'reply me',
onPress: () => console.log('reply me'),
style: { backgroundColor: 'green', color: 'white' },
},
{
text: 'reply other people',
onPress: () => console.log('reply other people'),
style: { backgroundColor: 'gray', color: 'white' },
},
]}
onOpen={() => console.log('open')}
onClose={() => console.log('close')}
>
<div style={{
height: 44,
backgroundColor: 'white',
lineHeight: '44px',
borderTop: '1px solid #dedede',
borderBottom: '1px solid #dedede',
}}
>swipe out simple demo</div>
</Swipeout>
);

ReactDOM.render(
<div style={{ marginBottom: 12 }}>
<Swipeout
style={{ backgroundColor: 'white' }}
autoClose
right={[
{ text: 'more',
onPress: () => console.log('more'),
style: { backgroundColor: 'orange', color: 'white' },
},
{ text: 'delete',
onPress: () => console.log('delete'),
style: { backgroundColor: 'red', color: 'white' },
},
]}
left={[
{
text: 'read',
onPress: () => console.log('read'),
style: { backgroundColor: 'blue', color: 'white' },
},
{
text: 'reply',
onPress: () => console.log('reply'),
style: { backgroundColor: 'green', color: 'white' },
},
]}
onOpen={() => console.log('open')}
onClose={() => console.log('close')}
>
<div style={{
height: 44,
backgroundColor: 'white',
lineHeight: '44px',
borderTop: '1px solid #dedede',
borderBottom: '1px solid #dedede',
}}
>swipe out simple demo</div>
</Swipeout>
<SwipeDemo />
<SwipeDemo />
<SwipeDemo />
<SwipeDemo />
<SwipeDemo />
<SwipeDemo />
<SwipeDemo />
<SwipeDemo />
<SwipeDemo />
<SwipeDemo />
<SwipeDemo />
</div>,
document.getElementById('__react-content'),
);
89 changes: 51 additions & 38 deletions src/Swipeout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,13 @@ class Swipeout extends React.Component <SwipeoutPropType, any> {
right: any;
btnsLeftWidth: number;
btnsRightWidth: number;
panStartX: number;
panStartY: number;
swiping: boolean;
needShowLeft: boolean;
needShowRight: boolean;

constructor(props) {
super(props);

this.onPanStart = this.onPanStart.bind(this);
this.onPan = this.onPan.bind(this);
this.onPanEnd = this.onPanEnd.bind(this);
this.onCloseSwipe = this.onCloseSwipe.bind(this);

this.openedLeft = false;
this.openedRight = false;
}
Expand All @@ -49,7 +44,7 @@ class Swipeout extends React.Component <SwipeoutPropType, any> {
document.body.removeEventListener('touchstart', this.onCloseSwipe, true);
}

onCloseSwipe(ev) {
onCloseSwipe = (ev) => {
if (this.openedLeft || this.openedRight) {
const pNode = (node => {
while (node.parentNode && node.parentNode !== document.body) {
Expand All @@ -66,49 +61,64 @@ class Swipeout extends React.Component <SwipeoutPropType, any> {
}
}

onPanStart(e) {
this.panStartX = e.deltaX;
this.panStartY = e.deltaY;
}

onPan(e) {
const posX = e.deltaX - this.panStartX;
const posY = e.deltaY - this.panStartY;
onPanStart = (e) => {
const { direction, deltaX } = e;
// http://hammerjs.github.io/api/#directions
const isLeft = direction === 2;
const isRight = direction === 4;

if (Math.abs(posX) <= Math.abs(posY)) {
if (!isLeft && !isRight) {
return;
}

const { left, right } = this.props;
if (posX < 0 && right!.length) {
this.swiping = true;
this._setStyle(Math.min(posX, 0));
} else if (posX > 0 && left!.length) {
this.needShowRight = isLeft && right!.length > 0;
this.needShowLeft = isRight && left!.length > 0;
if (this.left) {
this.left.style.visibility = this.needShowRight ? 'hidden' : 'visible';
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

加的这段逻辑是为了解决当往右滑的时候,如果左侧的按钮很多,会导致反而右侧的内容显示出来,左侧的左右边的部分内容被盖住

}
if (this.right) {
this.right.style.visibility = this.needShowLeft ? 'hidden' : 'visible';
}
if (this.needShowLeft || this.needShowRight) {
this.swiping = true;
this._setStyle(Math.max(posX, 0));
this._setStyle(deltaX);
}
}
onPan = (e) => {
const { deltaX } = e;
if (!this.swiping) {
return;
}
this._setStyle(deltaX);
}

onPanEnd(e) {
onPanEnd = (e) => {
if (!this.swiping) {
return;
}
this.swiping = false;

const { left = [], right = [] } = this.props;
const btnsLeftWidth = this.btnsLeftWidth;
const btnsRightWidth = this.btnsRightWidth;
const posX = e.deltaX - this.panStartX;
const openLeft = posX > btnsLeftWidth / 2;
const openRight = posX < -btnsRightWidth / 2;

if (openRight && posX < 0 && right.length) {
const { direction, deltaX } = e;
// http://hammerjs.github.io/api/#directions
const isLeft = direction === 2;
const isRight = direction === 4;

const needOpenRight = this.needShowRight && Math.abs(deltaX) > btnsRightWidth / 2;
const needOpenLeft = this.needShowLeft && Math.abs(deltaX) > btnsRightWidth / 2;

if (needOpenRight) {
this.open(-btnsRightWidth, false, true);
} else if (openLeft && posX > 0 && left.length) {
} else if (needOpenLeft) {
this.open(btnsLeftWidth, true, false);
} else {
this.close();
}
this.swiping = false;
this.needShowLeft = false;
this.needShowRight = false;
}

// left & right button click
Expand All @@ -124,16 +134,19 @@ class Swipeout extends React.Component <SwipeoutPropType, any> {

_getContentEasing(value, limit) {
// limit content style left when value > actions width
if (value < 0 && value < limit) {
return limit - Math.pow(limit - value, 0.85);
} else if (value > 0 && value > limit) {
return limit + Math.pow(value - limit, 0.85);
const delta = Math.abs(value) - Math.abs(limit);
const isOverflow = delta > 0;
const factor = limit > 0 ? 1 : -1;
if (isOverflow) {
value = limit + Math.pow(delta, 0.85) * factor;
return Math.abs(value) > Math.abs(limit) ? limit : value;
}
return value;
}

// set content & actions style
_setStyle(value) {
_setStyle = (value) => {

const limit = value > 0 ? this.btnsLeftWidth : -this.btnsRightWidth;
const contentLeft = this._getContentEasing(value, limit);
this.content.style.left = `${contentLeft}px`;
Expand All @@ -143,7 +156,7 @@ class Swipeout extends React.Component <SwipeoutPropType, any> {
}
}

open(value, openedLeft, openedRight) {
open = (value, openedLeft, openedRight) => {
if (!this.openedLeft && !this.openedRight && this.props.onOpen) {
this.props.onOpen();
}
Expand All @@ -153,7 +166,7 @@ class Swipeout extends React.Component <SwipeoutPropType, any> {
this._setStyle(value);
}

close() {
close = () => {
if ((this.openedLeft || this.openedRight) && this.props.onClose) {
this.props.onClose();
}
Expand Down Expand Up @@ -213,7 +226,7 @@ class Swipeout extends React.Component <SwipeoutPropType, any> {
>
<div className={`${prefixCls}-content`}>{children}</div>
</Hammer>
</div>
</div>
) : (
<div {...refProps} {...divProps}>{children}</div>
);
Expand Down