diff --git a/src/common/components/app/App.tsx b/src/common/components/app/App.tsx index 05e4a60b7..ba2f0573f 100644 --- a/src/common/components/app/App.tsx +++ b/src/common/components/app/App.tsx @@ -10,6 +10,7 @@ import theme from './theme'; import { GlobalNotificationProvider } from '../globalNotification/GlobalNotificationContext'; import GlobalNotification from '../globalNotification/GlobalNotification'; import { FeatureFlagsProvider } from '../featureFlags/FeatureFlagsContext'; +import ScrollToTop from '../scrollToTop/ScrollToTop'; import './app.scss'; import '../../../assets/styles/reset.css'; @@ -17,6 +18,7 @@ const queryClient = new QueryClient(); const App: React.FC> = () => ( + diff --git a/src/common/components/scrollToTop/ScrollToTop.tsx b/src/common/components/scrollToTop/ScrollToTop.tsx new file mode 100644 index 000000000..679019ff1 --- /dev/null +++ b/src/common/components/scrollToTop/ScrollToTop.tsx @@ -0,0 +1,15 @@ +import { useEffect } from 'react'; +import { useLocation } from 'react-router-dom'; + +/** + * Scroll to top of document when URL pathname changes + */ +export default function ScrollToTop() { + const { pathname } = useLocation(); + + useEffect(() => { + window.scrollTo(0, 0); + }, [pathname]); + + return null; +} diff --git a/src/domain/forms/MultipageForm.tsx b/src/domain/forms/MultipageForm.tsx index f66f9c553..23bc0e210 100644 --- a/src/domain/forms/MultipageForm.tsx +++ b/src/domain/forms/MultipageForm.tsx @@ -87,6 +87,7 @@ const MultipageForm: React.FC = ({ function handleStepChange(value: Action) { function changeStep() { + window.scrollTo(0, 0); dispatch(value); if (onStepChange) { onStepChange(); diff --git a/src/testUtils/render.tsx b/src/testUtils/render.tsx index 9b6de9c81..30d075783 100644 --- a/src/testUtils/render.tsx +++ b/src/testUtils/render.tsx @@ -44,6 +44,7 @@ const AllTheProviders = ({ children }: Props) => { const customRender = (ui: React.ReactElement, options: RenderOptions = {}, route = '/') => { window.history.pushState({}, 'Test page', route); + window.scrollTo = function () {}; return { user: userEvent.setup(), ...render(ui, {