Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent client-side navigation for method="dialog" #9327

Merged
merged 4 commits into from
Dec 6, 2023
Merged
Show file tree
Hide file tree
Changes from all 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/rich-keys-rescue.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

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.
7 changes: 7 additions & 0 deletions packages/astro/components/ViewTransitions.astro
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,12 @@ const { fallback = 'animate' } = Astro.props;
let action = submitter?.getAttribute('formaction') ?? form.action ?? location.pathname;
const method = submitter?.getAttribute('formmethod') ?? form.method;

// the "dialog" method is a special keyword used within <dialog> elements
// https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#attr-fs-method
if (method === "dialog") {
return
}

const options: Options = { sourceElement: submitter ?? form };
if (method === 'get') {
const params = new URLSearchParams(formData as any);
Expand All @@ -113,6 +119,7 @@ const { fallback = 'animate' } = Astro.props;
} else {
options.formData = formData;
}

ev.preventDefault();
navigate(action, options);
});
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
import { ViewTransitions } from "astro:transitions";
---
<html>
<head>
<ViewTransitions />
</head>
<body>
<button id="open" onclick="modal.showModal()">Open Modal</button>
<dialog id="modal">
<form method="dialog">
<button id="close">Close</button>
</form>
</dialog>
</body>
</html>
14 changes: 14 additions & 0 deletions packages/astro/e2e/view-transitions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1074,4 +1074,18 @@ test.describe('View Transitions', () => {
await page.click('#three');
await expect(page).toHaveURL(expected);
});

test('Dialog using form with method of "dialog" should not trigger navigation', async ({ page, astro }) => {
await page.goto(astro.resolveUrl('/dialog'));

let requests = [];
page.on('request', request => requests.push(`${request.method()} ${request.url()}`));

await page.click('#open');
await expect(page.locator("dialog")).toHaveAttribute("open")
await page.click('#close');
await expect(page.locator("dialog")).not.toHaveAttribute("open")

expect(requests).toHaveLength(0)
});
});
Loading