From c424a2a6a09181fc5cd0ff5968d86a9700d19cf4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oliver=20=C3=98rjavik?= Date: Mon, 16 Nov 2026 19:12:15 +0100 Subject: [PATCH 1/2] fix(a11y): move focus to the recipe when using Jump to recipe The button only scrolled: keyboard focus stayed on it, and smooth scrolling ignored prefers-reduced-motion. It now focuses the Ingredients heading after scrolling (so screen readers announce the destination and Tab continues there) and scrolls instantly when reduced motion is preferred. Closes #106 --- .../recipe-detail/RecipeDetailPage.test.tsx | 30 ++++++++++++++++++- .../recipe-detail/RecipeDetailPage.tsx | 17 +++++++++-- 2 files changed, 44 insertions(+), 3 deletions(-) diff --git a/apps/web/src/features/recipe-detail/RecipeDetailPage.test.tsx b/apps/web/src/features/recipe-detail/RecipeDetailPage.test.tsx index a0bf9fb..80fc713 100644 --- a/apps/web/src/features/recipe-detail/RecipeDetailPage.test.tsx +++ b/apps/web/src/features/recipe-detail/RecipeDetailPage.test.tsx @@ -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, @@ -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' }), + ); +}); diff --git a/apps/web/src/features/recipe-detail/RecipeDetailPage.tsx b/apps/web/src/features/recipe-detail/RecipeDetailPage.tsx index 6541630..261c0e2 100644 --- a/apps/web/src/features/recipe-detail/RecipeDetailPage.tsx +++ b/apps/web/src/features/recipe-detail/RecipeDetailPage.tsx @@ -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() { @@ -552,7 +563,9 @@ function RecipeDetailContent({ recipe }: RecipeDetailContentProps) { aria-labelledby="recipe-detail-ingredients-title" >
-

Ingredients

+

+ Ingredients +

{selectedServings}{' '} From 5f889933082179f5e2768a2b7251bcbcbc8027c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oliver=20=C3=98rjavik?= Date: Tue, 17 Nov 2026 00:50:57 +0100 Subject: [PATCH 2/2] fix(a11y): jump to the servings control so Tab stays in the recipe Focusing the Ingredients heading announced the right place, but the ingredient and step lists have nothing tabbable, so the next Tab left the recipe. Jump to recipe now lands on the visible "Adjust Servings" heading, directly above the ingredients; Tab continues to the servings buttons that scale them. The control exists once per layout and CSS hides the other, so the rendered copy is picked (falling back to the Ingredients heading). scroll-margin-top keeps it clear of the sticky header. Playwright, desktop and mobile: Enter on Jump focuses the heading in view below the header, then Tab reaches Decrease and Increase servings. --- .../recipe-detail/RecipeDetailPage.css | 3 + .../recipe-detail/RecipeDetailPage.test.tsx | 37 +++++++++-- .../recipe-detail/RecipeDetailPage.tsx | 38 ++++++++--- e2e/smoke.spec.ts | 65 +++++++++++++++++++ 4 files changed, 130 insertions(+), 13 deletions(-) diff --git a/apps/web/src/features/recipe-detail/RecipeDetailPage.css b/apps/web/src/features/recipe-detail/RecipeDetailPage.css index 059b14d..b47606d 100644 --- a/apps/web/src/features/recipe-detail/RecipeDetailPage.css +++ b/apps/web/src/features/recipe-detail/RecipeDetailPage.css @@ -230,6 +230,9 @@ display: flex; align-items: center; justify-content: space-between; + + /* "Jump to recipe" scrolls here; keep it clear of the sticky header. */ + scroll-margin-top: var(--space-16); gap: var(--space-4); padding: var(--space-4); diff --git a/apps/web/src/features/recipe-detail/RecipeDetailPage.test.tsx b/apps/web/src/features/recipe-detail/RecipeDetailPage.test.tsx index 80fc713..447d052 100644 --- a/apps/web/src/features/recipe-detail/RecipeDetailPage.test.tsx +++ b/apps/web/src/features/recipe-detail/RecipeDetailPage.test.tsx @@ -139,7 +139,7 @@ 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 () => { +function mockJumpScrolling() { // jsdom has no scrollIntoView; provide one for this test only. const originalScroll = Element.prototype.scrollIntoView; const scrollIntoView = vi.fn(); @@ -153,16 +153,45 @@ it('moves focus to the ingredients when jumping to the recipe, without motion if matches: query === '(prefers-reduced-motion: reduce)', })), ); + return scrollIntoView; +} +it('jumps to the visible servings control, and Tab continues to its buttons', async () => { + const scrollIntoView = mockJumpScrolling(); mockRecipeFetch(() => ({ data: { recipe: apiRecipe } })); mount(); await screen.findByRole('heading', { name: 'API soup' }); - await userEvent - .setup() - .click(screen.getByRole('button', { name: 'Jump to recipe' })); + // jsdom applies no CSS; mark the desktop copy as the rendered one. + const [mobile, desktop] = screen.getAllByRole('region', { + name: 'Adjust Servings', + }); + vi.spyOn(desktop!, 'getClientRects').mockReturnValue([ + new DOMRect(0, 0, 100, 100), + ] as unknown as DOMRectList); + const user = userEvent.setup(); + await user.click(screen.getByRole('button', { name: 'Jump to recipe' })); expect(scrollIntoView).toHaveBeenCalledWith({ behavior: 'auto', block: 'start', }); + expect(scrollIntoView.mock.contexts[0]).toBe(desktop); + expect(document.activeElement).toBe( + within(desktop!).getByRole('heading', { name: 'Adjust Servings' }), + ); + expect(mobile!.contains(document.activeElement)).toBe(false); + // Tab stays in the recipe: next come the servings buttons. + await user.tab(); + expect(document.activeElement).toBe( + within(desktop!).getByRole('button', { name: 'Decrease servings' }), + ); +}); +it('falls back to the ingredients heading when no servings control is rendered', async () => { + mockJumpScrolling(); + mockRecipeFetch(() => ({ data: { recipe: apiRecipe } })); + mount(); + await screen.findByRole('heading', { name: 'API soup' }); + await userEvent + .setup() + .click(screen.getByRole('button', { name: 'Jump to recipe' })); expect(document.activeElement).toBe( screen.getByRole('heading', { name: 'Ingredients' }), ); diff --git a/apps/web/src/features/recipe-detail/RecipeDetailPage.tsx b/apps/web/src/features/recipe-detail/RecipeDetailPage.tsx index 261c0e2..782f220 100644 --- a/apps/web/src/features/recipe-detail/RecipeDetailPage.tsx +++ b/apps/web/src/features/recipe-detail/RecipeDetailPage.tsx @@ -110,6 +110,8 @@ function RecipeDetailContent({ recipe }: RecipeDetailContentProps) { const [busy, setBusy] = useState(false); const [confirmDeleteOpen, setConfirmDeleteOpen] = useState(false); const [selectedServings, setSelectedServings] = useState(recipe.servings); + const mobileServingsRef = useRef(null); + const desktopServingsRef = useRef(null); const [confirmUnsaveOpen, setConfirmUnsaveOpen] = useState(false); const [feedback, setFeedback] = useState(null); const [showAllTags, setShowAllTags] = useState(false); @@ -169,21 +171,33 @@ function RecipeDetailContent({ recipe }: RecipeDetailContentProps) { } /* - * 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. + * The recipe starts at "Adjust Servings": it is directly above the + * ingredients and is the one control that acts on them. Jumping scrolls + * there and moves keyboard focus to its heading, so screen readers + * announce where the user landed and Tab continues to the servings + * buttons instead of leaving the recipe (the ingredient and step lists + * are text, read rather than tabbed through). The control exists twice, + * one copy per layout, and CSS hides the other; focusing a hidden element + * does nothing, so the rendered copy is picked. 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({ + const servings = [ + mobileServingsRef.current, + desktopServingsRef.current, + ].find((section) => section && section.getClientRects().length > 0); + const target = servings ?? document.getElementById('recipe-detail-recipe'); + target?.scrollIntoView({ behavior: reduceMotion ? 'auto' : 'smooth', block: 'start', }); - document - .getElementById('recipe-detail-ingredients-title') - ?.focus({ preventScroll: true }); + ( + servings?.querySelector('h2') ?? + document.getElementById('recipe-detail-ingredients-title') + )?.focus({ preventScroll: true }); } function openRating() { @@ -452,11 +466,14 @@ function RecipeDetailContent({ recipe }: RecipeDetailContentProps) {
-

Adjust Servings

+

+ Adjust Servings +

Ingredient amounts update automatically.

@@ -509,11 +526,14 @@ function RecipeDetailContent({ recipe }: RecipeDetailContentProps) {
-

Adjust Servings

+

+ Adjust Servings +

Ingredient amounts update automatically.

diff --git a/e2e/smoke.spec.ts b/e2e/smoke.spec.ts index aef8e07..1b06c40 100644 --- a/e2e/smoke.spec.ts +++ b/e2e/smoke.spec.ts @@ -364,3 +364,68 @@ test('keeps recipe-specific not-found handling separate from the global 404', as page.getByRole('heading', { name: 'Page not found' }), ).not.toBeVisible(); }); + +for (const viewport of [ + { name: 'desktop', width: 1280, height: 800 }, + { name: 'mobile', width: 390, height: 844 }, +]) { + test(`Jump to recipe keeps keyboard focus in the recipe (${viewport.name})`, async ({ + page, + }) => { + await page.setViewportSize(viewport); + // A recipe long enough that jumping has to scroll. + const longRecipe: RecipeFieldsFragment = { + ...recipe, + description: 'A long description. '.repeat(30), + ingredients: Array.from({ length: 12 }, (_, index) => ({ + id: `ingredient-${index}`, + name: `Ingredient ${index + 1}`, + amount: index + 1, + unit: 'g', + note: null, + })), + instructions: Array.from({ length: 8 }, (_, index) => ({ + step: index + 1, + text: 'Stir and simmer for a while. '.repeat(6), + })), + }; + await page.route('**/project2/api/graphql', async (route) => { + const body = route.request().postDataJSON() as { query?: string }; + await route.fulfill({ + json: body.query?.includes('currentUser') + ? { data: { currentUser: null } } + : { data: { recipe: longRecipe } }, + }); + }); + await page.goto(`recipes/${recipe.id}`); + const jump = page.getByRole('button', { name: 'Jump to recipe' }); + await jump.focus(); + await page.keyboard.press('Enter'); + + // Focus lands on the visible "Adjust Servings" heading, just above the + // ingredients, in view and not hidden behind the sticky header. + const heading = page + .getByRole('heading', { name: 'Adjust Servings' }) + .locator('visible=true'); + await expect(heading).toBeFocused(); + await expect(heading).toBeInViewport(); + const header = await page.locator('header').first().boundingBox(); + await expect + .poll(async () => (await heading.boundingBox())!.y) + .toBeGreaterThanOrEqual(header!.y + header!.height); + + // Tab continues inside the recipe, to the servings buttons. + await page.keyboard.press('Tab'); + await expect( + page + .getByRole('button', { name: 'Decrease servings' }) + .locator('visible=true'), + ).toBeFocused(); + await page.keyboard.press('Tab'); + await expect( + page + .getByRole('button', { name: 'Increase servings' }) + .locator('visible=true'), + ).toBeFocused(); + }); +}