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.

Follow exercise chip selection and scrolling in list layouts

Space-Hermesf0b2dc27391f
diff --git a/frontend/src/views/Workout.jsx b/frontend/src/views/Workout.jsx
index d6c23d640..d79809369 100644
--- a/frontend/src/views/Workout.jsx
+++ b/frontend/src/views/Workout.jsx
@@ -1169,6 +1169,33 @@ function ActiveWorkout() {
// 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)
+ const [visibleExercise, setVisibleExercise] = useState(cur)
+ useEffect(() => { setVisibleExercise(cur) }, [cur, listMode, A.id])
+ useEffect(() => {
+ if (!listMode) return
+ let frame
+ const measure = () => {
+ frame = undefined
+ const line = (hdrRef.current?.getBoundingClientRect().bottom || 0) + 24
+ const rows = [...(listRef.current?.querySelectorAll('.wl-unit') || [])]
+ .map(el => ({ index: Number(el.dataset.exidx), rect: el.getBoundingClientRect() }))
+ .filter(({ rect }) => rect.height > 0 && rect.bottom > line && rect.top < window.innerHeight)
+ if (rows.length) setVisibleExercise(rows[0].index)
+ }
+ const onScroll = event => {
+ // Revealing a chip scrolls the header horizontally, not the exercise list.
+ if (event.target instanceof Node && hdrRef.current?.contains(event.target)) return
+ if (frame === undefined) frame = window.requestAnimationFrame(measure)
+ }
+ window.addEventListener('scroll', onScroll, { passive: true, capture: true })
+ window.addEventListener('resize', onScroll)
+ return () => {
+ window.removeEventListener('scroll', onScroll, true)
+ window.removeEventListener('resize', onScroll)
+ if (frame !== undefined) window.cancelAnimationFrame(frame)
+ }
+ }, [listMode, A.id])
+
useEffect(() => {
if (!listMode) return
const schedule = callback => window.requestAnimationFrame
@@ -1889,11 +1916,10 @@ function ActiveWorkout() {
const still = typeof window.matchMedia === 'function' && window.matchMedia('(prefers-reduced-motion: reduce)').matches
el.scrollIntoView(glide && !still ? { block: 'start', behavior: 'smooth' } : { block: 'start' })
}
- // A tap on a chip at the top (#323). Cards and Focus show one unit, so the chip makes it the one
- // on screen: the marker moves because you asked it to, never on its own. The list already shows
- // every unit, so there the chip only scrolls to it and leaves "Current" where it was, the same
- // as scrolling there by hand; the frame wait is the one opening the list uses (above).
+ // Explicit chip selection moves the marker in every view; list views also reveal the unit.
const pickChip = chip => {
+ setVisibleExercise(chip.unit[0])
+ if (!chip.unit.includes(cur)) update(s => { if (s.active) s.active.cur = chip.unit[0] })
if (listMode) {
// A folded unit opens as you jump to it: you tapped it to see it.
if (collapsed.has(chip.key)) setUnitOpen(chip.key, true)
@@ -1902,11 +1928,9 @@ function ActiveWorkout() {
else window.setTimeout(go, 0)
return
}
- if (chip.unit.includes(cur)) return
- update(s => { if (s.active) s.active.cur = chip.unit[0] })
}
const chipRow = wc.exerciseChips && A.entries.length > 0
- ? <WorkoutChips entries={A.entries} cur={cur} onPick={pickChip} />
+ ? <WorkoutChips entries={A.entries} cur={listMode ? visibleExercise : cur} onPick={pickChip} />
: null
// Hardware keys (issue #133, lib/workout-keys.js): Space or Enter ticks the next set, ← and →
diff --git a/frontend/src/views/Workout.keys.test.jsx b/frontend/src/views/Workout.keys.test.jsx
index dfaf91c53..d419a30b1 100644
--- a/frontend/src/views/Workout.keys.test.jsx
+++ b/frontend/src/views/Workout.keys.test.jsx
@@ -100,6 +100,39 @@ describe('workout keys (issue #133)', () => {
expect(doneOf(0).concat(doneOf(1), doneOf(2))).toEqual([false, false, false])
})
+ it.each(['compact', 'list', 'cards', 'focus'])('chip selection moves the highlight without logging sets in %s', workoutView => {
+ renderWorkout([entry('1001', [false]), entry('1002', [false], { sg: 'pair' }), entry('1003', [false], { sg: 'pair' })], 0, { workoutView })
+ const chips = () => [...container.querySelectorAll('.wchip')]
+ expect(chips()[0].getAttribute('aria-current')).toBe('step')
+ act(() => chips()[1].click())
+ expect(active().cur).toBe(1)
+ expect(chips()[1].getAttribute('aria-current')).toBe('step')
+ expect(chips()[0].hasAttribute('aria-current')).toBe(false)
+ act(() => chips()[0].click())
+ expect(active().cur).toBe(0)
+ expect(chips()[0].classList.contains('cur')).toBe(true)
+ expect(doneOf(0).concat(doneOf(1), doneOf(2))).toEqual([false, false, false])
+ })
+
+ it.each(['compact', 'list'])('scrolling follows the visible exercise without changing workout progress in %s', workoutView => {
+ renderWorkout([entry('1001', [false]), entry('1002', [false]), entry('1003', [false])], 0, { workoutView })
+ const rows = [...container.querySelectorAll('.wl-unit')]
+ const rect = (top, bottom) => ({ top, bottom, height: bottom - top })
+ container.querySelector('.whdr').getBoundingClientRect = () => rect(0, 100)
+ let tops = [-200, 110, 450]
+ rows.forEach((row, i) => { row.getBoundingClientRect = () => rect(tops[i], tops[i] + 300) })
+ act(() => { window.dispatchEvent(new Event('scroll')); vi.advanceTimersByTime(50) })
+ expect(container.querySelector('.wchip[aria-current]').dataset.unitKey).toBe('1')
+ tops = [-550, -240, 110]
+ act(() => { window.dispatchEvent(new Event('scroll')); vi.advanceTimersByTime(50) })
+ expect(container.querySelector('.wchip[aria-current]').dataset.unitKey).toBe('2')
+ expect(active().cur).toBe(0)
+ expect(doneOf(0).concat(doneOf(1), doneOf(2))).toEqual([false, false, false])
+ tops = [110, 450, 790]
+ act(() => { window.dispatchEvent(new Event('scroll')); vi.advanceTimersByTime(50) })
+ expect(container.querySelector('.wchip[aria-current]').dataset.unitKey).toBe('0')
+ })
+
it('in the list, an arrow that moves the current exercise brings it into view', () => {
const scrolled = []
const original = Element.prototype.scrollIntoView