Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support form submissions in the ViewTransitions router (#8963)
* Support form submissions in the ViewTransitions router * Align with navigate API, add `formData` option * Change API to handleForms * Add a changeset * Add a test for non-200 responses * Update .changeset/many-weeks-sort.md Co-authored-by: Sarah Rainsberger <sarah@rainsberger.ca> * Update .changeset/many-weeks-sort.md Co-authored-by: Sarah Rainsberger <sarah@rainsberger.ca> * Add a little more on why this is exciting! * Update .changeset/many-weeks-sort.md Co-authored-by: Sarah Rainsberger <sarah@rainsberger.ca> * Switch to e.g. --------- Co-authored-by: Sarah Rainsberger <sarah@rainsberger.ca>
- Loading branch information
1 parent
8a51afd
commit fda3a02
Showing
9 changed files
with
204 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
--- | ||
'astro': minor | ||
--- | ||
|
||
Form support in View Transitions router | ||
|
||
The `<ViewTransitions />` router can now handle form submissions, allowing the same animated transitions and stateful UI retention on form posts that are already available on `<a>` links. With this addition, your Astro project can have animations in all of these scenarios: | ||
|
||
- Clicking links between pages. | ||
- Making stateful changes in forms (e.g. updating site preferences). | ||
- Manually triggering navigation via the `navigate()` API. | ||
|
||
This feature is opt-in for semver reasons and can be enabled by adding the `handleForms` prop to the `<ViewTransitions /> component: | ||
|
||
```astro | ||
--- | ||
// src/layouts/MainLayout.astro | ||
import { ViewTransitions } from 'astro:transitions'; | ||
--- | ||
<html> | ||
<head> | ||
<!-- ... --> | ||
<ViewTransitions handleForms /> | ||
</head> | ||
<body> | ||
<!-- ... --> | ||
</body> | ||
</html> | ||
``` | ||
|
||
Just as with links, if you don't want the routing handling a form submission, you can opt out on a per-form basis with the `data-astro-reload` property: | ||
|
||
```astro | ||
--- | ||
// src/components/Contact.astro | ||
--- | ||
<form class="contact-form" action="/request" method="post" data-astro-reload> | ||
<!-- ...--> | ||
</form> | ||
``` | ||
|
||
Form support works on post `method="get"` and `method="post"` forms. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
packages/astro/e2e/fixtures/view-transitions/src/pages/contact.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import type { APIContext } from 'astro'; | ||
|
||
export const POST = async ({ request, redirect }: APIContext) => { | ||
const formData = await request.formData(); | ||
const name = formData.get('name'); | ||
const shouldThrow = formData.has('throw'); | ||
if(shouldThrow) { | ||
throw new Error('oh no!'); | ||
} | ||
|
||
return redirect(`/form-response?name=${name}`); | ||
} | ||
|
||
export const GET = async ({ url, redirect }: APIContext) => { | ||
const name = url.searchParams.get('name'); | ||
return redirect(`/form-response?name=${name}`); | ||
} |
13 changes: 13 additions & 0 deletions
13
packages/astro/e2e/fixtures/view-transitions/src/pages/form-one.astro
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
--- | ||
import Layout from '../components/Layout.astro'; | ||
const method = Astro.url.searchParams.get('method') ?? 'POST'; | ||
const postShowThrow = Astro.url.searchParams.has('throw') ?? false; | ||
--- | ||
<Layout> | ||
<h2>Contact Form</h2> | ||
<form action="/contact" method={method}> | ||
<input type="hidden" name="name" value="Testing"> | ||
{postShowThrow ? <input type="hidden" name="throw" value="true"> : ''} | ||
<input type="submit" value="Submit" id="submit"> | ||
</form> | ||
</Layout> |
7 changes: 7 additions & 0 deletions
7
packages/astro/e2e/fixtures/view-transitions/src/pages/form-response.astro
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
--- | ||
import Layout from '../components/Layout.astro'; | ||
const name = Astro.url.searchParams.get('name'); | ||
--- | ||
<Layout> | ||
<div>Submitted contact: <span id="contact-name">{name}</span></div> | ||
</Layout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters