diff --git a/frontend/src/App.jsx b/frontend/src/App.jsx index 84d9d69..b30af2f 100644 --- a/frontend/src/App.jsx +++ b/frontend/src/App.jsx @@ -1,5 +1,5 @@ -import { useEffect } from 'react' -import { HashRouter, Routes, Route, Navigate, useNavigate, useLocation } from 'react-router-dom' +import { useEffect, useLayoutEffect, useRef } from 'react' +import { HashRouter, Routes, Route, Navigate, useNavigate, useLocation, useNavigationType } from 'react-router-dom' import { useStore } from './store/useStore.js' import { useUI } from './store/useUI.js' import { bindUI } from './components/ui.jsx' @@ -28,6 +28,9 @@ import Library from './views/Library.jsx' import Settings from './views/Settings.jsx' import Admin from './views/Admin.jsx' +// last known scrollY per route, so back-navigation can put the page where it was +const scrollPositions = new Map() + bindUI(useUI) // lets the shared controls open sheets without importing the store at module scope // theme === 'system' follows the OS/browser preference instead of a fixed choice. @@ -46,6 +49,7 @@ function applyPrefs(theme, accent) { function Shell() { const navigate = useNavigate() const loc = useLocation() + const navType = useNavigationType() const { S, user, ready } = useStore() const isGuest = useStore(s => s.isGuest()) const needsMobileOnboarding = useStore(s => s.needsMobileOnboarding) @@ -64,8 +68,27 @@ function Shell() { }, [S.theme, S.accent]) useEffect(() => { setLang(S.lang || 'en') }, [S.lang]) useEffect(() => { document.documentElement.lang = S.lang || 'en' }, [langV, S.lang]) - // every tab/route change starts at the top of the page - useEffect(() => { window.scrollTo(0, 0) }, [loc.pathname]) + // 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) + useEffect(() => { + const onScroll = () => { + // Modals pins the body while a sheet is open; scrollY is 0 then, not a position. + if (document.body.style.position === 'fixed') return + scrollPositions.set(pathRef.current, window.scrollY) + } + window.addEventListener('scroll', onScroll, { passive: true }) + return () => window.removeEventListener('scroll', onScroll) + }, []) + useLayoutEffect(() => { + pathRef.current = loc.pathname + if (navType !== 'POP') { window.scrollTo(0, 0); 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)) + return () => window.cancelAnimationFrame(frame) + }, [loc.pathname, navType]) // bound to the workout, not to the route — checking Stats mid-session keeps the screen on useWakeLock(!!S.active && S.keepAwake !== false) diff --git a/frontend/src/components/Heatmap.jsx b/frontend/src/components/Heatmap.jsx index 16f799d..03c6179 100644 --- a/frontend/src/components/Heatmap.jsx +++ b/frontend/src/components/Heatmap.jsx @@ -1,6 +1,7 @@ import { useEffect, useRef } from 'react' import { fmtVol, isoOf, todayISO, MONTHS } from '../lib/format.js' import { t } from '../lib/i18n.js' +import { tappable } from '../lib/use-sheet-keyboard.js' // GitHub-style activity heatmap, shaded by time trained per day. export default function Heatmap({ S, onDay }) { @@ -38,7 +39,7 @@ export default function Heatmap({ S, onDay }) { const cls = 'hm-c l' + level(a) + (key === todayISO() ? ' today' : '') + (day > today ? ' future' : '') cells.push(