diff --git a/docs/quality.md b/docs/quality.md index df075da1..0723109f 100644 --- a/docs/quality.md +++ b/docs/quality.md @@ -41,7 +41,7 @@ The app includes the following accessibility features: - **Page structure:** Semantic `header`, `nav`, `main` and `article` elements organise the content. Sidebar navigation has an accessible name, and the current page is marked with `aria-current="page"`. -- **Keyboard controls:** Navigation uses links, and actions use native buttons. Sidebar links, pagination, sorting controls and avatar options have explicit focus styles. Plant Match instructions are also shown when their control receives focus. The sort menu can be used with the arrow keys, Home, End and Tab, focus moves into the menu when it opens, and back to the sort button when it is closed with Escape or an option is chosen. +- **Keyboard controls:** Navigation uses links, and actions use native buttons. Sidebar links, pagination, sorting controls and avatar options have explicit focus styles. Plant Match instructions are also shown when their control receives focus. The sort menu can be used with the arrow keys, Home, End and Tab, focus moves into the menu when it opens, and back to the sort button when it is closed with Escape or an option is chosen. A "Skip to content" link is the first thing keyboard users reach on every page, so they can jump past the sidebar. When the user goes to another page, focus moves to the start of the page, like when a normal web page loads, so the next Tab shows the skip link again. The app uses HashRouter, so the link moves focus with JavaScript instead of a normal `#` link, which would have changed the route. - **Forms and images:** Search, filters and profile inputs have associated labels. Plant images use the plant name as alternative text, and profile and avatar images also include alternative text. @@ -57,7 +57,7 @@ The app includes the following accessibility features: - **Colour contrast:** We tested the contrast between our chosen colours to help ensure that text and interface elements are distinguishable from their backgrounds. -- **Language support:** The application supports both Norwegian and English, allowing users to choose their preferred language. +- **Language support:** The application supports both Norwegian and English, allowing users to choose their preferred language. The accessible names of buttons and landmarks, such as the menu buttons and the sidebar navigation, are translated too, so screen readers read them in the chosen language. - **Responsive layout:** We tested the application on both mobile and desktop devices. Manual testing helped us identify usability issues and make adjustments to the mobile layout. diff --git a/docs/tests.md b/docs/tests.md index f0aa11c6..2457cef2 100644 --- a/docs/tests.md +++ b/docs/tests.md @@ -111,6 +111,7 @@ The tests are in `frontend/src/tests`. Snapshot tests are used for the explore p - Tests the sidebar, including the title, navigation links to all pages, and that the language switcher and theme toggle are displayed. Also verifies that the sidebar closes when a link or the close button is clicked, that the open state is reflected in the `data-open` attribute, and that the navigation landmark is accessible and only the current page is marked as active. - `App.test.tsx` - Shows the right page for each route, and opens and closes the sidebar from the header. + - The skip link is the first thing a keyboard user reaches and moves focus to the main content without changing the route, and when the user goes to another page, focus moves to the start of the page so the next Tab goes to the skip link. - `ExplorePage.test.tsx` - Loading, error and empty results: a loading message, an error message when the query fails, and a message without page navigation when nothing matches. - Search, filters and sorting: the search text is only sent after the debounce delay, the chosen filters and sort order are sent in the query, the selected sort is visible and included in the sort button's accessible name, and the sort menu closes with Escape and with a click outside. @@ -284,8 +285,10 @@ Automatic testing only finds some accessibility problems, and the tests only che ## Manual testing -The group has tested the application manually in Chrome on Mac and on a mobile phone throughout development, using all pages and features on both. This testing led to adjustments such as improvements to the mobile layout. +The group has tested the application manually in Chrome on Mac and on a mobile phone throughout development, using all pages and features on both. This testing led to adjustments such as improvements to the mobile layout. Here are some examples: -**Keyboard only:** all pages have been tested with Tab, Shift+Tab, Enter and Escape only. All links and buttons can be reached and used, the focus is visible at all times, and in the sort menu, focus moves into the menu when it opens and back to the sort button when it closes. +**Keyboard only:** all pages have been tested with Tab, Shift+Tab, Enter and Escape only. All links and buttons can be reached and used, the focus is visible at all times, and in the sort menu, focus moves into the menu when it opens and back to the sort button when it closes. The first Tab on every page shows the "Skip to content" link, which jumps past the sidebar, and the same happens after going to another page. + +**Finding: the skip link hid the search field.** The first version scrolled the content up behind the sticky header, which the component tests did not catch, since jsdom does not scroll. The link now moves focus without scrolling. **Zoom to 200 %:** on a laptop screen, the app switches to the mobile layout at 200 % zoom. The sidebar is hidden and can be opened with the menu button in the header, so the layout adapts to the larger text instead of needing horizontal scrolling (WCAG 1.4.4 and 1.4.10). 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) { -