Skip to content

Commit c421a3d

Browse files
authoredNov 29, 2023
Unflag view transitions form handling (#9225)
1 parent 4b8a424 commit c421a3d

File tree

2 files changed

+33
-27
lines changed

2 files changed

+33
-27
lines changed
 

‎.changeset/odd-rivers-happen.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'astro': major
3+
---
4+
5+
Removes the opt-in `handleForms` property for `<ViewTransitions />`. Form submissions are now handled by default and can be disabled by setting `data-astro-reload` on relevant `<form />` elements.

‎packages/astro/components/ViewTransitions.astro

+28-27
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,14 @@ type Fallback = 'none' | 'animate' | 'swap';
33
44
export interface Props {
55
fallback?: Fallback;
6+
/** @deprecated handleForms is enabled by default in Astro 4.0
7+
*
8+
* Set `data-astro-reload` on your form to opt-out of the default behavior.
9+
*/
610
handleForms?: boolean;
711
}
812
9-
const { fallback = 'animate', handleForms } = Astro.props;
13+
const { fallback = 'animate' } = Astro.props;
1014
---
1115

1216
<style is:global>
@@ -25,7 +29,6 @@ const { fallback = 'animate', handleForms } = Astro.props;
2529
</style>
2630
<meta name="astro-view-transitions-enabled" content="true" />
2731
<meta name="astro-view-transitions-fallback" content={fallback} />
28-
{handleForms ? <meta name="astro-view-transitions-forms" content="true" /> : ''}
2932
<script>
3033
import type { Options } from 'astro:transitions/client';
3134
import { supportsViewTransitions, navigate } from 'astro:transitions/client';
@@ -89,33 +92,31 @@ const { fallback = 'animate', handleForms } = Astro.props;
8992
});
9093
});
9194

92-
if (document.querySelector('[name="astro-view-transitions-forms"]')) {
93-
document.addEventListener('submit', (ev) => {
94-
let el = ev.target as HTMLElement;
95-
if (el.tagName !== 'FORM' || isReloadEl(el)) {
96-
return;
97-
}
95+
document.addEventListener('submit', (ev) => {
96+
let el = ev.target as HTMLElement;
97+
if (el.tagName !== 'FORM' || isReloadEl(el)) {
98+
return;
99+
}
98100

99-
const form = el as HTMLFormElement;
100-
const submitter = ev.submitter;
101-
const formData = new FormData(form);
102-
// Use the form action, if defined, otherwise fallback to current path.
103-
let action = submitter?.getAttribute('formaction') ?? form.action ?? location.pathname;
104-
const method = submitter?.getAttribute('formmethod') ?? form.method;
101+
const form = el as HTMLFormElement;
102+
const submitter = ev.submitter;
103+
const formData = new FormData(form);
104+
// Use the form action, if defined, otherwise fallback to current path.
105+
let action = submitter?.getAttribute('formaction') ?? form.action ?? location.pathname;
106+
const method = submitter?.getAttribute('formmethod') ?? form.method;
105107

106-
const options: Options = { sourceElement: submitter ?? form };
107-
if (method === 'get') {
108-
const params = new URLSearchParams(formData as any);
109-
const url = new URL(action);
110-
url.search = params.toString();
111-
action = url.toString();
112-
} else {
113-
options.formData = formData;
114-
}
115-
ev.preventDefault();
116-
navigate(action, options);
117-
});
118-
}
108+
const options: Options = { sourceElement: submitter ?? form };
109+
if (method === 'get') {
110+
const params = new URLSearchParams(formData as any);
111+
const url = new URL(action);
112+
url.search = params.toString();
113+
action = url.toString();
114+
} else {
115+
options.formData = formData;
116+
}
117+
ev.preventDefault();
118+
navigate(action, options);
119+
});
119120

120121
// @ts-expect-error injected by vite-plugin-transitions for treeshaking
121122
if (!__PREFETCH_DISABLED__) {

0 commit comments

Comments
 (0)
Please sign in to comment.