Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add support for mouse events #52

Closed
wants to merge 1 commit into from
Closed
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
40 changes: 33 additions & 7 deletions src/Swipeable.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,15 @@ const Swipeable = React.createClass({
},

calculatePos: function (e) {
const x = e.changedTouches[0].clientX
const y = e.changedTouches[0].clientY
let x, y
// If not a touch, determine point from mouse coordinates
if (e.changedTouches) {
x = e.changedTouches[0].clientX
y = e.changedTouches[0].clientY
} else {
x = e.clientX
y = e.clientY
}

const xd = this.state.x - x
const yd = this.state.y - y
Expand All @@ -61,22 +68,26 @@ const Swipeable = React.createClass({
},

touchStart: function (e) {
if (e.touches.length > 1) {
if (e.touches && e.touches.length > 1) {
return
}

// If not a touch, determine point from mouse coordinates
let touches = e.touches
if (!touches) {
touches = [{ clientX: e.clientX, clientY: e.clientY }]
}
if (this.props.stopPropagation) e.stopPropagation()

this.setState({
start: Date.now(),
x: e.touches[0].clientX,
y: e.touches[0].clientY,
x: touches[0].clientX,
y: touches[0].clientY,
swiping: false
})
},

touchMove: function (e) {
if (!this.state.x || !this.state.y || e.touches.length > 1) {
if (!this.state.x || !this.state.y || e.touches && e.touches.length > 1) {
return
}

Expand Down Expand Up @@ -160,12 +171,27 @@ const Swipeable = React.createClass({
this.setState(this.getInitialState())
},

mouseDown(e) {
this.touchStart(e);
},

mouseMove(e) {
this.touchMove(e);
},

mouseUp(e) {
this.touchEnd(e);
},

render: function () {
const newProps = {
...this.props,
onTouchStart: this.touchStart,
onTouchMove: this.touchMove,
onTouchEnd: this.touchEnd,
onMouseDown: this.mouseDown,
onMouseMove: this.mouseMove,
onMouseUp: this.mouseUp
}

delete newProps.onSwiped
Expand Down