Skip to content

Commit

Permalink
Feat: Add mouse swipe event support (#4)
Browse files Browse the repository at this point in the history
* Feat: Add mouse swipe event support

* Update readme
  • Loading branch information
rajatkantinandi authored Oct 12, 2020
1 parent 6768143 commit 43fa1ad
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 9 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

[![NPM](https://img.shields.io/npm/v/use-swipe-hook.svg)](https://www.npmjs.com/package/use-swipe-hook)

> A simple and easy to use tiny library that provides useSwipe hook to use with React that enables swipe gestures for touch screens
> A simple and easy to use tiny library that provides useSwipe hook to use with React that enables swipe gestures for touch screens & non-touch screens via mouse events
## Install

Expand All @@ -23,7 +23,7 @@ function MyAwesomeComponent() {
return (
<div className="App">
<h1>use-swipe-hook demo</h1>
<h2>Works on touch devices</h2>
<h2>Works on both touch & non touch devices (by dragging mouse over the container)</h2>
<div className="swipeContainer" ref={swipeContainerRef}>
{direction
? `You have swiped ${direction}`
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "use-swipe-hook",
"version": "1.0.4",
"version": "1.1.0",
"description": "A simple and easy to use tiny library that provides useSwipe hook to use with React that enables swipe gestures for touch screens",
"main": "lib/index.umd.js",
"browser": "lib/index.umd.js",
Expand Down
34 changes: 29 additions & 5 deletions src/useSwipe.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,26 @@ const useSwipe = ({ ref, thresholdPX = 5 }) => {
React.useEffect(() => {
const currentElement = ref.current;
if (currentElement) {
currentElement.addEventListener('touchstart', handleTouchStart);
currentElement.addEventListener('touchend', handleTouchEnd);
if (isTouchDevice()) {
currentElement.addEventListener('touchstart', handleTouchStart);
currentElement.addEventListener('touchend', handleTouchEnd);
}
else {
currentElement.addEventListener('mousedown', handleMouseDown);
currentElement.addEventListener('mouseup', handleMouseUp);
}
}

return () => {
if (currentElement) {
currentElement.removeEventListener('touchstart', handleTouchStart);
currentElement.removeEventListener('touchend', handleTouchEnd);
if (isTouchDevice()) {
currentElement.removeEventListener('touchstart', handleTouchStart);
currentElement.removeEventListener('touchend', handleTouchEnd);
}
else {
currentElement.removeEventListener('mousedown', handleMouseDown);
currentElement.removeEventListener('mouseup', handleMouseUp);
}
}
}
}, [ref]);
Expand All @@ -43,7 +55,19 @@ const useSwipe = ({ ref, thresholdPX = 5 }) => {
}
}

function handleMouseDown(event) {
setX1(event.clientX);
setY1(event.clientY);
}

function handleMouseUp(event) {
setX2(event.clientX);
setY2(event.clientY);
}

return direction;
}
};

const isTouchDevice = () => ('ontouchstart' in window);

export default useSwipe;

0 comments on commit 43fa1ad

Please sign in to comment.