From 6734fea82e4d722cdf03691984768239ed9913d4 Mon Sep 17 00:00:00 2001 From: Erwan Leboucher Date: Sat, 18 Jul 2026 10:40:38 +0200 Subject: [PATCH] refactor(swipe): track overlay swipe via live-seeded offset --- .../components/SwipeableOverlayWrapper.tsx | 26 ++++++++++--------- 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/src/app/components/SwipeableOverlayWrapper.tsx b/src/app/components/SwipeableOverlayWrapper.tsx index a77b802f5..9ea51218c 100644 --- a/src/app/components/SwipeableOverlayWrapper.tsx +++ b/src/app/components/SwipeableOverlayWrapper.tsx @@ -1,7 +1,7 @@ import type { ReactNode } from 'react'; -import { motion, useMotionValue, useSpring } from 'framer-motion'; +import { animate, motion, useMotionValue } from 'framer-motion'; import { useDrag } from '@use-gesture/react'; -import { useAtomValue } from 'jotai'; +import { useSetting } from '$state/hooks/settings'; import { settingsAtom } from '$state/settings'; import { mobileOrTablet } from '$utils/user-agent'; @@ -16,28 +16,29 @@ export function SwipeableOverlayWrapper({ onClose, direction, }: SwipeableOverlayWrapperProps) { - const settings = useAtomValue(settingsAtom); + const [mobileGestures] = useSetting(settingsAtom, 'mobileGestures'); const x = useMotionValue(0); - const springX = useSpring(x, { stiffness: 400, damping: 40 }); const bind = useDrag( - ({ active, movement: [mx], velocity: [vx], direction: [dx], event, event: e }) => { - if (e && 'target' in e && e.target instanceof HTMLElement) { - if (e.target.closest('[data-gestures="ignore"]')) { + ({ first, active, offset: [ox], velocity: [vx], direction: [dx], event }) => { + if (event && 'target' in event && event.target instanceof HTMLElement) { + if (event.target.closest('[data-gestures="ignore"]')) { return; } } - if (!settings.mobileGestures || !mobileOrTablet()) return; + if (!mobileGestures || !mobileOrTablet()) return; event.stopPropagation(); - let val = mx; + let val = ox; if (direction === 'left' && val > 0) val = 0; if (direction === 'right' && val < 0) val = 0; if (active) { + // Take over any settling spring; offset is seeded from the live position. + if (first) x.stop(); x.set(val); } else { const swipeThreshold = 100; @@ -52,7 +53,7 @@ export function SwipeableOverlayWrapper({ onClose(); } - x.set(0); + animate(x, 0, { type: 'spring', stiffness: 400, damping: 40 }); } }, { @@ -61,10 +62,11 @@ export function SwipeableOverlayWrapper({ rubberband: true, filterTaps: true, pointer: { capture: true }, + from: () => [x.get(), 0], } ); - if (!settings.mobileGestures || !mobileOrTablet()) { + if (!mobileGestures || !mobileOrTablet()) { return (