Skip to content

Commit

Permalink
Updates to ScrollRestoration for Remix
Browse files Browse the repository at this point in the history
  • Loading branch information
brophdawg11 committed Dec 12, 2022
1 parent ecbca64 commit 09209b6
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions packages/react-router-dom/index.tsx
Expand Up @@ -64,6 +64,7 @@ import {
export type {
FormEncType,
FormMethod,
GetScrollRestorationKeyFunction,
ParamKeyValuePair,
SubmitOptions,
URLSearchParamsInit,
Expand Down Expand Up @@ -683,7 +684,7 @@ if (__DEV__) {
FormImpl.displayName = "FormImpl";
}

interface ScrollRestorationProps {
export interface ScrollRestorationProps {
getKey?: GetScrollRestorationKeyFunction;
storageKey?: string;
}
Expand Down Expand Up @@ -1080,9 +1081,11 @@ let savedScrollPositions: Record<string, number> = {};
function useScrollRestoration({
getKey,
storageKey,
skip = false,
}: {
getKey?: GetScrollRestorationKeyFunction;
storageKey?: string;
skip?: boolean;
} = {}) {
let { router } = useDataRouterContext(DataRouterHook.UseScrollRestoration);
let { restoreScrollPosition, preventScrollReset } = useDataRouterState(
Expand Down Expand Up @@ -1141,6 +1144,11 @@ function useScrollRestoration({

// Restore scrolling when state.restoreScrollPosition changes
React.useLayoutEffect(() => {
// Skip if the consumer asked us to (used for SSR)
if (skip) {
return;
}

// Explicit false means don't do anything (used for submissions)
if (restoreScrollPosition === false) {
return;
Expand Down Expand Up @@ -1168,7 +1176,7 @@ function useScrollRestoration({

// otherwise go to the top on new locations
window.scrollTo(0, 0);
}, [location, restoreScrollPosition, preventScrollReset]);
}, [location, restoreScrollPosition, preventScrollReset, skip]);
}

function useBeforeUnload(callback: () => any): void {
Expand Down Expand Up @@ -1203,3 +1211,5 @@ function warning(cond: boolean, message: string): void {
}
}
//#endregion

export { useScrollRestoration as UNSAFE_useScrollRestoration };

0 comments on commit 09209b6

Please sign in to comment.