Scroll to top during navigation changes

This commit is contained in:
Alex Gleason 2025-06-01 07:32:33 -05:00
parent 98f3305aa1
commit ee42c84171
No known key found for this signature in database
GPG Key ID: 7211D1F99744FBB7
3 changed files with 20 additions and 0 deletions

View File

@ -2,10 +2,12 @@ import { BrowserRouter, Route, Routes } from "react-router-dom";
import Index from "./pages/Index"; import Index from "./pages/Index";
import NotFound from "./pages/NotFound"; import NotFound from "./pages/NotFound";
import { ScrollToTop } from "./components/ScrollToTop";
export function AppRouter() { export function AppRouter() {
return ( return (
<BrowserRouter> <BrowserRouter>
<ScrollToTop />
<Routes> <Routes>
<Route path="/" element={<Index />} /> <Route path="/" element={<Index />} />
{/* ADD ALL CUSTOM ROUTES ABOVE THE CATCH-ALL "*" ROUTE */} {/* ADD ALL CUSTOM ROUTES ABOVE THE CATCH-ALL "*" ROUTE */}

View File

@ -0,0 +1,12 @@
import { useEffect } from 'react';
import { useLocation } from 'react-router-dom';
export function ScrollToTop() {
const { pathname } = useLocation();
useEffect(() => {
window.scrollTo(0, 0);
}, [pathname]);
return null;
}

View File

@ -14,4 +14,10 @@ Object.defineProperty(window, 'matchMedia', {
removeEventListener: vi.fn(), removeEventListener: vi.fn(),
dispatchEvent: vi.fn(), dispatchEvent: vi.fn(),
})), })),
});
// Mock window.scrollTo
Object.defineProperty(window, 'scrollTo', {
writable: true,
value: vi.fn(),
}); });