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
5 changes: 5 additions & 0 deletions .changeset/honest-islands-flash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

[breaking] `tabindex="-1"` is no longer added to `<body>`; `<html>` only briefly receives it during navigation
18 changes: 17 additions & 1 deletion packages/kit/src/runtime/client/renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -369,8 +369,24 @@ export class Renderer {
const { scroll, keepfocus } = opts;

if (!keepfocus) {
// Reset page selection and focus
// We try to mimick browsers' behaviour as closely as possible by targeting the
// viewport, but unfortunately it's not a perfect match — e.g. shift-tabbing won't
// immediately cycle from the end of the page
// See https://html.spec.whatwg.org/multipage/interaction.html#get-the-focusable-area
const root = document.documentElement;
const tabindex = root.getAttribute('tabindex');

getSelection()?.removeAllRanges();
document.body.focus();
root.tabIndex = -1;
root.focus();

// restore `tabindex` as to prevent the document from stealing input from elements
if (tabindex !== null) {
root.setAttribute('tabindex', tabindex);
} else {
root.removeAttribute('tabindex');
}
}

// need to render the DOM before we can scroll to the rendered elements
Expand Down
3 changes: 0 additions & 3 deletions packages/kit/src/runtime/client/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,6 @@ export class Router {
this.enabled = true;
this.initialized = false;

// make it possible to reset focus
document.body.setAttribute('tabindex', '-1');

// keeping track of the history index in order to prevent popstate navigation events if needed
this.current_history_index = history.state?.['sveltekit:index'] ?? 0;

Expand Down
2 changes: 2 additions & 0 deletions packages/kit/test/apps/basics/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ test.describe.parallel('a11y', () => {
await page.keyboard.press('Tab');
expect(await page.evaluate(() => (document.activeElement || {}).nodeName)).toBe('A');
expect(await page.evaluate(() => (document.activeElement || {}).textContent)).toBe('a');

expect(await page.evaluate(() => document.documentElement.getAttribute('tabindex'))).toBe(null);
});

test('announces client-side navigation', async ({ page, clicknav, javaScriptEnabled }) => {
Expand Down
2 changes: 2 additions & 0 deletions sites/kit.svelte.dev/src/lib/search/SearchBox.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,9 @@
const scroll = -parseInt(document.body.style.top || '0');
document.body.style.position = '';
document.body.style.top = '';
document.body.tabIndex = -1;
document.body.focus();
document.body.removeAttribute('tabindex');
window.scrollTo(0, scroll);
}
}
Expand Down