diff --git a/.changeset/itchy-donuts-destroy.md b/.changeset/itchy-donuts-destroy.md new file mode 100644 index 000000000000..655b27a26565 --- /dev/null +++ b/.changeset/itchy-donuts-destroy.md @@ -0,0 +1,5 @@ +--- +"astro": patch +--- + +Fixes an issue with persisted non-text input fields that have the focus during view transition navigation. diff --git a/packages/astro/e2e/fixtures/view-transitions/src/pages/persist-1.astro b/packages/astro/e2e/fixtures/view-transitions/src/pages/persist-1.astro new file mode 100644 index 000000000000..830667b3e635 --- /dev/null +++ b/packages/astro/e2e/fixtures/view-transitions/src/pages/persist-1.astro @@ -0,0 +1,26 @@ +--- +import Layout from '../components/Layout.astro'; +--- + +

Persist 1

+
+ + +
+
test content
+
+ diff --git a/packages/astro/e2e/fixtures/view-transitions/src/pages/persist-2.astro b/packages/astro/e2e/fixtures/view-transitions/src/pages/persist-2.astro new file mode 100644 index 000000000000..f79dedcd5ed6 --- /dev/null +++ b/packages/astro/e2e/fixtures/view-transitions/src/pages/persist-2.astro @@ -0,0 +1,17 @@ +--- +import Layout from '../components/Layout.astro'; +--- + +

Persist 2

+ go to 3 +
+
test content
+ + + + diff --git a/packages/astro/e2e/view-transitions.test.js b/packages/astro/e2e/view-transitions.test.js index b963bf2fd7f4..14cb8668d236 100644 --- a/packages/astro/e2e/view-transitions.test.js +++ b/packages/astro/e2e/view-transitions.test.js @@ -1403,3 +1403,20 @@ test.describe('View Transitions', () => { ).toEqual(0); }); }); + +test('transition:persist persists selection', async ({ page, astro }) => { + let text = ""; + page.on('console', (msg) => { + text=msg.text(); + }); + await page.goto(astro.resolveUrl('/persist-1')); + await expect(page.locator('#one'), 'should have content').toHaveText('Persist 1'); + // go to page 2 + await page.press('input[name="name"]', 'Enter'); + await expect(page.locator('#two'), 'should have content').toHaveText('Persist 2'); + expect(text).toBe('true some cool text 5 9'); + + await page.goBack(); + await expect(page.locator('#one'), 'should have content').toHaveText('Persist 1'); + expect(text).toBe('true true'); +}); diff --git a/packages/astro/src/transitions/router.ts b/packages/astro/src/transitions/router.ts index e0ffb4d7eef4..a5d06d5c769c 100644 --- a/packages/astro/src/transitions/router.ts +++ b/packages/astro/src/transitions/router.ts @@ -305,8 +305,8 @@ async function updateDOM( activeElement instanceof HTMLInputElement || activeElement instanceof HTMLTextAreaElement ) { - activeElement.selectionStart = start!; - activeElement.selectionEnd = end!; + if (typeof start === 'number') activeElement.selectionStart = start; + if (typeof end === 'number') activeElement.selectionEnd = end; } } };