Skip to content

Commit

Permalink
Prevent client-side navigation for method="dialog" (#9327)
Browse files Browse the repository at this point in the history
Co-authored-by: Nate Moore <[email protected]>
  • Loading branch information
doseofted and natemoo-re authored Dec 6, 2023
1 parent dd24379 commit 3878a91
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 0 deletions.
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)
});
});

0 comments on commit 3878a91

Please sign in to comment.