feat(web): skip link + route focus management + html lang sync (#52)
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
{
|
{
|
||||||
"common": { "yes": "Yes", "no": "No", "close": "Close", "loading": "Loading", "filter": "Filter…", "noMatches": "No matches", "language": "Language" },
|
"common": { "yes": "Yes", "no": "No", "close": "Close", "loading": "Loading", "filter": "Filter…", "noMatches": "No matches", "language": "Language", "skipToContent": "Skip to content" },
|
||||||
"nav": { "objects": "Objects", "vocabularies": "Vocabularies", "authorities": "Authorities", "fields": "Fields", "search": "Search", "collapseSidebar": "Collapse sidebar", "expandSidebar": "Expand sidebar" },
|
"nav": { "objects": "Objects", "vocabularies": "Vocabularies", "authorities": "Authorities", "fields": "Fields", "search": "Search", "collapseSidebar": "Collapse sidebar", "expandSidebar": "Expand sidebar" },
|
||||||
"auth": { "email": "Email", "password": "Password", "signIn": "Sign in", "signOut": "Sign out", "invalid": "Invalid email or password", "networkError": "Could not reach the server" },
|
"auth": { "email": "Email", "password": "Password", "signIn": "Sign in", "signOut": "Sign out", "invalid": "Invalid email or password", "networkError": "Could not reach the server" },
|
||||||
"objects": { "title": "Objects", "empty": "No objects yet", "loadError": "Could not load objects", "notFound": "Object not found", "prev": "Previous", "next": "Next", "of": "of", "new": "New object", "filter": "Filter objects…", "pageSize": "Per page", "columns": { "number": "Object №", "name": "Name", "visibility": "Visibility", "location": "Location", "count": "#", "updated": "Updated" }, "unknownRef": "(unknown)" },
|
"objects": { "title": "Objects", "empty": "No objects yet", "loadError": "Could not load objects", "notFound": "Object not found", "prev": "Previous", "next": "Next", "of": "of", "new": "New object", "filter": "Filter objects…", "pageSize": "Per page", "columns": { "number": "Object №", "name": "Name", "visibility": "Visibility", "location": "Location", "count": "#", "updated": "Updated" }, "unknownRef": "(unknown)" },
|
||||||
|
|||||||
@@ -33,3 +33,10 @@ test("switches language at runtime", async () => {
|
|||||||
});
|
});
|
||||||
expect(screen.getByTestId("title")).toHaveTextContent("Föremål");
|
expect(screen.getByTestId("title")).toHaveTextContent("Föremål");
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test("syncs document.documentElement.lang on language change", async () => {
|
||||||
|
await i18n.changeLanguage("sv");
|
||||||
|
expect(document.documentElement.lang).toBe("sv");
|
||||||
|
await i18n.changeLanguage("en");
|
||||||
|
expect(document.documentElement.lang).toBe("en");
|
||||||
|
});
|
||||||
|
|||||||
@@ -15,4 +15,13 @@ void i18n.use(initReactI18next).init({
|
|||||||
interpolation: { escapeValue: false },
|
interpolation: { escapeValue: false },
|
||||||
});
|
});
|
||||||
|
|
||||||
|
function syncHtmlLang(lng: string) {
|
||||||
|
if (typeof document !== "undefined") {
|
||||||
|
document.documentElement.lang = lng.startsWith("sv") ? "sv" : "en";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
i18n.on("languageChanged", syncHtmlLang);
|
||||||
|
syncHtmlLang(i18n.language);
|
||||||
|
|
||||||
export default i18n;
|
export default i18n;
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
{
|
{
|
||||||
"common": { "yes": "Ja", "no": "Nej", "close": "Stäng", "loading": "Laddar", "filter": "Filtrera…", "noMatches": "Inga träffar", "language": "Språk" },
|
"common": { "yes": "Ja", "no": "Nej", "close": "Stäng", "loading": "Laddar", "filter": "Filtrera…", "noMatches": "Inga träffar", "language": "Språk", "skipToContent": "Hoppa till innehåll" },
|
||||||
"nav": { "objects": "Föremål", "vocabularies": "Vokabulär", "authorities": "Auktoriteter", "fields": "Fält", "search": "Sök", "collapseSidebar": "Fäll ihop sidofältet", "expandSidebar": "Fäll ut sidofältet" },
|
"nav": { "objects": "Föremål", "vocabularies": "Vokabulär", "authorities": "Auktoriteter", "fields": "Fält", "search": "Sök", "collapseSidebar": "Fäll ihop sidofältet", "expandSidebar": "Fäll ut sidofältet" },
|
||||||
"auth": { "email": "E-post", "password": "Lösenord", "signIn": "Logga in", "signOut": "Logga ut", "invalid": "Fel e-post eller lösenord", "networkError": "Kunde inte nå servern" },
|
"auth": { "email": "E-post", "password": "Lösenord", "signIn": "Logga in", "signOut": "Logga ut", "invalid": "Fel e-post eller lösenord", "networkError": "Kunde inte nå servern" },
|
||||||
"objects": { "title": "Föremål", "empty": "Inga föremål ännu", "loadError": "Kunde inte ladda föremål", "notFound": "Föremålet hittades inte", "prev": "Föregående", "next": "Nästa", "of": "av", "new": "Nytt föremål", "filter": "Filtrera föremål…", "pageSize": "Per sida", "columns": { "number": "Föremålsnr", "name": "Namn", "visibility": "Synlighet", "location": "Plats", "count": "Antal", "updated": "Uppdaterad" }, "unknownRef": "(okänd)" },
|
"objects": { "title": "Föremål", "empty": "Inga föremål ännu", "loadError": "Kunde inte ladda föremål", "notFound": "Föremålet hittades inte", "prev": "Föregående", "next": "Nästa", "of": "av", "new": "Nytt föremål", "filter": "Filtrera föremål…", "pageSize": "Per sida", "columns": { "number": "Föremålsnr", "name": "Namn", "visibility": "Synlighet", "location": "Plats", "count": "Antal", "updated": "Uppdaterad" }, "unknownRef": "(okänd)" },
|
||||||
|
|||||||
@@ -21,6 +21,7 @@ function tree() {
|
|||||||
<Routes>
|
<Routes>
|
||||||
<Route element={<AppShell />}>
|
<Route element={<AppShell />}>
|
||||||
<Route path="/objects" element={<div>objects outlet</div>} />
|
<Route path="/objects" element={<div>objects outlet</div>} />
|
||||||
|
<Route path="/fields" element={<div>fields outlet</div>} />
|
||||||
</Route>
|
</Route>
|
||||||
<Route path="/login" element={<div>login page</div>} />
|
<Route path="/login" element={<div>login page</div>} />
|
||||||
</Routes>
|
</Routes>
|
||||||
@@ -36,6 +37,35 @@ test("shows active and disabled nav and renders the outlet", async () => {
|
|||||||
expect(screen.getByRole("link", { name: /fields/i })).toBeInTheDocument();
|
expect(screen.getByRole("link", { name: /fields/i })).toBeInTheDocument();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test("renders a skip link targeting the focusable main region", async () => {
|
||||||
|
renderApp(tree(), { route: "/objects" });
|
||||||
|
await screen.findByText("objects outlet");
|
||||||
|
|
||||||
|
expect(screen.getByRole("link", { name: /skip to content/i })).toHaveAttribute(
|
||||||
|
"href",
|
||||||
|
"#main-content",
|
||||||
|
);
|
||||||
|
|
||||||
|
const main = document.getElementById("main-content");
|
||||||
|
expect(main).toBeTruthy();
|
||||||
|
expect(main?.tabIndex).toBe(-1);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("moves focus to the main region on route change", async () => {
|
||||||
|
renderApp(tree(), { route: "/objects" });
|
||||||
|
await screen.findByText("objects outlet");
|
||||||
|
|
||||||
|
// Initial mount must NOT steal focus to main.
|
||||||
|
expect(document.activeElement).not.toBe(document.getElementById("main-content"));
|
||||||
|
|
||||||
|
await userEvent.click(screen.getByRole("link", { name: /fields/i }));
|
||||||
|
await screen.findByText("fields outlet");
|
||||||
|
|
||||||
|
await waitFor(() =>
|
||||||
|
expect(document.activeElement).toBe(document.getElementById("main-content")),
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
test("language switch toggles to Swedish", async () => {
|
test("language switch toggles to Swedish", async () => {
|
||||||
renderApp(tree(), { route: "/objects" });
|
renderApp(tree(), { route: "/objects" });
|
||||||
await userEvent.click(await screen.findByRole("button", { name: "SV" }));
|
await userEvent.click(await screen.findByRole("button", { name: "SV" }));
|
||||||
|
|||||||
@@ -1,4 +1,6 @@
|
|||||||
import { Outlet } from "react-router-dom";
|
import { useEffect, useRef } from "react";
|
||||||
|
import { Outlet, useLocation } from "react-router-dom";
|
||||||
|
import { useTranslation } from "react-i18next";
|
||||||
|
|
||||||
import { LangSwitch } from "./lang-switch";
|
import { LangSwitch } from "./lang-switch";
|
||||||
import { ThemeSwitch } from "./theme-switch";
|
import { ThemeSwitch } from "./theme-switch";
|
||||||
@@ -9,8 +11,27 @@ import { HeaderSearch } from "./header-search";
|
|||||||
import { UserMenu } from "./user-menu";
|
import { UserMenu } from "./user-menu";
|
||||||
|
|
||||||
export function AppShell() {
|
export function AppShell() {
|
||||||
|
const { t } = useTranslation();
|
||||||
|
const location = useLocation();
|
||||||
|
const mainRef = useRef<HTMLElement>(null);
|
||||||
|
const didMount = useRef(false);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (!didMount.current) {
|
||||||
|
didMount.current = true;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
mainRef.current?.focus();
|
||||||
|
}, [location.pathname]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="flex min-h-screen">
|
<div className="flex min-h-screen">
|
||||||
|
<a
|
||||||
|
href="#main-content"
|
||||||
|
className="sr-only focus:not-sr-only focus:absolute focus:left-2 focus:top-2 focus:z-50 focus:rounded-md focus:border focus:bg-background focus:px-3 focus:py-2 focus:text-sm focus:ring-3 focus:ring-ring/50"
|
||||||
|
>
|
||||||
|
{t("common.skipToContent")}
|
||||||
|
</a>
|
||||||
<Sidebar />
|
<Sidebar />
|
||||||
<BreadcrumbProvider>
|
<BreadcrumbProvider>
|
||||||
<div className="flex flex-1 flex-col">
|
<div className="flex flex-1 flex-col">
|
||||||
@@ -21,7 +42,7 @@ export function AppShell() {
|
|||||||
<LangSwitch />
|
<LangSwitch />
|
||||||
<UserMenu />
|
<UserMenu />
|
||||||
</header>
|
</header>
|
||||||
<main className="flex-1 overflow-hidden">
|
<main ref={mainRef} id="main-content" tabIndex={-1} className="flex-1 overflow-hidden outline-none">
|
||||||
<Outlet />
|
<Outlet />
|
||||||
</main>
|
</main>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
Reference in New Issue
Block a user