Skip to content

Commit 246c788

Browse files
authored
Fix scroll position not resetting between routes (#2672)
1 parent 553a72b commit 246c788

2 files changed

Lines changed: 22 additions & 0 deletions

File tree

client/src/Routes.jsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { useWidget } from "./appReducer";
1111
import useFeatureFlag from "./hooks/useFeatureFlag";
1212
import SurveySnackbar from "./components/UI/SurveySnackbar";
1313
import AnnouncementSnackbar from "components/UI/AnnouncementSnackbar";
14+
import ScrollToTop from "./components/ScrollToTop";
1415

1516
const VerificationAdmin = lazy(() =>
1617
import("components/Admin/VerificationAdmin")
@@ -241,6 +242,7 @@ function AppWrapper() {
241242
wrap="nowrap"
242243
alignContent="stretch"
243244
spacing={0}
245+
id="app-container"
244246
sx={{
245247
color: "black",
246248
backgroundColor: "#fff",
@@ -249,6 +251,7 @@ function AppWrapper() {
249251
overflowX: "hidden",
250252
}}
251253
>
254+
<ScrollToTop />
252255
{isAlertLocation && <AnnouncementSnackbar />}
253256

254257
{isWidget ? null : <Header />}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { useEffect } from "react";
2+
import { useLocation } from "react-router-dom";
3+
4+
export default function ScrollToTop() {
5+
const { pathname } = useLocation();
6+
7+
useEffect(() => {
8+
// Scroll the window
9+
window.scrollTo(0, 0);
10+
11+
// Scroll the app container (Grid with fixed height)
12+
const appContainer = document.getElementById("app-container");
13+
if (appContainer) {
14+
appContainer.scrollTop = 0;
15+
}
16+
}, [pathname]);
17+
18+
return null;
19+
}

0 commit comments

Comments
 (0)