Files
immich-folio/hooks/useSwipe.ts

33 lines
976 B
TypeScript
Raw Normal View History

import { useRef, useCallback } from 'react';
interface UseSwipeProps {
onSwipeLeft: () => void;
onSwipeRight: () => void;
threshold?: number;
}
export function useSwipe({ onSwipeLeft, onSwipeRight, threshold = 60 }: UseSwipeProps) {
const touchStartX = useRef<number>(0);
const touchStartY = useRef<number>(0);
const handleTouchStart = useCallback((e: React.TouchEvent | TouchEvent) => {
touchStartX.current = e.touches[0].clientX;
touchStartY.current = e.touches[0].clientY;
}, []);
const handleTouchEnd = useCallback(
(e: React.TouchEvent | TouchEvent) => {
const dx = e.changedTouches[0].clientX - touchStartX.current;
const dy = e.changedTouches[0].clientY - touchStartY.current;
if (Math.abs(dx) > threshold && Math.abs(dx) > Math.abs(dy)) {
if (dx > 0) onSwipeRight();
else onSwipeLeft();
}
},
[onSwipeLeft, onSwipeRight, threshold],
);
return { handleTouchStart, handleTouchEnd };
}