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
37 changes: 37 additions & 0 deletions apps/web/src/components/shell/AppShell/AppShell.css
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
18 changes: 17 additions & 1 deletion apps/web/src/components/shell/AppShell/AppShell.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,24 @@ import './AppShell.css';
function AppShell() {
return (
<div className="app-shell">
{/*
* 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.
*/}
<a
className="app-shell__skip-link"
href="#main-content"
onClick={(event) => {
// Focus main directly instead of adding #main-content to the URL.
event.preventDefault();
document.getElementById('main-content')?.focus();
}}
>
Skip to main content
</a>
<SiteHeader />
<main className="container">
<main id="main-content" className="container" tabIndex={-1}>
{/* Lazily loaded pages (App.tsx) suspend here; the header stays. */}
<Suspense fallback={<LoadingIndicator label="Loading page…" />}>
<Outlet />
Expand Down
4 changes: 2 additions & 2 deletions apps/web/src/components/shell/SiteHeader/SiteHeader.css
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
display: flex;
align-items: center;
gap: var(--space-8);
min-height: 5rem;
min-height: var(--site-header-height);
}

.brand {
Expand Down Expand Up @@ -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);
}

Expand Down
2 changes: 2 additions & 0 deletions apps/web/src/styles/tokens.css
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
81 changes: 81 additions & 0 deletions e2e/smoke.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
);
});