Skip to content

Commit

Permalink
Merge branch 'main' into remove-current-user-loader
Browse files Browse the repository at this point in the history
  • Loading branch information
fraxachun committed Feb 13, 2024
2 parents f604773 + 84062ec commit fb56ddf
Showing 1 changed file with 4 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import * as React from "react";
import { useLocation } from "react-router";

const scrollPositions: Record<string, number | undefined> = {};

interface UseScrollRestorationProps<Element> {
ref: React.MutableRefObject<Element | null>;
onScroll: () => void;
Expand All @@ -14,15 +12,17 @@ export function useScrollRestoration<Element extends HTMLElement>(identifier: st

const ref = React.useRef<Element>(null);

const scrollPositionsRef = React.useRef<Record<string, number | undefined>>({});

React.useLayoutEffect((): void => {
if (ref.current) {
ref.current.scrollTo(0, scrollPositions[identifierForRoute] ?? 0);
ref.current.scrollTo(0, scrollPositionsRef.current[identifierForRoute] ?? 0);
}
}, [identifierForRoute]);

const onScroll = React.useCallback(() => {
if (ref.current) {
scrollPositions[identifierForRoute] = ref.current.scrollTop;
scrollPositionsRef.current[identifierForRoute] = ref.current.scrollTop;
}
}, [identifierForRoute]);

Expand Down

0 comments on commit fb56ddf

Please sign in to comment.