Skip to content

Commit 0369b32

Browse files
hoangsonwwclaude
andcommitted
feat(frontend): reset scroll to top on every route navigation
Navigating between pages (e.g. Home -> Privacy Policy) could leave the user partway down the new page, because the browser kept the previous page's scroll position. Reset the scroll to the top from the CSSTransition `onEnter`, i.e. as the incoming page begins entering. With the out-in transition the old page fades out at its position, then the new page mounts at the top and fades in -- so there's no jarring mid-scroll jump; the existing fade/slide is the smoothness. Tied to the keyed transition (location.pathname), so in-page hash/anchor links are unaffected. 60 tests / 10 snapshots pass; eslint + prettier clean. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
1 parent 5c3b340 commit 0369b32

1 file changed

Lines changed: 13 additions & 0 deletions

File tree

frontend/src/App.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,18 @@ function AppLayout() {
7777
window.matchMedia("(prefers-reduced-motion: reduce)").matches;
7878
const timeout = reduceMotion ? 0 : { enter: 480, exit: 300 };
7979

80+
// Reset scroll to the top as the new page begins entering. With the
81+
// out-in transition the old page fades out at its scroll position, then
82+
// the incoming page mounts here at the top and fades in -- so navigating
83+
// (e.g. Home -> Privacy Policy) never drops the user mid-page, and the
84+
// existing fade/slide provides the smoothness rather than a jarring
85+
// mid-scroll jump. Tied to the keyed CSSTransition (location.pathname),
86+
// so in-page hash/anchor links are left alone.
87+
const scrollToTopOnEnter = () => {
88+
if (typeof window === "undefined") return;
89+
window.scrollTo(0, 0);
90+
};
91+
8092
return (
8193
<>
8294
{!hideNavbar && <Navbar />}
@@ -88,6 +100,7 @@ function AppLayout() {
88100
classNames="page"
89101
appear
90102
unmountOnExit
103+
onEnter={scrollToTopOnEnter}
91104
>
92105
<div ref={nodeRef} className="page-anim">
93106
<Routes location={location}>

0 commit comments

Comments
 (0)