Skip to content

Commit 3878a91

Browse files
doseoftednatemoo-re
andauthoredDec 6, 2023
Prevent client-side navigation for method="dialog" (#9327)
Co-authored-by: Nate Moore <natemoo-re@users.noreply.github.com>
1 parent dd24379 commit 3878a91

File tree

4 files changed

+42
-0
lines changed

4 files changed

+42
-0
lines changed
 

‎.changeset/rich-keys-rescue.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'astro': patch
3+
---
4+
5+
Fixes an edge case for `<form method="dialog">` when using View Transitions. Forms with `method="dialog"` no longer require an additional `data-astro-reload` attribute.

‎packages/astro/components/ViewTransitions.astro

+7
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,12 @@ const { fallback = 'animate' } = Astro.props;
104104
let action = submitter?.getAttribute('formaction') ?? form.action ?? location.pathname;
105105
const method = submitter?.getAttribute('formmethod') ?? form.method;
106106

107+
// the "dialog" method is a special keyword used within <dialog> elements
108+
// https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#attr-fs-method
109+
if (method === "dialog") {
110+
return
111+
}
112+
107113
const options: Options = { sourceElement: submitter ?? form };
108114
if (method === 'get') {
109115
const params = new URLSearchParams(formData as any);
@@ -113,6 +119,7 @@ const { fallback = 'animate' } = Astro.props;
113119
} else {
114120
options.formData = formData;
115121
}
122+
116123
ev.preventDefault();
117124
navigate(action, options);
118125
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
---
2+
import { ViewTransitions } from "astro:transitions";
3+
---
4+
<html>
5+
<head>
6+
<ViewTransitions />
7+
</head>
8+
<body>
9+
<button id="open" onclick="modal.showModal()">Open Modal</button>
10+
<dialog id="modal">
11+
<form method="dialog">
12+
<button id="close">Close</button>
13+
</form>
14+
</dialog>
15+
</body>
16+
</html>

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

+14
Original file line numberDiff line numberDiff line change
@@ -1074,4 +1074,18 @@ test.describe('View Transitions', () => {
10741074
await page.click('#three');
10751075
await expect(page).toHaveURL(expected);
10761076
});
1077+
1078+
test('Dialog using form with method of "dialog" should not trigger navigation', async ({ page, astro }) => {
1079+
await page.goto(astro.resolveUrl('/dialog'));
1080+
1081+
let requests = [];
1082+
page.on('request', request => requests.push(`${request.method()} ${request.url()}`));
1083+
1084+
await page.click('#open');
1085+
await expect(page.locator("dialog")).toHaveAttribute("open")
1086+
await page.click('#close');
1087+
await expect(page.locator("dialog")).not.toHaveAttribute("open")
1088+
1089+
expect(requests).toHaveLength(0)
1090+
});
10771091
});

0 commit comments

Comments
 (0)
Please sign in to comment.