Skip to content

useElementScrollRestoration #9573

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

Closed
Closed
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
11 changes: 3 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@
"packages/react-router-native",
"packages/router"
],
"nohoist": [
"**/react-native",
"**/react-native/**"
]
"nohoist": ["**/react-native", "**/react-native/**"]
},
"scripts": {
"build": "rollup -c",
Expand All @@ -32,9 +29,7 @@
"watch": "rollup -c -w"
},
"jest": {
"projects": [
"<rootDir>/packages/*"
]
"projects": ["<rootDir>/packages/*"]
},
"resolutions": {
"@types/react": "^18.0.0",
Expand Down Expand Up @@ -116,7 +111,7 @@
"none": "14.5 kB"
},
"packages/react-router-dom/dist/react-router-dom.production.min.js": {
"none": "10 kB"
"none": "10.5 kB"
},
"packages/react-router-dom/dist/umd/react-router-dom.production.min.js": {
"none": "16 kB"
Expand Down
36 changes: 26 additions & 10 deletions packages/react-router-dom/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1025,15 +1025,14 @@ const SCROLL_RESTORATION_STORAGE_KEY = "react-router-scroll-positions";
let savedScrollPositions: Record<string, number> = {};

/**
* When rendered inside a RouterProvider, will restore scroll positions on navigations
* When rendered inside a RouterProvider, will restore scroll positions on navigations.
* If element `id` is not specified, will use `window` instead.
*/
function useScrollRestoration({
function useElementScrollRestoration({
id,
getKey,
storageKey,
}: {
getKey?: GetScrollRestorationKeyFunction;
storageKey?: string;
} = {}) {
}: { id?: string } & ScrollRestorationProps = {}) {
let { router } = useDataRouterContext(DataRouterHook.UseScrollRestoration);
let { restoreScrollPosition, preventScrollReset } = useDataRouterState(
DataRouterStateHook.UseScrollRestoration
Expand All @@ -1042,6 +1041,14 @@ function useScrollRestoration({
let matches = useMatches();
let navigation = useNavigation();

const getScrollElement = () => (id && document.getElementById(id)) || window;

// window has scrollY but normal elements have scrollTop
const getScrollY = () => {
const el = getScrollElement();
return "scrollY" in el ? el.scrollY : el.scrollTop;
};

// Trigger manual scroll restoration while we're active
React.useEffect(() => {
window.history.scrollRestoration = "manual";
Expand All @@ -1054,8 +1061,9 @@ function useScrollRestoration({
useBeforeUnload(
React.useCallback(() => {
if (navigation.state === "idle") {
// TODO: key should probably incorporate the ID somehow
let key = (getKey ? getKey(location, matches) : null) || location.key;
savedScrollPositions[key] = window.scrollY;
savedScrollPositions[key] = getScrollY();
}
sessionStorage.setItem(
storageKey || SCROLL_RESTORATION_STORAGE_KEY,
Expand Down Expand Up @@ -1083,7 +1091,7 @@ function useScrollRestoration({
React.useLayoutEffect(() => {
let disableScrollRestoration = router?.enableScrollRestoration(
savedScrollPositions,
() => window.scrollY,
getScrollY,
getKey
);
return () => disableScrollRestoration && disableScrollRestoration();
Expand All @@ -1098,7 +1106,7 @@ function useScrollRestoration({

// been here before, scroll to it
if (typeof restoreScrollPosition === "number") {
window.scrollTo(0, restoreScrollPosition);
getScrollElement().scrollTo(0, restoreScrollPosition);
return;
}

Expand All @@ -1117,10 +1125,18 @@ function useScrollRestoration({
}

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

/**
* When rendered inside a RouterProvider, will restore scroll positions on navigations
*/
function useScrollRestoration(options: ScrollRestorationProps = {}) {
// default no id means it will use window
useElementScrollRestoration(options);
}

function useBeforeUnload(callback: () => any): void {
React.useEffect(() => {
window.addEventListener("beforeunload", callback);
Expand Down