diff --git a/apps/web/src/features/cookbook/CookbookPage.tsx b/apps/web/src/features/cookbook/CookbookPage.tsx index bf753b4..2e22b67 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 resultCountRef = useRef(null); const [searchParams, setSearchParams] = useSearchParams(); @@ -237,6 +238,9 @@ function CookbookPage() { left: 0, behavior: 'auto', }); + + // Move keyboard focus to the visible result count, as in ExplorePage. + resultCountRef.current?.focus({ preventScroll: true }); } async function handleRecipeSaveChange(recipe: RecipeSummary, saved: boolean) { @@ -370,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 @@ -384,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 9513503..7e4d156 100644 --- a/apps/web/src/features/explore/ExplorePage.test.tsx +++ b/apps/web/src/features/explore/ExplorePage.test.tsx @@ -79,6 +79,30 @@ 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 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' }); + // 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[] = []; mockRecipeFetch((_operation, variables) => { diff --git a/apps/web/src/features/explore/ExplorePage.tsx b/apps/web/src/features/explore/ExplorePage.tsx index f5e486c..69bcbf0 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 resultCountRef = 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 result count is visible (so the focus ring shows) and + * stays mounted while loading, so its new text is announced. + */ + resultCountRef.current?.focus({ preventScroll: true }); } async function handleRecipeSaveChange(recipe: RecipeSummary, saved: boolean) { @@ -333,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 @@ -346,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 ? ( <>