diff --git a/apps/web/src/components/shell/AppShell/AppShell.css b/apps/web/src/components/shell/AppShell/AppShell.css index e945a95..4c1b55a 100644 --- a/apps/web/src/components/shell/AppShell/AppShell.css +++ b/apps/web/src/components/shell/AppShell/AppShell.css @@ -9,4 +9,41 @@ ), var(--color-bg-page); } + + /* Off-screen until it receives keyboard focus, then shown top left. */ + .app-shell__skip-link { + position: absolute; + z-index: var(--z-modal); + top: var(--space-2); + left: var(--space-2); + + padding: var(--space-2) var(--space-4); + + border-radius: var(--radius-md); + + color: var(--color-text-inverse); + background-color: var(--color-text-primary); + + font-weight: var(--font-weight-semibold); + text-decoration: none; + + transform: translateY(calc(-100% - var(--space-4))); + } + + .app-shell__skip-link:focus { + transform: none; + } + + /* + * Focusing main scrolls it into view. On a scrolled-down page it would + * otherwise end up behind the sticky header, hiding its first control. + */ + .app-shell main { + scroll-margin-top: calc(var(--site-header-height) + var(--space-2)); + } + + /* Main is only focused as a skip-link or navigation target, not typed in. */ + .app-shell main:focus { + outline: none; + } } diff --git a/apps/web/src/components/shell/AppShell/AppShell.tsx b/apps/web/src/components/shell/AppShell/AppShell.tsx index d5d7c9b..d426645 100644 --- a/apps/web/src/components/shell/AppShell/AppShell.tsx +++ b/apps/web/src/components/shell/AppShell/AppShell.tsx @@ -8,8 +8,24 @@ import './AppShell.css'; function AppShell() { return (
+ {/* + * First focusable element: lets keyboard users skip the header. + * Hidden until focused (AppShell.css). Main is focusable (tabIndex -1) + * so focus actually moves there, not only the scroll position. + */} + { + // Focus main directly instead of adding #main-content to the URL. + event.preventDefault(); + document.getElementById('main-content')?.focus(); + }} + > + Skip to main content + -
+
{/* Lazily loaded pages (App.tsx) suspend here; the header stays. */} }> diff --git a/apps/web/src/components/shell/SiteHeader/SiteHeader.css b/apps/web/src/components/shell/SiteHeader/SiteHeader.css index 9dabf39..cb557d2 100644 --- a/apps/web/src/components/shell/SiteHeader/SiteHeader.css +++ b/apps/web/src/components/shell/SiteHeader/SiteHeader.css @@ -12,7 +12,7 @@ display: flex; align-items: center; gap: var(--space-8); - min-height: 5rem; + min-height: var(--site-header-height); } .brand { @@ -112,7 +112,7 @@ .site-header__inner { flex-wrap: wrap; gap: 0; - min-height: 5rem; + min-height: var(--site-header-height); padding-block: var(--space-3); } diff --git a/apps/web/src/styles/tokens.css b/apps/web/src/styles/tokens.css index 202c5a4..aa144d5 100644 --- a/apps/web/src/styles/tokens.css +++ b/apps/web/src/styles/tokens.css @@ -219,6 +219,8 @@ /* Layering */ --z-dropdown: 100; --z-sticky: 200; + /* Height of the sticky site header; content scrolled to must clear it. */ + --site-header-height: 5rem; --z-overlay: 300; --z-modal: 400; diff --git a/e2e/smoke.spec.ts b/e2e/smoke.spec.ts index 7ea6b98..251bcfb 100644 --- a/e2e/smoke.spec.ts +++ b/e2e/smoke.spec.ts @@ -394,3 +394,84 @@ test('keeps recipe-specific not-found handling separate from the global 404', as page.getByRole('heading', { name: 'Page not found' }), ).not.toBeVisible(); }); + +test('skip link is the first tab stop, visible on focus, and moves focus to main', async ({ + page, +}) => { + await page.setViewportSize({ width: 1280, height: 800 }); + await page.goto('.'); + await expect( + page.getByRole('heading', { name: 'Explore Recipes' }), + ).toBeVisible(); + const skip = page.getByRole('link', { name: 'Skip to main content' }); + await expect(skip).not.toBeInViewport(); + await page.keyboard.press('Tab'); + await expect(skip).toBeFocused(); + await expect(skip).toBeInViewport(); + // Light text on the dark text colour: well above the 4.5:1 AA minimum. + await expect(skip).toHaveCSS('color', 'rgb(255, 255, 255)'); + await expect(skip).toHaveCSS('background-color', 'rgb(15, 23, 42)'); + await page.keyboard.press('Enter'); + await expect(page.locator('main#main-content')).toBeFocused(); + await expect(page).not.toHaveURL(/#main-content/); + // The next Tab continues inside the page content, not in the header. + await page.keyboard.press('Tab'); + expect( + await page.evaluate(() => Boolean(document.activeElement?.closest('main'))), + ).toBe(true); +}); + +test('after the skip link on a scrolled page, the next control is not under the header', async ({ + page, +}) => { + await page.setViewportSize({ width: 1280, height: 700 }); + const longRecipe: RecipeFieldsFragment = { + ...recipe, + description: 'A long description. '.repeat(40), + ingredients: Array.from({ length: 15 }, (_, index) => ({ + id: `ingredient-${index}`, + name: `Ingredient ${index + 1}`, + amount: index + 1, + unit: 'g', + note: null, + })), + instructions: Array.from({ length: 10 }, (_, 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}`); + await expect( + page.getByRole('heading', { name: recipe.title, level: 1 }), + ).toBeVisible(); + // Read the recipe, scrolled well past the header area. + await page.mouse.wheel(0, 2000); + await expect + .poll(() => page.evaluate(() => window.scrollY)) + .toBeGreaterThan(500); + + await page.keyboard.press('Tab'); + await expect( + page.getByRole('link', { name: 'Skip to main content' }), + ).toBeFocused(); + await page.keyboard.press('Enter'); + await expect(page.locator('main#main-content')).toBeFocused(); + await page.keyboard.press('Tab'); + // The first control in main is the recipe's Back button: focused, in view + // and fully below the sticky header. + const back = page.getByRole('button', { name: /Back/ }); + await expect(back).toBeFocused(); + await expect(back).toBeInViewport({ ratio: 1 }); + const header = (await page.locator('.site-header').boundingBox())!; + expect((await back.boundingBox())!.y).toBeGreaterThanOrEqual( + header.y + header.height, + ); +});