-
Notifications
You must be signed in to change notification settings - Fork 0
fix(app): scroll to top on route change #5403
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'; | ||
| import { render } from '@testing-library/react'; | ||
| import { ThemeProvider, createTheme } from '@mui/material/styles'; | ||
| import { MemoryRouter, Routes, Route, Link } from 'react-router-dom'; | ||
| import userEvent from '@testing-library/user-event'; | ||
| import { RootLayout } from './RootLayout'; | ||
|
|
||
| vi.mock('../hooks', () => ({ | ||
| useAnalytics: () => ({ trackEvent: vi.fn(), trackPageview: vi.fn() }), | ||
| })); | ||
|
|
||
| vi.mock('./MastheadRule', () => ({ | ||
| MastheadRule: () => <div data-testid="masthead" />, | ||
| })); | ||
|
|
||
| vi.mock('./NavBar', () => ({ | ||
| NavBar: () => <div data-testid="navbar" />, | ||
| })); | ||
|
|
||
| vi.mock('./Footer', () => ({ | ||
| Footer: () => <div data-testid="footer" />, | ||
| })); | ||
|
|
||
| const theme = createTheme(); | ||
|
|
||
| function renderAt(initialPath: string) { | ||
| return render( | ||
| <ThemeProvider theme={theme}> | ||
| <MemoryRouter initialEntries={[initialPath]}> | ||
| <Routes> | ||
| <Route element={<RootLayout />}> | ||
| <Route | ||
| path="/" | ||
| element={ | ||
| <div> | ||
| <Link to="/legal">go-legal</Link> | ||
| <Link to="/about#section">go-about-hash</Link> | ||
| </div> | ||
| } | ||
| /> | ||
| <Route path="/legal" element={<div>legal-page</div>} /> | ||
| <Route path="/about" element={<div>about-page</div>} /> | ||
| </Route> | ||
| </Routes> | ||
| </MemoryRouter> | ||
| </ThemeProvider>, | ||
| ); | ||
| } | ||
|
|
||
| describe('RootLayout', () => { | ||
| let scrollSpy: ReturnType<typeof vi.spyOn>; | ||
|
|
||
| beforeEach(() => { | ||
| scrollSpy = vi.spyOn(window, 'scrollTo').mockImplementation(() => {}); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| scrollSpy.mockRestore(); | ||
| }); | ||
|
|
||
| it('scrolls to top when navigating to a new path (PUSH)', async () => { | ||
| const user = userEvent.setup(); | ||
| renderAt('/'); | ||
| scrollSpy.mockClear(); | ||
|
|
||
| await user.click(document.querySelector('a[href="/legal"]') as HTMLElement); | ||
|
|
||
| expect(scrollSpy).toHaveBeenCalledWith(0, 0); | ||
| }); | ||
|
|
||
| it('does not scroll to top when the destination has a hash anchor', async () => { | ||
| const user = userEvent.setup(); | ||
| renderAt('/'); | ||
| scrollSpy.mockClear(); | ||
|
|
||
| await user.click(document.querySelector('a[href="/about#section"]') as HTMLElement); | ||
|
|
||
| expect(scrollSpy).not.toHaveBeenCalledWith(0, 0); | ||
| }); | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,11 +21,15 @@ export function PlotsPage() { | |
| const { specsData, librariesData } = useAppData(); | ||
| const { homeStateRef, saveScrollPosition } = useHomeState(); | ||
|
|
||
| // Disable browser's automatic scroll restoration | ||
| // Disable browser's automatic scroll restoration so we can restore from | ||
| // our persisted state (homeStateRef.scrollY) instead. Restore on unmount | ||
| // so other routes get native back/forward behavior. | ||
| useEffect(() => { | ||
| if ('scrollRestoration' in history) { | ||
| history.scrollRestoration = 'manual'; | ||
| } | ||
| if (!('scrollRestoration' in history)) return; | ||
| history.scrollRestoration = 'manual'; | ||
| return () => { | ||
| history.scrollRestoration = 'auto'; | ||
| }; | ||
|
Comment on lines
+28
to
+32
|
||
| }, []); | ||
|
|
||
| const { trackPageview, trackEvent } = useAnalytics(); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same issue as PlotsPage: the effect cleanup hard-sets
history.scrollRestorationto'auto', which may override a pre-existing non-default value. Prefer saving the previous value on mount and restoring it on unmount (and keep the comment in sync with the behavior).