Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
12 changes: 11 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,18 @@ export class Renderer {
const { scroll, keepfocus } = opts;

if (!keepfocus) {
const root = document.documentElement;
const tabindex = root.getAttribute('tabindex');

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

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