Skip to content

Commit

Permalink
Changed implementation to use ScrollToTop component instead of
Browse files Browse the repository at this point in the history
useScrollToTop hook and that scroll to top is done in each page
change instead of just forms.
  • Loading branch information
markohaarni committed Oct 12, 2023
1 parent 5665870 commit 499ae0b
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 27 deletions.
2 changes: 2 additions & 0 deletions src/common/components/app/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@ 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';

const queryClient = new QueryClient();

const App: React.FC<React.PropsWithChildren<unknown>> = () => (
<Router>
<ScrollToTop />
<Provider store={store}>
<QueryClientProvider client={queryClient}>
<ChakraProvider theme={theme}>
Expand Down
15 changes: 15 additions & 0 deletions src/common/components/scrollToTop/ScrollToTop.tsx
Original file line number Diff line number Diff line change
@@ -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;
}
24 changes: 0 additions & 24 deletions src/common/hooks/useScrollToTop.ts

This file was deleted.

4 changes: 1 addition & 3 deletions src/domain/forms/MultipageForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import Text from '../../common/components/text/Text';
import { createStepReducer } from './formStepReducer';
import { Action, ACTION_TYPE, StepperStep } from './types';
import { SKIP_TO_ELEMENT_ID } from '../../common/constants/constants';
import useScrollToTop from '../../common/hooks/useScrollToTop';

function LoadingIndicator({ loadingText }: { loadingText?: string }) {
return (
Expand Down Expand Up @@ -78,7 +77,6 @@ const MultipageForm: React.FC<Props> = ({
notificationText,
}) => {
const locale = useLocale();
const scrollToTop = useScrollToTop();

const stepReducer = createStepReducer(formSteps.length);
const initialState = {
Expand All @@ -89,7 +87,7 @@ const MultipageForm: React.FC<Props> = ({

function handleStepChange(value: Action) {
function changeStep() {
scrollToTop();
window.scrollTo(0, 0);
dispatch(value);
if (onStepChange) {
onStepChange();
Expand Down

0 comments on commit 499ae0b

Please sign in to comment.