-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
91 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
type HorizontalSwipeDirection = 'right' | 'left'; | ||
|
||
export function onHorizontalSwipe( | ||
element: HTMLElement, | ||
handler: (swipeDirection: HorizontalSwipeDirection) => void | ||
) { | ||
// Min x distance traveled to be considered swipe | ||
const xMinThreshold = 50; | ||
// Max y distance that can be traveled before | ||
// it's no longer considered a horizontal swipe | ||
const yMaxThreshold = 100; | ||
// Max time allowed to travel that distance | ||
const allowedTime = 200; | ||
|
||
let startTime = 0; | ||
let startX: number; | ||
let startY: number; | ||
|
||
element.addEventListener( | ||
'touchstart', | ||
function (e) { | ||
startX = e.changedTouches[0].pageX; | ||
startY = e.changedTouches[0].pageY; | ||
startTime = performance.now(); | ||
}, | ||
false | ||
); | ||
|
||
element.addEventListener( | ||
'touchmove', | ||
function (e) { | ||
// Prevent dragging viewport | ||
e.preventDefault(); | ||
}, | ||
false | ||
); | ||
|
||
element.addEventListener( | ||
'touchend', | ||
function (e) { | ||
const touch = e.changedTouches[0]; | ||
const deltaX = touch.pageX - startX; | ||
const deltaY = touch.pageY - startY; | ||
const elapsedTime = performance.now() - startTime; | ||
|
||
// Check that elapsed time is within specified, horizontal dist | ||
// traveled >= threshold, and vertical dist traveled <= 100 | ||
const isSwipe = | ||
elapsedTime <= allowedTime && | ||
Math.abs(deltaX) >= xMinThreshold && | ||
Math.abs(deltaY) <= yMaxThreshold; | ||
if (isSwipe) { | ||
handler(deltaX < 0 ? 'right' : 'left'); | ||
} | ||
}, | ||
false | ||
); | ||
} |