From 6e39fbd4b42780301cc3882b988e6aeeba43bd6b Mon Sep 17 00:00:00 2001 From: Duarte Santos Date: Sat, 19 Sep 2026 21:23:26 +0200 Subject: [PATCH 1/2] =?UTF-8?q?List=20view=20lands=20on=20the=20current=20?= =?UTF-8?q?exercise=20through=20the=20=E2=8B=AF=20menu=20too=20=E2=80=94?= =?UTF-8?q?=20two=20scroll=20restores=20were=20undoing=20it=20(QA=20C1)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Choosing List (or Compact) from ⋯ → Layout closes a sheet, and two things put the page back where it was before that sheet opened, after the list had already scrolled to the current unit (#224): - Modals.jsx re-applied the pre-sheet scrollY a second time 350 ms after closing. That repeat exists for one case only — iOS scrolling the page again while the keyboard dismisses — so it is now armed only when the keyboard is up at close time. With no keyboard there is nothing to undo, and the late restore overwrote a scroll the view made on purpose. - App.jsx treated the popstate from the sheet's history entry (Modals pushes one per sheet, #63) as a back-navigation: the pathname is the same, only the navigation type flips to POP after the PUSH that started the workout, and the effect restored the position it had recorded — read before the scroll event for the new position had been dispatched, so it was the old one. A POP that stays on the current route is not a navigation and is skipped now. That is also why the first sheet close after Start failed and later ones (POP → POP) did not. The list's own scroll now runs in the next frame instead of in the effect: App.jsx asks for its restore frame during the same commit (reload, back-navigation), so a frame asked for afterwards runs after it. It also re-runs on List ↔ Compact, where the rows change height and the old offset lands on a different exercise. Measured at 368 px on the QA rig (scripts/opengym-qa/run/fix-list-scroll/measure.mjs, current unit's top vs. sticky header bottom, 77 px): before cards→list via menu: scrollY 2601 at 223 ms, back to 399 at 304 ms, unit 2217 px below the fold (2/2); list→compact ok after cards→list 93 px (gap 15) 2/2, list→compact 93 px 2/2, compact→list 93 px 2/2, reload 89 px 6/6, reload with a current superset 89 px 4/4, back from Stats 89 px 2/2; verifier's own scripts 8/8 MENU OK at 368 and 412 --- frontend/src/App.jsx | 10 ++++++- frontend/src/components/Modals.jsx | 7 +++++ frontend/src/components/Modals.test.jsx | 35 +++++++++++++++++++++++++ frontend/src/views/Workout.jsx | 26 +++++++++++++++--- frontend/src/views/Workout.test.jsx | 25 ++++++++++++++++++ 5 files changed, 98 insertions(+), 5 deletions(-) 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/views/Workout.jsx b/frontend/src/views/Workout.jsx index 89a474b..1eb5023 100644 --- a/frontend/src/views/Workout.jsx +++ b/frontend/src/views/Workout.jsx @@ -581,13 +581,31 @@ 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) 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 + el.scrollIntoView({ block: 'start' }) + }) + return () => cancel(frame) + }, [workoutView]) const total = setUnitsTotal(A.entries) const done = setsDoneActive(A) diff --git a/frontend/src/views/Workout.test.jsx b/frontend/src/views/Workout.test.jsx index 7a737e3..a195acb 100644 --- a/frontend/src/views/Workout.test.jsx +++ b/frontend/src/views/Workout.test.jsx @@ -185,6 +185,11 @@ async function addExerciseThroughSheets(ex = { id: 'added-exercise' }, cfg = { m await act(async () => { configCall[2](cfg) }) } +// The list's scroll-to-current waits for the next frame (linkedom has no rAF, so a 0 ms timer). +async function flushFrame() { + await act(async () => { await new Promise(resolve => setTimeout(resolve, 0)) }) +} + async function rerenderAt(cur) { mocks.S.active.cur = cur await act(async () => { root.render(React.createElement(Workout)) }) @@ -1014,11 +1019,31 @@ describe('workout list view', () => { it('opens at the current exercise instead of the top of the session (#224)', async () => { await mount([exercise('plain-bench', [true]), exercise('plain-row', [true]), exercise('plain-curl', [false])], 2, { workoutView: 'list' }) + // Not in the mount's own effect pass: App restores the route's scroll position in a frame + // of its own, so the list scrolls in the frame after it, or the restore would win. + expect(mocks.scrollCalls.length).toBe(0) + await flushFrame() expect(mocks.scrollCalls.length).toBe(1) expect(mocks.scrollCalls[0].node).toBe(units()[2]) expect(mocks.scrollCalls[0].node.classList.contains('cur')).toBe(true) }) + it('re-anchors on the current exercise when the layout changes between list and compact (QA C1)', async () => { + await mount([exercise('plain-bench', [true]), exercise('plain-row', [true]), exercise('plain-curl', [false])], 2, { workoutView: 'list' }) + await flushFrame() + mocks.scrollCalls.length = 0 + mocks.S.active.workoutView = 'compact' + await rerender() + await flushFrame() + expect(mocks.scrollCalls.length).toBe(1) + expect(mocks.scrollCalls[0].node.classList.contains('cur')).toBe(true) + // ...but not when "current" merely moves inside the open list (that was #224's rule). + mocks.scrollCalls.length = 0 + await rerenderAt(1) + await flushFrame() + expect(mocks.scrollCalls.length).toBe(0) + }) + it('stacks every exercise, labels each unit, and hides card navigation', async () => { await mount([exercise('plain-bench', [false, false]), exercise('plain-row', [false])], 0, { workoutView: 'list' }) From a54e4b6e8a8ea6f2483498961850fabc8562a7af Mon Sep 17 00:00:00 2001 From: Duarte Santos Date: Sat, 19 Sep 2026 21:24:12 +0200 Subject: [PATCH 2/2] =?UTF-8?q?List=20view=20clears=20the=20sticky=20heade?= =?UTF-8?q?r=20at=20its=20real=20height=20=E2=80=94=20a=20wrapped=20workou?= =?UTF-8?q?t=20name=20hid=20the=20current=20exercise's=20header=20row=20(Q?= =?UTF-8?q?A=20C27)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The scroll margin that keeps the current unit out from under the pinned header was a fixed 96 px, the height of a one-line header. The header renders the session name without truncation, so a descriptive routine name or a two-routine "A + B" session makes it two to five lines tall and the unit's label, Current badge and menu ended up underneath it. Workout.jsx measures the header right before it scrolls and hands the height to the list as --whdr-h; .wl-unit's scroll-margin-top uses it (safe area included, since the header's padding carries it) and keeps the one-line height as the fallback. Measured on the QA rig (scripts/opengym-qa/run/fix-list-scroll/measure.mjs, gap from header bottom to the current unit's top, tab-bar return with "Set current" on the 4th exercise; before: negative = covered): 320 px 8ch 12 | 42ch (143 px header) 12, before −51 | 62ch (165) 12, before −73 368 px 8ch 11 | 42ch (121) 12, before −29 | 62ch (143) 12, before −51 412 px 8ch 11 | 42ch (99) 12, before −7 | 62ch (121) 12, before −29 backfilled session, 42ch name, via ⋯ → Layout → List at 368 px: 16 --- frontend/src/index.css | 6 ++++-- frontend/src/views/Workout.jsx | 7 ++++++- frontend/src/views/Workout.test.jsx | 14 ++++++++++++++ 3 files changed, 24 insertions(+), 3 deletions(-) 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 1eb5023..dee908e 100644 --- a/frontend/src/views/Workout.jsx +++ b/frontend/src/views/Workout.jsx @@ -590,6 +590,7 @@ function ActiveWorkout() { // 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 schedule = callback => window.requestAnimationFrame @@ -602,6 +603,10 @@ function ActiveWorkout() { 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) @@ -960,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. */} -
+
{A.name}
{A.backfill ? fmtDate(A.d, true) : } · {t('{0} sets', done + '/' + total)}
diff --git a/frontend/src/views/Workout.test.jsx b/frontend/src/views/Workout.test.jsx index a195acb..d7f06ab 100644 --- a/frontend/src/views/Workout.test.jsx +++ b/frontend/src/views/Workout.test.jsx @@ -20,6 +20,7 @@ const mocks = vi.hoisted(() => { exConfigSheet: vi.fn(), toast: vi.fn(), scrollCalls: [], + headerHeight: 0, swapActiveWorkoutExercise: vi.fn(), menuSheet: vi.fn(), effortPickerSheet: vi.fn(), @@ -121,6 +122,10 @@ function installDom() { dom.Element.prototype.scrollIntoView = vi.fn(function (options) { mocks.scrollCalls.push({ node: this, options }) }) + // linkedom has no layout; the sticky workout header reports the height a test gives it. + Object.defineProperty(dom.HTMLElement.prototype, 'offsetHeight', { + configurable: true, get() { return this.classList.contains('whdr') ? mocks.headerHeight : 0 }, + }) globalThis.IS_REACT_ACT_ENVIRONMENT = true container = document.getElementById('root') root = createRoot(container) @@ -200,6 +205,7 @@ beforeEach(() => { mocks.timer = null mocks.work = null mocks.scrollCalls.length = 0 + mocks.headerHeight = 0 }) afterEach(async () => { @@ -1028,6 +1034,14 @@ describe('workout list view', () => { expect(mocks.scrollCalls[0].node.classList.contains('cur')).toBe(true) }) + it('clears the sticky header at its measured height, not a one-line guess (QA C27)', async () => { + mocks.headerHeight = 143 // a routine name that wraps to three lines at 368 px + await mount([exercise('plain-bench', [true]), exercise('plain-row', [true]), exercise('plain-curl', [false])], 2, { workoutView: 'list' }) + await flushFrame() + expect(mocks.scrollCalls.length).toBe(1) + expect(container.querySelector('.workout-list').style.getPropertyValue('--whdr-h')).toBe('143px') + }) + it('re-anchors on the current exercise when the layout changes between list and compact (QA C1)', async () => { await mount([exercise('plain-bench', [true]), exercise('plain-row', [true]), exercise('plain-curl', [false])], 2, { workoutView: 'list' }) await flushFrame()