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
29 changes: 29 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,33 @@
),
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;
}

/* 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 @@ -6,8 +6,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}>
<Outlet />
</main>
</div>
Expand Down
26 changes: 26 additions & 0 deletions e2e/smoke.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -356,3 +356,29 @@ 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);
});