-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
do scroll restoration in userland. lol. fucking lmao
- Loading branch information
1 parent
1eb450f
commit f00ec83
Showing
3 changed files
with
18 additions
and
122 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { useEffect, useRef } from 'react' | ||
import { useLocation, useNavigation } from 'react-router-dom' | ||
|
||
export function useScrollRestoration(elementRef: React.RefObject<HTMLElement>) { | ||
const cache = useRef(new Map<string, number>()) | ||
const { key } = useLocation() | ||
const navigation = useNavigation() | ||
const navState = navigation.state | ||
const nextKey = navigation.location?.key | ||
useEffect(() => { | ||
if (navState === 'loading') { | ||
cache.current.set(key, elementRef.current?.scrollTop ?? 0) | ||
} else if (navState === 'idle' && cache.current.has(key)) { | ||
elementRef.current?.scrollTo(0, cache.current.get(key)!) | ||
} | ||
}, [key, navState, nextKey, elementRef]) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.