Skip to content

Commit

Permalink
improve comments
Browse files Browse the repository at this point in the history
  • Loading branch information
vitormv committed Feb 25, 2024
1 parent 8e6158f commit 9c7b7de
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/hooks/useHorizontalSwipe.ts
Expand Up @@ -16,6 +16,10 @@ type Coords = {
y: number;
};

/**
* Handy hook for enabling horizontal swiping on any component.
* The returned methods should be attached to the desired DOM element's onTouchStart, onTouchMove and onTouchEnd events.
*/
export const useHorizontalSwipe = (input: SwipeInput): SwipeOutput => {
const [touchStart, setTouchStart] = useState<Coords>({ x: 0, y: 0 });
const [touchEnd, setTouchEnd] = useState<Coords>({ x: 0, y: 0 });
Expand All @@ -35,7 +39,9 @@ export const useHorizontalSwipe = (input: SwipeInput): SwipeOutput => {
const horizontalDistance = touchStart.x - touchEnd.x;
const verticalDistance = touchStart.y - touchEnd.y;

// vertical distance should be no bigger than 50% of the horizontal distance
// in order to be considered an horizontal swipe, the vertical distance should be
// no bigger than 50% of the horizontal distance. This prevents accidental swipes
// while scrolling for example.
const isMostlyHorizontal = Math.abs(verticalDistance) < Math.abs(horizontalDistance) * 0.5;

if (!touchStart || !touchEnd || !isMostlyHorizontal) return;
Expand Down

0 comments on commit 9c7b7de

Please sign in to comment.