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
34 changes: 27 additions & 7 deletions apps/web/src/features/cookbook/CookbookPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ import './CookbookPage.css';

function CookbookPage() {
const searchInputRef = useRef<HTMLInputElement>(null);
const resultCountRef = useRef<HTMLParagraphElement>(null);

const [searchParams, setSearchParams] = useSearchParams();

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -370,11 +374,21 @@ function CookbookPage() {
onFiltersChange={handleFiltersChange}
/>

<RecipeQueryStatus {...query} />
{saveError && <p role="alert">{saveError}</p>}
{!query.loading && !query.error && (
<>
<p className="cookbook-page__result-count" role="status">
{/*
* 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.
*/}
<p
ref={resultCountRef}
tabIndex={-1}
className="cookbook-page__result-count"
role="status"
>
{query.loading ? (
'Loading recipes…'
) : query.error ? null : (
<>
{result.totalItems}{' '}
{result.totalItems === 1 ? 'recipe' : 'recipes'} found
<span className="sr-only">
Expand All @@ -384,8 +398,14 @@ function CookbookPage() {
? `. Page ${result.page} of ${result.pageCount}.`
: '.'}
</span>
</p>

</>
)}
</p>
{/* Errors and retry only; loading is shown in the line above. */}
<RecipeQueryStatus {...query} loading={false} />
{saveError && <p role="alert">{saveError}</p>}
{!query.loading && !query.error && (
<>
{result.totalItems > 0 ? (
<>
<div className="cookbook-page__grid">
Expand Down
24 changes: 24 additions & 0 deletions apps/web/src/features/explore/ExplorePage.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
37 changes: 31 additions & 6 deletions apps/web/src/features/explore/ExplorePage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ type SaveFeedback = {

function ExplorePage() {
const searchInputRef = useRef<HTMLInputElement>(null);
const resultCountRef = useRef<HTMLParagraphElement>(null);

const [searchParams, setSearchParams] = useSearchParams();

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -333,10 +342,21 @@ function ExplorePage() {
onChange={handleBrowseStateChange}
/>

<RecipeQueryStatus {...query} />
{!query.loading && !query.error && (
<>
<p className="explore-page__result-count" role="status">
{/*
* 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.
*/}
<p
ref={resultCountRef}
tabIndex={-1}
className="explore-page__result-count"
role="status"
>
{query.loading ? (
'Loading recipes…'
) : query.error ? null : (
<>
{result.totalItems}{' '}
{result.totalItems === 1 ? 'recipe' : 'recipes'} found
<span className="sr-only">
Expand All @@ -346,8 +366,13 @@ function ExplorePage() {
? `. Page ${result.page} of ${result.pageCount}.`
: '.'}
</span>
</p>

</>
)}
</p>
{/* Errors and retry only; loading is shown in the line above. */}
<RecipeQueryStatus {...query} loading={false} />
{!query.loading && !query.error && (
<>
{result.totalItems > 0 ? (
<>
<div className="explore-page__grid">
Expand Down