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

Fix scroll restoration when redirecting in an action #9886

Merged
merged 6 commits into from
Jan 11, 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/kind-seals-know.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@remix-run/router": patch
---

Fix scroll reset if a submission redirects
5 changes: 5 additions & 0 deletions .changeset/quiet-crabs-jump.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"react-router-dom": minor
---

Add `preventScrollReset` prop to `<Form>`
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@
},
"filesize": {
"packages/router/dist/router.umd.min.js": {
"none": "38.5 kB"
"none": "39 kB"
},
"packages/react-router/dist/react-router.production.min.js": {
"none": "12.5 kB"
Expand Down
6 changes: 6 additions & 0 deletions packages/react-router-dom/dom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,12 @@ export interface SubmitOptions {
* hierarchy and want to instead route based on /-delimited URL segments
*/
relative?: RelativeRoutingType;

/**
* In browser-based environments, prevent resetting scroll after this
* navigation when using the <ScrollRestoration> component
*/
preventScrollReset?: boolean;
}

export function getFormSubmissionInfo(
Expand Down
16 changes: 13 additions & 3 deletions packages/react-router-dom/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -593,6 +593,12 @@ export interface FormProps extends React.FormHTMLAttributes<HTMLFormElement> {
*/
relative?: RelativeRoutingType;

/**
* Prevent the scroll position from resetting to the top of the viewport on
* completion of the navigation when using the <ScrollRestoration> component
*/
preventScrollReset?: boolean;

/**
* A function to call when the form is submitted. If you call
* `event.preventDefault()` then this form will not do anything.
Expand Down Expand Up @@ -640,6 +646,7 @@ const FormImpl = React.forwardRef<HTMLFormElement, FormImplProps>(
fetcherKey,
routeId,
relative,
preventScrollReset,
...props
},
forwardedRef
Expand All @@ -664,6 +671,7 @@ const FormImpl = React.forwardRef<HTMLFormElement, FormImplProps>(
method: submitMethod,
replace,
relative,
preventScrollReset,
});
};

Expand Down Expand Up @@ -906,6 +914,7 @@ function useSubmitImpl(fetcherKey?: string, routeId?: string): SubmitFunction {
let href = url.pathname + url.search;
let opts = {
replace: options.replace,
preventScrollReset: options.preventScrollReset,
formData,
formMethod: method as FormMethod,
formEncType: encType as FormEncType,
Expand Down Expand Up @@ -1000,8 +1009,9 @@ export type FetcherWithComponents<TData> = Fetcher<TData> & {
Form: ReturnType<typeof createFetcherForm>;
submit: (
target: SubmitTarget,
// Fetchers cannot replace because they are not navigation events
options?: Omit<SubmitOptions, "replace">
// Fetchers cannot replace/preventScrollReset because they are not
// navigation events
options?: Omit<SubmitOptions, "replace" | "preventScrollReset">
) => void;
load: (href: string) => void;
};
Expand Down Expand Up @@ -1165,7 +1175,7 @@ function useScrollRestoration({
}
}

// Opt out of scroll reset if this link requested it
// Don't reset if this navigation opted out
if (preventScrollReset === true) {
return;
}
Expand Down
Loading