aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorArmand Philippot <git@armandphilippot.com>2023-12-16 18:40:32 +0100
committerArmand Philippot <git@armandphilippot.com>2023-12-16 19:43:42 +0100
commit9dfbd6e7e8749543b318fc7937501d102129bd1b (patch)
treef00d5fc744710b0cbecafa1ef477811611fc5176
parentbb2f79e09dd4776d611e4751ede1cbb43340fba0 (diff)
fix(layout): reset focus on route change
I mistakenly removed the `giveFocusToTop` on route change but Next router is still not fully accessible. It is important for keyboard users to be redirected at the top of the page when changing page so we need to reset the focus.
-rw-r--r--src/components/templates/layout/layout.tsx18
1 files changed, 16 insertions, 2 deletions
diff --git a/src/components/templates/layout/layout.tsx b/src/components/templates/layout/layout.tsx
index 2a6ac2e..16c9180 100644
--- a/src/components/templates/layout/layout.tsx
+++ b/src/components/templates/layout/layout.tsx
@@ -1,6 +1,13 @@
-import type { FC, ReactElement, ReactNode } from 'react';
+import {
+ useRef,
+ type FC,
+ type ReactElement,
+ type ReactNode,
+ useCallback,
+} from 'react';
import { useIntl } from 'react-intl';
import type { NextPageWithLayoutOptions } from '../../../types';
+import { useOnRouteChange } from '../../../utils/hooks';
import { ButtonLink, Main } from '../../atoms';
import styles from './layout.module.scss';
import { SiteFooter } from './site-footer';
@@ -34,12 +41,19 @@ export const Layout: FC<LayoutProps> = ({ children, isHome }) => {
}),
};
+ const topRef = useRef<HTMLSpanElement>(null);
const topId = 'top';
const mainId = 'main';
+ const giveFocusToTop = useCallback(() => {
+ if (topRef.current) topRef.current.focus();
+ }, []);
+
+ useOnRouteChange(giveFocusToTop);
+
return (
<>
- <span id={topId} />
+ <span id={topId} ref={topRef} tabIndex={-1} />
<noscript>
<div className={styles['noscript-spacing']} />
</noscript>