function createSwipeDetector({ target = document.body, threshold = 30, onSwipe = null } = {}) {
let startY = 0;
let endY = 0;
const handleTouchStart = (e) => {
if (e.touches.length !== 1) return;
startY = e.touches[0].clientY;
endY = startY;
};
const handleTouchMove = (e) => {
if (e.touches.length !== 1) return;
endY = e.touches[0].clientY;
};
const handleTouchEnd = () => {
const diff = endY - startY;
let direction = null;
if (diff > threshold) {
direction = 'down';
} else if (diff < -threshold) {
direction = 'up';
}
if (onSwipe && direction) {
onSwipe(direction);
}
};
const optionsPassive = { passive: true };
target.addEventListener('touchstart', handleTouchStart, optionsPassive);
target.addEventListener('touchmove', handleTouchMove, { passive: false });
target.addEventListener('touchend', handleTouchEnd, optionsPassive);
target.addEventListener('touchcancel', handleTouchEnd, optionsPassive);
const destroy = () => {
target.removeEventListener('touchstart', handleTouchStart);
target.removeEventListener('touchmove', handleTouchMove);
target.removeEventListener('touchend', handleTouchEnd);
target.removeEventListener('touchcancel', handleTouchEnd);
};
return { destroy };
}