List view lands on the current exercise through the ⋯ menu too — two scroll restores were undoing it (QA C1)

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
This commit is contained in:
Duarte Santos committed 2026-09-19 21:23:26 +02:00
1 parent 83b2a140d6
commit 6e39fbd4b4
5 files changed
+98 -5

No files matched your search

+9 -1
View File
@@ -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))
+7
View File
@@ -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])
+35
View File
@@ -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()
})
})
+22 -4
View File
@@ -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)
+25
View File
@@ -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' })