From 65b0c30d8738992a5398072e1832e3f0612f3619 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oliver=20=C3=98rjavik?= Date: Mon, 16 Nov 2026 14:08:13 +0100 Subject: [PATCH 1/2] fix(a11y): move focus to the results after a page change Changing page scrolled to the top but left keyboard focus on the pagination button at the bottom, so keyboard and screen-reader users lost their place. Explore and Cookbook now focus the results heading, which stays mounted while the next page loads. Closes #81 --- .../src/features/cookbook/CookbookPage.tsx | 11 +++++++++- .../src/features/explore/ExplorePage.test.tsx | 22 +++++++++++++++++++ apps/web/src/features/explore/ExplorePage.tsx | 16 +++++++++++++- 3 files changed, 47 insertions(+), 2 deletions(-) diff --git a/apps/web/src/features/cookbook/CookbookPage.tsx b/apps/web/src/features/cookbook/CookbookPage.tsx index bf753b4..80bbcab 100644 --- a/apps/web/src/features/cookbook/CookbookPage.tsx +++ b/apps/web/src/features/cookbook/CookbookPage.tsx @@ -45,6 +45,7 @@ import './CookbookPage.css'; function CookbookPage() { const searchInputRef = useRef(null); + const resultsHeadingRef = useRef(null); const [searchParams, setSearchParams] = useSearchParams(); @@ -237,6 +238,9 @@ function CookbookPage() { left: 0, behavior: 'auto', }); + + // Move keyboard focus to the new results, as in ExplorePage. + resultsHeadingRef.current?.focus({ preventScroll: true }); } async function handleRecipeSaveChange(recipe: RecipeSummary, saved: boolean) { @@ -358,7 +362,12 @@ function CookbookPage() { className="cookbook-page__results" aria-labelledby="cookbook-results-heading" > -

+

Cookbook recipe results

diff --git a/apps/web/src/features/explore/ExplorePage.test.tsx b/apps/web/src/features/explore/ExplorePage.test.tsx index 9513503..e661d72 100644 --- a/apps/web/src/features/explore/ExplorePage.test.tsx +++ b/apps/web/src/features/explore/ExplorePage.test.tsx @@ -79,6 +79,28 @@ describe('Explore API integration', () => { ).toBeTruthy(); expect(screen.queryByRole('option', { name: 'Trending' })).toBeNull(); }); + it('moves focus to the results after a page change', async () => { + mockRecipeFetch((_operation, variables) => { + const page = (variables.input as { page: number }).page; + return { + data: { + recipes: apiPage([{ ...apiRecipe, title: `Page ${page} recipe` }], { + page, + totalPages: 2, + totalCount: 7, + }), + }, + }; + }); + mount('/'); + await screen.findByRole('heading', { name: 'Page 1 recipe' }); + const user = userEvent.setup(); + await user.click(screen.getAllByRole('button', { name: 'Next page' })[0]!); + await screen.findByRole('heading', { name: 'Page 2 recipe' }); + expect(document.activeElement).toBe( + screen.getByRole('heading', { name: 'Recipe results' }), + ); + }); it('commits search to the URL and resets the requested page', async () => { const requests: unknown[] = []; mockRecipeFetch((_operation, variables) => { diff --git a/apps/web/src/features/explore/ExplorePage.tsx b/apps/web/src/features/explore/ExplorePage.tsx index f5e486c..63d3633 100644 --- a/apps/web/src/features/explore/ExplorePage.tsx +++ b/apps/web/src/features/explore/ExplorePage.tsx @@ -49,6 +49,7 @@ type SaveFeedback = { function ExplorePage() { const searchInputRef = useRef(null); + const resultsHeadingRef = useRef(null); const [searchParams, setSearchParams] = useSearchParams(); @@ -214,6 +215,14 @@ function ExplorePage() { left: 0, behavior: 'auto', }); + + /* + * The pagination buttons sit below the results. Without this, keyboard + * and screen-reader users would stay at the bottom of a page they can no + * longer see. The heading stays mounted while the next page loads, and + * the result count after it is announced as a live region. + */ + resultsHeadingRef.current?.focus({ preventScroll: true }); } async function handleRecipeSaveChange(recipe: RecipeSummary, saved: boolean) { @@ -324,7 +333,12 @@ function ExplorePage() { className="explore-page__results" aria-labelledby="explore-results-heading" > -

+

Recipe results

From ac464acc1ac14f1c1cf34efb9ffc07a587d534ca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oliver=20=C3=98rjavik?= Date: Mon, 16 Nov 2026 19:07:17 +0100 Subject: [PATCH 2/2] fix(a11y): focus the visible result count after a page change MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Review on #91: focusing the sr-only heading left sighted keyboard users without a visible focus ring, and the result count (role=status) was unmounted while the next page loaded, so the update might not be announced. Explore and Cookbook now keep the count mounted (showing "Loading recipes…" while loading) and move focus to it; it is visible text, so the global :focus-visible ring shows. Refs #81 --- .../src/features/cookbook/CookbookPage.tsx | 43 ++++++++++++------- .../src/features/explore/ExplorePage.test.tsx | 8 ++-- apps/web/src/features/explore/ExplorePage.tsx | 43 ++++++++++++------- 3 files changed, 59 insertions(+), 35 deletions(-) diff --git a/apps/web/src/features/cookbook/CookbookPage.tsx b/apps/web/src/features/cookbook/CookbookPage.tsx index 80bbcab..2e22b67 100644 --- a/apps/web/src/features/cookbook/CookbookPage.tsx +++ b/apps/web/src/features/cookbook/CookbookPage.tsx @@ -45,7 +45,7 @@ import './CookbookPage.css'; function CookbookPage() { const searchInputRef = useRef(null); - const resultsHeadingRef = useRef(null); + const resultCountRef = useRef(null); const [searchParams, setSearchParams] = useSearchParams(); @@ -239,8 +239,8 @@ function CookbookPage() { behavior: 'auto', }); - // Move keyboard focus to the new results, as in ExplorePage. - resultsHeadingRef.current?.focus({ preventScroll: true }); + // Move keyboard focus to the visible result count, as in ExplorePage. + resultCountRef.current?.focus({ preventScroll: true }); } async function handleRecipeSaveChange(recipe: RecipeSummary, saved: boolean) { @@ -362,12 +362,7 @@ function CookbookPage() { className="cookbook-page__results" aria-labelledby="cookbook-results-heading" > -

+

Cookbook recipe results

@@ -379,11 +374,21 @@ function CookbookPage() { onFiltersChange={handleFiltersChange} /> - - {saveError &&

{saveError}

} - {!query.loading && !query.error && ( - <> -

+ {/* + * Always mounted, so screen readers announce the new count when a + * page change or filter finishes loading. Paging moves focus here: + * it is visible text, so keyboard users can see where they are. + */} +

+ {query.loading ? ( + 'Loading recipes…' + ) : query.error ? null : ( + <> {result.totalItems}{' '} {result.totalItems === 1 ? 'recipe' : 'recipes'} found @@ -393,8 +398,14 @@ function CookbookPage() { ? `. Page ${result.page} of ${result.pageCount}.` : '.'} -

- + + )} +

+ {/* Errors and retry only; loading is shown in the line above. */} + + {saveError &&

{saveError}

} + {!query.loading && !query.error && ( + <> {result.totalItems > 0 ? ( <>
diff --git a/apps/web/src/features/explore/ExplorePage.test.tsx b/apps/web/src/features/explore/ExplorePage.test.tsx index e661d72..7e4d156 100644 --- a/apps/web/src/features/explore/ExplorePage.test.tsx +++ b/apps/web/src/features/explore/ExplorePage.test.tsx @@ -94,12 +94,14 @@ describe('Explore API integration', () => { }); mount('/'); await screen.findByRole('heading', { name: 'Page 1 recipe' }); + const count = screen.getByText(/^7 recipes found/); const user = userEvent.setup(); await user.click(screen.getAllByRole('button', { name: 'Next page' })[0]!); await screen.findByRole('heading', { name: 'Page 2 recipe' }); - expect(document.activeElement).toBe( - screen.getByRole('heading', { name: 'Recipe results' }), - ); + // Focus is on the visible count, which stayed mounted while loading + // (same element) and now announces the new page. + expect(document.activeElement).toBe(count); + expect(count.textContent).toBe('7 recipes found. Page 2 of 2.'); }); it('commits search to the URL and resets the requested page', async () => { const requests: unknown[] = []; diff --git a/apps/web/src/features/explore/ExplorePage.tsx b/apps/web/src/features/explore/ExplorePage.tsx index 63d3633..69bcbf0 100644 --- a/apps/web/src/features/explore/ExplorePage.tsx +++ b/apps/web/src/features/explore/ExplorePage.tsx @@ -49,7 +49,7 @@ type SaveFeedback = { function ExplorePage() { const searchInputRef = useRef(null); - const resultsHeadingRef = useRef(null); + const resultCountRef = useRef(null); const [searchParams, setSearchParams] = useSearchParams(); @@ -219,10 +219,10 @@ function ExplorePage() { /* * The pagination buttons sit below the results. Without this, keyboard * and screen-reader users would stay at the bottom of a page they can no - * longer see. The heading stays mounted while the next page loads, and - * the result count after it is announced as a live region. + * longer see. The result count is visible (so the focus ring shows) and + * stays mounted while loading, so its new text is announced. */ - resultsHeadingRef.current?.focus({ preventScroll: true }); + resultCountRef.current?.focus({ preventScroll: true }); } async function handleRecipeSaveChange(recipe: RecipeSummary, saved: boolean) { @@ -333,12 +333,7 @@ function ExplorePage() { className="explore-page__results" aria-labelledby="explore-results-heading" > -

+

Recipe results

@@ -347,10 +342,21 @@ function ExplorePage() { onChange={handleBrowseStateChange} /> - - {!query.loading && !query.error && ( - <> -

+ {/* + * Always mounted, so screen readers announce the new count when a + * page change or filter finishes loading. Paging moves focus here: + * it is visible text, so keyboard users can see where they are. + */} +

+ {query.loading ? ( + 'Loading recipes…' + ) : query.error ? null : ( + <> {result.totalItems}{' '} {result.totalItems === 1 ? 'recipe' : 'recipes'} found @@ -360,8 +366,13 @@ function ExplorePage() { ? `. Page ${result.page} of ${result.pageCount}.` : '.'} -

- + + )} +

+ {/* Errors and retry only; loading is shown in the line above. */} + + {!query.loading && !query.error && ( + <> {result.totalItems > 0 ? ( <>