import { ref, inject } from 'vue'; import { StoreKey } from '../core/store'; const SWIPE_THRESHOLD = 50; const SWIPE_HORIZONTAL_RATIO = 1.5; const THROTTLE_MS = 400; /** * Touch swipe to navigate between slides on mobile. * Swipe left -> next, swipe right -> prev. * Only triggers when the gesture is clearly horizontal (avoids conflict with vertical scroll). */ export function useSwipeNav() { const store = inject(StoreKey); if (!store) return { onTouchStart: () => {}, onTouchEnd: () => {} }; const touchStart = ref<{ x: number; y: number } | null>(null); let lastSwipeTime = 0; const onTouchStart = (e: TouchEvent) => { if (e.touches.length !== 1) return; touchStart.value = { x: e.touches[0].clientX, y: e.touches[0].clientY }; }; const onTouchEnd = (e: TouchEvent) => { const start = touchStart.value; touchStart.value = null; if (!start || e.changedTouches.length !== 1) return; const { options } = store.state; if (!options.navigation) return; const now = Date.now(); if (now - lastSwipeTime < THROTTLE_MS) return; const end = e.changedTouches[0]; const dx = end.clientX - start.x; const dy = end.clientY - start.y; if (Math.abs(dx) < SWIPE_THRESHOLD) return; if (Math.abs(dx) <= Math.abs(dy) * SWIPE_HORIZONTAL_RATIO) return; if (dx < 0 && store.hasNext()) { store.next(); lastSwipeTime = now; } else if (dx > 0 && store.hasPrev()) { store.prev(); lastSwipeTime = now; } }; return { onTouchStart, onTouchEnd }; }