Skip to content

Commit dc74afc

Browse files
authored
Fixes an issue with persisted non-text input fields that have the focus during view transition navigation. (#10799)
* Fixes an issue with persisted non-text input fields that have the focus during view transition navigation. * better check
1 parent 347bdfe commit dc74afc

File tree

5 files changed

+67
-2
lines changed

5 files changed

+67
-2
lines changed

.changeset/itchy-donuts-destroy.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"astro": patch
3+
---
4+
5+
Fixes an issue with persisted non-text input fields that have the focus during view transition navigation.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
---
2+
import Layout from '../components/Layout.astro';
3+
---
4+
<Layout>
5+
<p id="one">Persist 1</p>
6+
<form transition:persist="form" action="/persist-2">
7+
<input type="text" name="name" />
8+
<input type="checkbox" />
9+
</form>
10+
<div id="test">test content</div>
11+
</Layout>
12+
<script>
13+
const input = document.querySelector<HTMLInputElement>("form input")!;
14+
input.focus();
15+
input.value = "some cool text";
16+
input.selectionStart=5;
17+
input.selectionEnd=9;
18+
19+
document.addEventListener('astro:after-swap', () => {
20+
const textInput = document.querySelector<HTMLInputElement>("form input:first-of-type")!;
21+
console.log(textInput === document.activeElement, textInput.value, textInput.selectionStart, textInput.selectionEnd);
22+
const checkBox = document.querySelector<HTMLInputElement>("form input:nth-of-type(2)")!;
23+
checkBox.checked = true;
24+
checkBox.focus();
25+
}, {once:true});
26+
</script>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
---
2+
import Layout from '../components/Layout.astro';
3+
---
4+
<Layout>
5+
<p id="two">Persist 2</p>
6+
<a id="click-persist-one" href="/persist-1">go to 3</a>
7+
<div transition:persist="form"/>
8+
<div id="test">test content</div>
9+
</Layout>
10+
<script>
11+
document.addEventListener('astro:after-swap', () => {
12+
const checkBox = document.querySelector<HTMLInputElement>("form input:nth-of-type(2)")!;
13+
console.log(checkBox === document.activeElement, checkBox.checked);
14+
}, {once:true});
15+
</script>
16+
17+

packages/astro/e2e/view-transitions.test.js

+17
Original file line numberDiff line numberDiff line change
@@ -1403,3 +1403,20 @@ test.describe('View Transitions', () => {
14031403
).toEqual(0);
14041404
});
14051405
});
1406+
1407+
test('transition:persist persists selection', async ({ page, astro }) => {
1408+
let text = "";
1409+
page.on('console', (msg) => {
1410+
text=msg.text();
1411+
});
1412+
await page.goto(astro.resolveUrl('/persist-1'));
1413+
await expect(page.locator('#one'), 'should have content').toHaveText('Persist 1');
1414+
// go to page 2
1415+
await page.press('input[name="name"]', 'Enter');
1416+
await expect(page.locator('#two'), 'should have content').toHaveText('Persist 2');
1417+
expect(text).toBe('true some cool text 5 9');
1418+
1419+
await page.goBack();
1420+
await expect(page.locator('#one'), 'should have content').toHaveText('Persist 1');
1421+
expect(text).toBe('true true');
1422+
});

packages/astro/src/transitions/router.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -305,8 +305,8 @@ async function updateDOM(
305305
activeElement instanceof HTMLInputElement ||
306306
activeElement instanceof HTMLTextAreaElement
307307
) {
308-
activeElement.selectionStart = start!;
309-
activeElement.selectionEnd = end!;
308+
if (typeof start === 'number') activeElement.selectionStart = start;
309+
if (typeof end === 'number') activeElement.selectionEnd = end;
310310
}
311311
}
312312
};

0 commit comments

Comments
 (0)