Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions docs/quality.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand All @@ -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.

Expand Down
7 changes: 5 additions & 2 deletions docs/tests.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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).
31 changes: 31 additions & 0 deletions frontend/src/App.css
Original file line number Diff line number Diff line change
@@ -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;
}
50 changes: 48 additions & 2 deletions frontend/src/App.tsx
Original file line number Diff line number Diff line change
@@ -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<HTMLDivElement>(null);
const pageStartRef = useRef<HTMLDivElement>(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);
Expand All @@ -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 */}
<div ref={pageStartRef} tabIndex={-1} className="page-start" />
{/* 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. */}
<a
href="#main-content"
className="skip-link"
onClick={(e) => {
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")}
</a>

<Sidebar isOpen={isSidebarOpen} onCloseSidebar={closeSidebar} />
<Header isSidebarOpen={isSidebarOpen} onOpenSidebar={openSidebar} />
<AppRoutes />

{/* tabIndex -1 lets the skip link focus it, without adding it to the tab order */}
<div
id="main-content"
className="main-content"
ref={mainContentRef}
tabIndex={-1}
>
<AppRoutes />
</div>
</>
);
}
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/components/header/Header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export default function Header({ isSidebarOpen, onOpenSidebar }: HeaderProps) {
<button
className="menu-button"
onClick={onOpenSidebar}
aria-label="Open sidebar"
aria-label={t("sidebar.openSidebar")}
>
☰
</button>
Expand Down
4 changes: 2 additions & 2 deletions frontend/src/components/sidebar/Sidebar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ export default function Sidebar({ isOpen, onCloseSidebar }: SidebarProps) {
<button
className="close-button"
onClick={onCloseSidebar}
aria-label="Close menu"
aria-label={t("sidebar.closeMenu")}
>
×
</button>

<h2 className="sidebar_title" id="sidebar-title">
{t("sidebar.title")}
</h2>
<nav className="sidebar__nav" aria-label="Sidebar navigation">
<nav className="sidebar__nav" aria-label={t("sidebar.navigationLabel")}>
<ul>
<li>
<NavLink
Expand Down
8 changes: 7 additions & 1 deletion frontend/src/i18n/locales/en/translation.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
{
"app": {
"skipToContent": "Skip to content"
},
"search": {
"button": "Search"
},
Expand All @@ -13,7 +16,10 @@
"explorePlants": "Explore Plants",
"profile": "Profile",
"title": "I <3 Plants",
"plantOwnerTest": "Plant Owner Test"
"plantOwnerTest": "Plant Owner Test",
"openSidebar": "Open sidebar",
"closeMenu": "Close menu",
"navigationLabel": "Sidebar navigation"
},
"plantMatchPage": {
"skipPlant": "Skip plant",
Expand Down
8 changes: 7 additions & 1 deletion frontend/src/i18n/locales/no/translation.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
{
"app": {
"skipToContent": "Hopp til innhold"
},
"search": {
"button": "Søk"
},
Expand All @@ -13,7 +16,10 @@
"explorePlants": "Utforsk Planter",
"profile": "Profil",
"title": "I <3 Plants",
"plantOwnerTest": "Planteeiertest"
"plantOwnerTest": "Planteeiertest",
"openSidebar": "Åpne sidemeny",
"closeMenu": "Lukk menyen",
"navigationLabel": "Navigasjon i sidemenyen"
},
"plantMatchPage": {
"skipPlant": "Hopp over planten",
Expand Down
35 changes: 34 additions & 1 deletion frontend/src/tests/App.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,44 @@ describe("App", () => {
const header = document.querySelector(".header");

expect(header).toHaveAttribute("data-sidebar-open", "false");
await user.click(screen.getByRole("button", { name: "Open sidebar" }));
await user.click(
screen.getByRole("button", { name: "sidebar.openSidebar" }),
);
expect(header).toHaveAttribute("data-sidebar-open", "true");

await user.click(screen.getByRole("link", { name: "sidebar.profile" }));
expect(await screen.findByText("profile page")).toBeInTheDocument();
expect(header).toHaveAttribute("data-sidebar-open", "false");
});

it("has a skip link first on the page that moves focus to the main content", async () => {
const user = userEvent.setup();
renderApp(ROUTES.plantMatch);

// The skip link is the first thing a keyboard user reaches
await user.tab();
const skipLink = screen.getByRole("link", { name: "app.skipToContent" });
expect(skipLink).toHaveFocus();

await user.keyboard("{Enter}");

expect(document.getElementById("main-content")).toHaveFocus();
// The page is still shown, so the link did not change the route
expect(screen.getByText("plant match page")).toBeInTheDocument();
});

it("moves focus to the start of the page when the user goes to another page", async () => {
const user = userEvent.setup();
renderApp(ROUTES.plantMatch);

await user.click(screen.getByRole("link", { name: "sidebar.myMatches" }));
expect(await screen.findByText("my matches page")).toBeInTheDocument();

// The next Tab goes to the skip link, so the user can choose to skip the
// sidebar or go through it, like on a page that was just loaded
await user.tab();
expect(
screen.getByRole("link", { name: "app.skipToContent" }),
).toHaveFocus();
});
});
4 changes: 3 additions & 1 deletion frontend/src/tests/Header.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ describe("Header", () => {
it("opens the sidebar with the menu button", async () => {
const onOpenSidebar = renderHeader(ROUTES.plantMatch);

await userEvent.click(screen.getByRole("button", { name: "Open sidebar" }));
await userEvent.click(
screen.getByRole("button", { name: "sidebar.openSidebar" }),
);

expect(onOpenSidebar).toHaveBeenCalledTimes(1);
});
Expand Down
4 changes: 2 additions & 2 deletions frontend/src/tests/Sidebar.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ describe("Sidebar", () => {
it("closes the sidebar when the close button is clicked", async () => {
const { user, onCloseSidebar } = renderSidebar();

await user.click(screen.getByRole("button", { name: "Close menu" }));
await user.click(screen.getByRole("button", { name: "sidebar.closeMenu" }));

expect(onCloseSidebar).toHaveBeenCalledTimes(1);
});
Expand Down Expand Up @@ -126,7 +126,7 @@ describe("Sidebar", () => {
renderSidebar();

expect(
screen.getByRole("navigation", { name: "Sidebar navigation" }),
screen.getByRole("navigation", { name: "sidebar.navigationLabel" }),
).toBeInTheDocument();
});

Expand Down
Loading