Skip to content

Commit b9a137e

Browse files
authored
Revert "fix: hash link to new page focuses the correct element (#10856)"
This reverts commit 6261a87.
1 parent 6261a87 commit b9a137e

File tree

5 files changed

+17
-67
lines changed

5 files changed

+17
-67
lines changed

.changeset/big-pants-peel.md

Lines changed: 0 additions & 5 deletions
This file was deleted.

packages/kit/src/runtime/client/client.js

Lines changed: 14 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2800,42 +2800,22 @@ function reset_focus() {
28002800
autofocus.focus();
28012801
} else {
28022802
// Reset page selection and focus
2803-
if (location.hash && document.querySelector(location.hash)) {
2804-
const { x, y } = scroll_state();
2803+
// We try to mimic browsers' behaviour as closely as possible by targeting the
2804+
// first scrollable region, but unfortunately it's not a perfect match — e.g.
2805+
// shift-tabbing won't immediately cycle up from the end of the page on Chromium
2806+
// See https://html.spec.whatwg.org/multipage/interaction.html#get-the-focusable-area
2807+
const root = document.body;
2808+
const tabindex = root.getAttribute('tabindex');
2809+
2810+
root.tabIndex = -1;
2811+
// @ts-expect-error
2812+
root.focus({ preventScroll: true, focusVisible: false });
28052813

2806-
setTimeout(() => {
2807-
const history_state = history.state;
2808-
// Mimic the browsers' behaviour and set the sequential focus navigation
2809-
// starting point to the fragment identifier
2810-
location.replace(location.hash);
2811-
// but Firefox has a bug that sets the history state as null so we
2812-
// need to restore the history state
2813-
// See https://bugzilla.mozilla.org/show_bug.cgi?id=1199924
2814-
history.replaceState(history_state, '', location.hash);
2815-
2816-
// Scroll management has already happened earlier so we need to restore
2817-
// the scroll position after setting the sequential focus navigation starting point
2818-
scrollTo(x, y);
2819-
});
2814+
// restore `tabindex` as to prevent `root` from stealing input from elements
2815+
if (tabindex !== null) {
2816+
root.setAttribute('tabindex', tabindex);
28202817
} else {
2821-
// We try to mimic browsers' behaviour as closely as possible by targeting the
2822-
// first scrollable region, but unfortunately it's not a perfect match — e.g.
2823-
// shift-tabbing won't immediately cycle up from the end of the page on Chromium
2824-
// See https://html.spec.whatwg.org/multipage/interaction.html#get-the-focusable-area
2825-
const root = document.body;
2826-
const tabindex = root.getAttribute('tabindex');
2827-
2828-
root.tabIndex = -1;
2829-
// @ts-expect-error options.focusVisible is only supported in Firefox
2830-
// See https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/focus#browser_compatibility
2831-
root.focus({ preventScroll: true, focusVisible: false });
2832-
2833-
// restore `tabindex` as to prevent `root` from stealing input from elements
2834-
if (tabindex !== null) {
2835-
root.setAttribute('tabindex', tabindex);
2836-
} else {
2837-
root.removeAttribute('tabindex');
2838-
}
2818+
root.removeAttribute('tabindex');
28392819
}
28402820

28412821
// capture current selection, so we can compare the state after

packages/kit/test/apps/basics/src/routes/routing/focus/+page.svelte

Lines changed: 0 additions & 1 deletion
This file was deleted.

packages/kit/test/apps/basics/src/routes/routing/focus/a/+page.svelte

Lines changed: 0 additions & 4 deletions
This file was deleted.

packages/kit/test/apps/basics/test/cross-platform/client.test.js

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -806,31 +806,14 @@ test.describe('Routing', () => {
806806
await expect(page.getByRole('textbox')).toBeVisible();
807807
});
808808

809-
test('sequential focus navigation starting point is set correctly on navigation', async ({
809+
test('back button returns to previous route when previous route has been navigated to via hash anchor', async ({
810810
page,
811-
browserName
812-
}) => {
813-
const tab = browserName === 'webkit' ? 'Alt+Tab' : 'Tab';
814-
await page.goto('/routing/focus');
815-
await page.locator('[href="/routing/focus/a#p"]').click();
816-
await page.waitForURL('**/routing/focus/a#p');
817-
expect(await page.evaluate(() => (document.activeElement || {}).nodeName)).toBe('BODY');
818-
await page.keyboard.press(tab);
819-
await expect(page.locator('#button3')).toBeFocused();
820-
});
821-
822-
test('back button returns to previous route when previous route was navigated to via hash anchor', async ({
823-
page,
824-
clicknav,
825-
baseURL
811+
clicknav
826812
}) => {
827813
await page.goto('/routing/hashes/a');
828814

829815
await page.locator('[href="#hash-target"]').click();
830-
expect(page.url()).toBe(`${baseURL}/routing/hashes/a#hash-target`);
831-
832816
await clicknav('[href="/routing/hashes/b"]');
833-
expect(await page.textContent('h1')).toBe('b');
834817

835818
await expect(page.locator('h1')).toHaveText('b');
836819
await page.goBack();
@@ -845,13 +828,10 @@ test.describe('Routing', () => {
845828
await page.goto('/routing/hashes/a');
846829

847830
await clicknav('[href="#hash-target"]');
848-
expect(page.url()).toBe(`${baseURL}/routing/hashes/a#hash-target`);
849-
850831
await clicknav('[href="#replace-state"]');
851-
expect(page.url()).toBe(`${baseURL}/routing/hashes/a#replace-state`);
852832

853833
await page.goBack();
854-
expect(page.url()).toBe(`${baseURL}/routing/hashes/a`);
834+
expect(await page.url()).toBe(`${baseURL}/routing/hashes/a`);
855835
});
856836

857837
test('does not normalize external path', async ({ page, start_server }) => {

0 commit comments

Comments
 (0)