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

feat: allow to opt-out of use:enhance for forms #13250

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
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/spotty-queens-beam.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': minor
---

feat: allow opting out of `use:enhance` with `data-sveltekit-reload` on the form element or submit button
2 changes: 2 additions & 0 deletions documentation/docs/20-core-concepts/30-form-actions.md
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,8 @@ Without an argument, `use:enhance` will emulate the browser-native behaviour, ju
- render the nearest `+error` boundary if an error occurs
- [reset focus](accessibility#Focus-management) to the appropriate element

You can add the [`data-sveltekit-reload`](link-options#data-sveltekit-reload) attribute on the `<form>` or the `<button>` to opt-out of this behavior and instead let the browser handle the form submission.
fehnomenal marked this conversation as resolved.
Show resolved Hide resolved

### Customising use:enhance

To customise the behaviour, you can provide a `SubmitFunction` that runs immediately before the form is submitted, and (optionally) returns a callback that runs with the `ActionResult`. Note that if you return a callback, the default behavior mentioned above is not triggered. To get it back, call `update`.
Expand Down
2 changes: 2 additions & 0 deletions documentation/docs/30-advanced/30-link-options.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ Occasionally, we need to tell SvelteKit not to handle a link, but allow the brow

Links with a `rel="external"` attribute will receive the same treatment. In addition, they will be ignored during [prerendering](page-options#prerender).

Forms with the `use:enhance` action can skip using the enhanced behavior by adding a `data-sveltekit-reload` attribute to the form element or the submit button.

## data-sveltekit-replacestate

Sometimes you don't want navigation to create a new entry in the browser's session history. Adding a `data-sveltekit-replacestate` attribute to a link...
Expand Down
11 changes: 11 additions & 0 deletions packages/kit/src/runtime/app/forms.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as devalue from 'devalue';
import { DEV } from 'esm-env';
import { invalidateAll } from './navigation.js';
import { app, applyAction } from '../client/client.js';
import { link_option } from '../client/utils.js';

export { applyAction };

Expand Down Expand Up @@ -117,6 +118,16 @@ export function enhance(form_element, submit = () => {}) {
: clone(form_element).method;
if (method !== 'post') return;

/** @type {import('../client/utils.js').ValidLinkOptions<'reload'> | null} */
let reload = null;
if (event.submitter) {
reload = link_option(event.submitter, 'reload');
}
if (reload === null) {
reload = link_option(form_element, 'reload');
}
if (reload === '' || reload === 'true') return;

event.preventDefault();

const action = new URL(
Expand Down
2 changes: 1 addition & 1 deletion packages/kit/src/runtime/client/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ const valid_link_options = /** @type {const} */ ({
* @param {Element} element
* @param {T} name
*/
function link_option(element, name) {
export function link_option(element, name) {
const value = /** @type {ValidLinkOptions<T> | null} */ (
element.getAttribute(`data-sveltekit-${name}`)
);
Expand Down
Loading