Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
30 changes: 29 additions & 1 deletion apps/web/src/features/recipe-detail/RecipeDetailPage.test.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { render, screen, within } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { MemoryRouter, Route, Routes } from 'react-router-dom';
import { afterEach, beforeEach, expect, it, vi } from 'vitest';
import { afterEach, beforeEach, expect, it, onTestFinished, vi } from 'vitest';
import {
apiRecipe,
apiUser,
Expand Down Expand Up @@ -139,3 +139,31 @@ it('keeps a failed rating editable and refreshes the persisted rating after retr
await screen.findByRole('button', { name: 'Rate Recipe' }),
).toBeTruthy();
});
it('moves focus to the ingredients when jumping to the recipe, without motion if reduced', async () => {
// jsdom has no scrollIntoView; provide one for this test only.
const originalScroll = Element.prototype.scrollIntoView;
const scrollIntoView = vi.fn();
Element.prototype.scrollIntoView = scrollIntoView;
onTestFinished(() => {
Element.prototype.scrollIntoView = originalScroll;
});
vi.stubGlobal(
'matchMedia',
vi.fn((query: string) => ({
matches: query === '(prefers-reduced-motion: reduce)',
})),
);
mockRecipeFetch(() => ({ data: { recipe: apiRecipe } }));
mount();
await screen.findByRole('heading', { name: 'API soup' });
await userEvent
.setup()
.click(screen.getByRole('button', { name: 'Jump to recipe' }));
expect(scrollIntoView).toHaveBeenCalledWith({
behavior: 'auto',
block: 'start',
});
expect(document.activeElement).toBe(
screen.getByRole('heading', { name: 'Ingredients' }),
);
});
17 changes: 15 additions & 2 deletions apps/web/src/features/recipe-detail/RecipeDetailPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -168,11 +168,22 @@ function RecipeDetailContent({ recipe }: RecipeDetailContentProps) {
navigate(fallbackPath);
}

/*
* Scrolls to the ingredients and moves keyboard focus there, so screen
* readers announce where the user landed and Tab continues from the
* recipe. Smooth scrolling is skipped when the user prefers less motion.
*/
function handleJumpToRecipe() {
const reduceMotion = window.matchMedia?.(
'(prefers-reduced-motion: reduce)',
).matches;
document.getElementById('recipe-detail-recipe')?.scrollIntoView({
behavior: 'smooth',
behavior: reduceMotion ? 'auto' : 'smooth',
block: 'start',
});
document
.getElementById('recipe-detail-ingredients-title')
?.focus({ preventScroll: true });
}

function openRating() {
Expand Down Expand Up @@ -552,7 +563,9 @@ function RecipeDetailContent({ recipe }: RecipeDetailContentProps) {
aria-labelledby="recipe-detail-ingredients-title"
>
<div className="recipe-detail-page__section-heading">
<h2 id="recipe-detail-ingredients-title">Ingredients</h2>
<h2 id="recipe-detail-ingredients-title" tabIndex={-1}>
Ingredients
</h2>

<span>
{selectedServings}{' '}
Expand Down