Skip to content

Commit 0582210

Browse files
Fix scroll restoration when redirecting in an action (#9886)
* Fix scroll restoration when redirecting in an action (#9815) * Add preventScrollReset prop to Form Co-authored-by: Johann R <johann.rakotoharisoa@gmail.com>
1 parent 6836155 commit 0582210

File tree

7 files changed

+432
-136
lines changed

7 files changed

+432
-136
lines changed

.changeset/kind-seals-know.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@remix-run/router": patch
3+
---
4+
5+
Fix scroll reset if a submission redirects

.changeset/quiet-crabs-jump.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"react-router-dom": minor
3+
---
4+
5+
Add `preventScrollReset` prop to `<Form>`

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@
108108
},
109109
"filesize": {
110110
"packages/router/dist/router.umd.min.js": {
111-
"none": "38.5 kB"
111+
"none": "39 kB"
112112
},
113113
"packages/react-router/dist/react-router.production.min.js": {
114114
"none": "12.5 kB"

packages/react-router-dom/dom.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,12 @@ export interface SubmitOptions {
138138
* hierarchy and want to instead route based on /-delimited URL segments
139139
*/
140140
relative?: RelativeRoutingType;
141+
142+
/**
143+
* In browser-based environments, prevent resetting scroll after this
144+
* navigation when using the <ScrollRestoration> component
145+
*/
146+
preventScrollReset?: boolean;
141147
}
142148

143149
export function getFormSubmissionInfo(

packages/react-router-dom/index.tsx

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -593,6 +593,12 @@ export interface FormProps extends React.FormHTMLAttributes<HTMLFormElement> {
593593
*/
594594
relative?: RelativeRoutingType;
595595

596+
/**
597+
* Prevent the scroll position from resetting to the top of the viewport on
598+
* completion of the navigation when using the <ScrollRestoration> component
599+
*/
600+
preventScrollReset?: boolean;
601+
596602
/**
597603
* A function to call when the form is submitted. If you call
598604
* `event.preventDefault()` then this form will not do anything.
@@ -640,6 +646,7 @@ const FormImpl = React.forwardRef<HTMLFormElement, FormImplProps>(
640646
fetcherKey,
641647
routeId,
642648
relative,
649+
preventScrollReset,
643650
...props
644651
},
645652
forwardedRef
@@ -664,6 +671,7 @@ const FormImpl = React.forwardRef<HTMLFormElement, FormImplProps>(
664671
method: submitMethod,
665672
replace,
666673
relative,
674+
preventScrollReset,
667675
});
668676
};
669677

@@ -906,6 +914,7 @@ function useSubmitImpl(fetcherKey?: string, routeId?: string): SubmitFunction {
906914
let href = url.pathname + url.search;
907915
let opts = {
908916
replace: options.replace,
917+
preventScrollReset: options.preventScrollReset,
909918
formData,
910919
formMethod: method as FormMethod,
911920
formEncType: encType as FormEncType,
@@ -1000,8 +1009,9 @@ export type FetcherWithComponents<TData> = Fetcher<TData> & {
10001009
Form: ReturnType<typeof createFetcherForm>;
10011010
submit: (
10021011
target: SubmitTarget,
1003-
// Fetchers cannot replace because they are not navigation events
1004-
options?: Omit<SubmitOptions, "replace">
1012+
// Fetchers cannot replace/preventScrollReset because they are not
1013+
// navigation events
1014+
options?: Omit<SubmitOptions, "replace" | "preventScrollReset">
10051015
) => void;
10061016
load: (href: string) => void;
10071017
};
@@ -1165,7 +1175,7 @@ function useScrollRestoration({
11651175
}
11661176
}
11671177

1168-
// Opt out of scroll reset if this link requested it
1178+
// Don't reset if this navigation opted out
11691179
if (preventScrollReset === true) {
11701180
return;
11711181
}

0 commit comments

Comments
 (0)