fix(nav): Back restores the focus after a click on the page's blank area

The page region is <main tabIndex=-1>, so a background click focused it and the
Back restore took that for the reader's choice. A resting place is marked and
one predicate (focusIsUnclaimed) decides "the focus is nowhere" everywhere.
This commit is contained in:
2026-09-27 14:06:16 +02:00
parent 383c85231a
commit 9bedd96774
6 changed files with 45 additions and 11 deletions
@@ -10,6 +10,7 @@ import { afterEach, describe, expect, it } from "vitest";
import { act, cleanup, render, waitFor } from "@testing-library/react";
import { rememberNavPosition } from "./history";
import { navItemAttrs, useListNav } from "./listNav";
import { FOCUS_REST_ATTRS } from "./useFocusKeeper";
function List({ id, count, label }: { id: string; count: number; label?: string }) {
const ref = useRef<HTMLDivElement>(null);
@@ -146,6 +147,22 @@ describe("rememberNavPosition · Back focuses the item a view was left from", ()
});
});
// E28 review H1: the page region is `<main tabIndex={-1}>` (the skip link's target), so a mouse click
// on the page's blank area focuses it. The restore took that for a control the reader had chosen and
// gave up: Back left the focus on the page instead of on the item the view was opened from.
describe("rememberNavPosition · the focus resting on the page region", () => {
it("still restores when the page region holds the focus", async () => {
const { unmount } = render(<Page />);
const stamped = leaveFrom("b1", 4);
unmount();
const region = render(<main tabIndex={-1} data-testid="main" {...FOCUS_REST_ATTRS} />);
region.getByTestId("main").focus();
landOn(stamped);
render(<Page />);
await waitFor(() => expect(focused()).toBe("b1"));
});
});
describe("rememberNavPosition · the list it comes back to (deep-review E11 round 2)", () => {
// Discover: a title opened from "Recently added" and watched — the page's cache expires and
// "Continue watching" fills with that very title, a rail ABOVE. Found by its place, focus landed in