Skip to content

Open-source gym & body-weight tracker, now in the App Store (iPhone + Apple Watch) and as an Android APK. Plan routines, log workouts, see which muscles are trained, import from Strong/Hevy/FitNotes. Self-host it or use it on the phone alone.

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

Duarte Santos6e39fbd4b427
diff --git a/frontend/src/App.jsx b/frontend/src/App.jsx
index 6d2198472..28ff0eb63 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 397ccc4ab..39156cd9c 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 09038a7bf..3bdb4f673 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 89a474b90..1eb502353 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 7a737e387..a195acb52 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' })