From db16bbe808a69bfa3cedabfc4686cbe1a5e0348d Mon Sep 17 00:00:00 2001 From: Helene Lotsberg Date: Tue, 10 Nov 2026 18:00:38 +0100 Subject: [PATCH 1/4] add a skip link for keyboard navigation and translate aria-labels --- frontend/src/App.css | 31 ++++++++++++ frontend/src/App.tsx | 50 ++++++++++++++++++- frontend/src/components/header/Header.tsx | 2 +- frontend/src/components/sidebar/Sidebar.tsx | 4 +- frontend/src/i18n/locales/en/translation.json | 8 ++- frontend/src/i18n/locales/no/translation.json | 8 ++- 6 files changed, 96 insertions(+), 7 deletions(-) diff --git a/frontend/src/App.css b/frontend/src/App.css index e69de29b..aed689d9 100644 --- a/frontend/src/App.css +++ b/frontend/src/App.css @@ -0,0 +1,31 @@ +/* Hidden above the page until it gets focus with Tab */ +.skip-link { + position: absolute; + top: 0.5rem; + left: 0.5rem; + z-index: 1000; + padding: 0.75rem 1rem; + border-radius: 8px; + background-color: var(--dark-green); + color: var(--text-on-green); + font-family: var(--font-ui); + text-decoration: none; + transform: translateY(-200%); +} + +.skip-link:focus { + transform: translateY(0); + outline: 2px solid var(--dark-green); + outline-offset: 2px; +} + +/* The main content gets focus from the skip link, but should not show a ring + around the whole page. The first element inside shows its own ring on Tab. */ +.main-content:focus { + outline: none; +} + +/* Only used to move focus to the top of the page, so it should not show a ring */ +.page-start:focus { + outline: none; +} diff --git a/frontend/src/App.tsx b/frontend/src/App.tsx index b72cb664..b930ab61 100644 --- a/frontend/src/App.tsx +++ b/frontend/src/App.tsx @@ -1,11 +1,29 @@ -import { useState } from "react"; +import { useEffect, useRef, useState } from "react"; +import { useTranslation } from "react-i18next"; +import { useLocation } from "react-router-dom"; import "./App.css"; import Sidebar from "./components/sidebar/Sidebar"; import Header from "./components/header/Header"; import AppRoutes from "./AppRoutes.tsx"; function App() { + const { t } = useTranslation(); const [isSidebarOpen, setIsSidebarOpen] = useState(false); + const mainContentRef = useRef(null); + const pageStartRef = useRef(null); + + // When the user goes to another page, move focus to the start of the page, + // like when a normal web page loads. The next Tab then shows the skip link, + // so keyboard users can choose to jump to the content or go through the + // sidebar. Only the path counts: changing a filter or the page number + // changes the URL parameters, but should not move focus away from the filter. + const { pathname } = useLocation(); + const previousPathname = useRef(pathname); + useEffect(() => { + if (previousPathname.current === pathname) return; + previousPathname.current = pathname; + pageStartRef.current?.focus({ preventScroll: true }); + }, [pathname]); const openSidebar = () => { setIsSidebarOpen(true); @@ -17,9 +35,37 @@ function App() { return ( <> + {/* The start of the page. Focus is moved here when the page changes, + so the next Tab goes to the skip link */} +
+ {/* Lets keyboard users skip the sidebar. The app uses HashRouter, so a + normal "#main-content" link would change the route and show the 404 + page. Instead the default is prevented and focus is moved directly. */} + { + e.preventDefault(); + // preventScroll, so the page does not jump and hide the top of the + // content (like the search field) behind the sticky header + mainContentRef.current?.focus({ preventScroll: true }); + }} + > + {t("app.skipToContent")} + +
- + + {/* tabIndex -1 lets the skip link focus it, without adding it to the tab order */} +
+ +
); } diff --git a/frontend/src/components/header/Header.tsx b/frontend/src/components/header/Header.tsx index d0ebc0e0..8318c1a2 100644 --- a/frontend/src/components/header/Header.tsx +++ b/frontend/src/components/header/Header.tsx @@ -29,7 +29,7 @@ export default function Header({ isSidebarOpen, onOpenSidebar }: HeaderProps) { diff --git a/frontend/src/components/sidebar/Sidebar.tsx b/frontend/src/components/sidebar/Sidebar.tsx index 961a599e..8f02343c 100644 --- a/frontend/src/components/sidebar/Sidebar.tsx +++ b/frontend/src/components/sidebar/Sidebar.tsx @@ -23,7 +23,7 @@ export default function Sidebar({ isOpen, onCloseSidebar }: SidebarProps) { @@ -31,7 +31,7 @@ export default function Sidebar({ isOpen, onCloseSidebar }: SidebarProps) { -