diff --git a/frontend/src/App.jsx b/frontend/src/App.jsx index 6d21984..28ff0eb 100644 --- a/frontend/src/App.jsx +++ b/frontend/src/App.jsx @@ -85,7 +85,7 @@ function Shell() { // Forward navigation starts at the top; going back lands where you left off. // The position is recorded from scroll events rather than read at route // change, because by then a shorter page may already have clamped it. - const pathRef = useRef(loc.pathname) + const pathRef = useRef(null) // iOS leaves the page displaced after the keyboard goes away (see lib/viewport-guard.js). useEffect(() => installViewportGuard(), []) // Click-drag a horizontal chip strip to scroll it sideways (lib/hchips.js) — on a desktop @@ -108,8 +108,16 @@ function Shell() { return () => window.removeEventListener('scroll', onScroll) }, []) useLayoutEffect(() => { + const samePath = pathRef.current === loc.pathname pathRef.current = loc.pathname if (navType !== 'POP') { window.scrollTo(0, 0); return } + // A POP that stays on the route we are on is not a back-navigation: it is the history + // entry a sheet pushed (Modals.jsx, #63) being unwound as the sheet closes. Nothing new + // mounted, Modals puts the page back where it was itself, and a view that scrolled on + // purpose because the sheet closed — the workout list going to the current exercise after + // ⋯ → Layout → List (#224) — must not be dragged back to a position recorded before that + // scroll's event had even been dispatched. + if (samePath) return const y = scrollPositions.get(loc.pathname) || 0 // the restored view needs a layout pass before it is tall enough to scroll to y const frame = window.requestAnimationFrame(() => window.scrollTo(0, y)) diff --git a/frontend/src/components/Modals.jsx b/frontend/src/components/Modals.jsx index 397ccc4..39156cd 100644 --- a/frontend/src/components/Modals.jsx +++ b/frontend/src/components/Modals.jsx @@ -1,5 +1,6 @@ import { useEffect, useRef } from 'react' import { useUI } from '../store/useUI.js' +import { keyboardOpen } from '../lib/viewport-guard.js' // One bottom sheet (or centered dialog) with swipe-to-dismiss. function Sheet({ sheet }) { @@ -180,6 +181,12 @@ export default function Modals() { // iOS scrolls the page again while the keyboard dismisses — after the line above ran. // Ask once more when that animation is over. The window-level guard in // lib/viewport-guard.js covers the keyboard closing while a sheet stays open. + // Only with the keyboard up, though: without one there is nothing to undo, and a late + // restore would overwrite a scroll the page behind made on purpose because the sheet + // closed — the workout list going to the current exercise after ⋯ → Layout → List + // (issue #224). At this point the sheet's field is already out of the DOM but the + // keyboard has not started to go, so the visual viewport still tells the truth. + if (!keyboardOpen()) return window.setTimeout(() => { if (document.body.style.position !== 'fixed') window.scrollTo(0, y) }, 350) } }, [sheets.length > 0]) diff --git a/frontend/src/components/Modals.test.jsx b/frontend/src/components/Modals.test.jsx index 09038a7..3bdb4f6 100644 --- a/frontend/src/components/Modals.test.jsx +++ b/frontend/src/components/Modals.test.jsx @@ -292,3 +292,38 @@ describe('Modals drag axis lock and dismiss', () => { expect(mocks.state.sheets).toHaveLength(1) }) }) + +describe('Modals scroll restore on close', () => { + // The body is pinned while a sheet is open and the page is put back where it was on close. + // The second, delayed restore exists for one reason — iOS scrolling the page again while the + // keyboard dismisses — so it must only be armed when the keyboard is actually up (QA C1). + async function openAndClose() { + dom.scrollY = 320 + await setSheets([sheet('menu')]) + expect(document.body.style.position).toBe('fixed') + dom.scrollTo.mockClear() + await setSheets([]) + } + + it('restores the position once and leaves a scroll the page made afterwards alone', async () => { + vi.useFakeTimers() + await openAndClose() + expect(dom.scrollTo).toHaveBeenCalledTimes(1) + expect(dom.scrollTo).toHaveBeenCalledWith(0, 320) + await act(async () => { vi.advanceTimersByTime(400) }) + expect(dom.scrollTo).toHaveBeenCalledTimes(1) + vi.useRealTimers() + }) + + it('restores again after the keyboard dismiss animation when the sheet closed with the keyboard up', async () => { + vi.useFakeTimers() + dom.innerHeight = 800 + dom.visualViewport = { height: 460 } + await openAndClose() + expect(dom.scrollTo).toHaveBeenCalledTimes(1) + await act(async () => { vi.advanceTimersByTime(400) }) + expect(dom.scrollTo).toHaveBeenCalledTimes(2) + expect(dom.scrollTo).toHaveBeenLastCalledWith(0, 320) + vi.useRealTimers() + }) +}) diff --git a/frontend/src/index.css b/frontend/src/index.css index 9fbcc37..10936fd 100644 --- a/frontend/src/index.css +++ b/frontend/src/index.css @@ -1165,8 +1165,10 @@ body.chips-dragging *{cursor:inherit} border-radius:var(--r-lg);padding:14px 14px 4px; background:var(--surface-2); } -/* scrolled to when the list opens (#224): clear the sticky header above it */ -.wl-unit{scroll-margin-top:calc(var(--sat) + 96px)} +/* scrolled to when the list opens (#224): clear the sticky header above it. Workout.jsx + measures the header (--whdr-h on .workout-list, safe area included) because it grows when + the session name wraps; the fallback is the height of a one-line header. */ +.wl-unit{scroll-margin-top:calc(var(--whdr-h, var(--sat) + 80px) + 16px)} .wl-unit.cur{box-shadow:inset 0 0 0 1.5px var(--acc-line)} .wl-hd{display:flex;align-items:center;justify-content:space-between;gap:8px;margin-bottom:10px} @media (max-width:400px){ diff --git a/frontend/src/views/Workout.jsx b/frontend/src/views/Workout.jsx index 89a474b..dee908e 100644 --- a/frontend/src/views/Workout.jsx +++ b/frontend/src/views/Workout.jsx @@ -581,13 +581,36 @@ function ActiveWorkout() { // The list opens at the exercise you are on, not at the top of the session (issue #224): you // switch to it mid-workout to look at what comes before and after. Only on the way in — once // the list is open, "current" moves because you tick rows in it, and a list that scrolls - // itself under your thumb is worse than one that stays put. + // itself under your thumb is worse than one that stays put. List ↔ Compact counts as a way + // in: the rows change height, so the same scroll offset lands somewhere else. + // + // The scroll waits for the next frame rather than running in the effect itself. App.jsx + // restores the route's remembered position in a frame it asked for during the same commit + // (a reload, a back navigation), and a sheet closing (⋯ → Layout → List) puts the page back + // where it was before the sheet opened, in an effect cleanup that runs before this one — + // both would win over a scroll made right here. A frame asked for now runs after theirs. const listRef = useRef(null) + const hdrRef = useRef(null) useEffect(() => { if (!listMode) return - const el = listRef.current?.querySelector('.wl-unit.cur') - if (el && typeof el.scrollIntoView === 'function') el.scrollIntoView({ block: 'start' }) - }, [listMode]) + const schedule = callback => window.requestAnimationFrame + ? window.requestAnimationFrame(callback) + : window.setTimeout(callback, 0) + const cancel = frame => window.cancelAnimationFrame + ? window.cancelAnimationFrame(frame) + : window.clearTimeout(frame) + const frame = schedule(() => { + const list = listRef.current + const el = list?.querySelector('.wl-unit.cur') + if (!el || typeof el.scrollIntoView !== 'function') return + // The sticky header grows when the workout name wraps; the unit's scroll margin (index.css) + // clears whatever height it has right now, with a one-line height as the fallback. + const hdrH = hdrRef.current?.offsetHeight + if (hdrH) list.style.setProperty('--whdr-h', hdrH + 'px') + el.scrollIntoView({ block: 'start' }) + }) + return () => cancel(frame) + }, [workoutView]) const total = setUnitsTotal(A.entries) const done = setsDoneActive(A) @@ -942,7 +965,7 @@ function ActiveWorkout() { {/* In list mode the whole session scrolls under the header, so the header (name, clock, set counter, discard/finish, progress) stays pinned — the one thing you want in view while you are somewhere in the middle of a long stack. Cards mode never scrolls far. */} -