Skip to content

Commit

Permalink
fix: fix horizontal scroll mouse button navigation (fix #305)
Browse files Browse the repository at this point in the history
  • Loading branch information
igordanchenko committed Sep 19, 2024
1 parent b4435e9 commit 626368b
Showing 1 changed file with 21 additions and 6 deletions.
27 changes: 21 additions & 6 deletions src/modules/Controller/useWheelSwipe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ export function useWheelSwipe<T extends Element = Element>(
const intent = React.useRef(0);
const intentCleanup = React.useRef<number>();
const resetCleanup = React.useRef<number>();
const wheelResidualMomentum = React.useRef(0);
const wheelInertia = React.useRef(0);
const wheelInertiaCleanup = React.useRef<number>();
const startTime = React.useRef(0);

const { setTimeout, clearTimeout } = useTimeouts();
Expand Down Expand Up @@ -70,9 +71,23 @@ export function useWheelSwipe<T extends Element = Element>(
return;
}

const setWheelInertia = (inertia: number) => {
wheelInertia.current = inertia;

clearTimeout(wheelInertiaCleanup.current);

wheelInertiaCleanup.current =
inertia > 0
? setTimeout(() => {
wheelInertia.current = 0;
wheelInertiaCleanup.current = undefined;
}, 300)
: undefined;
};

if (swipeState === SwipeState.NONE) {
if (Math.abs(event.deltaX) <= 1.2 * Math.abs(wheelResidualMomentum.current)) {
wheelResidualMomentum.current = event.deltaX;
if (Math.abs(event.deltaX) <= 1.2 * Math.abs(wheelInertia.current)) {
setWheelInertia(event.deltaX);
return;
}

Expand All @@ -86,7 +101,7 @@ export function useWheelSwipe<T extends Element = Element>(

if (Math.abs(intent.current) > 30) {
intent.current = 0;
wheelResidualMomentum.current = 0;
setWheelInertia(0);
startTime.current = Date.now();

onSwipeStart();
Expand All @@ -109,7 +124,7 @@ export function useWheelSwipe<T extends Element = Element>(
cancelSwipeResetCleanup();

if (Math.abs(newSwipeOffset) > 0.2 * containerWidth) {
wheelResidualMomentum.current = event.deltaX;
setWheelInertia(event.deltaX);

onSwipeFinish(newSwipeOffset, Date.now() - startTime.current);

Expand All @@ -118,7 +133,7 @@ export function useWheelSwipe<T extends Element = Element>(

resetCleanup.current = setTimeout(() => handleCancelSwipe(newSwipeOffset), 2 * swipeAnimationDuration);
} else {
wheelResidualMomentum.current = event.deltaX;
setWheelInertia(event.deltaX);
}
});

Expand Down

0 comments on commit 626368b

Please sign in to comment.