Skip to content

Commit f03c734

Browse files
committed
do scroll restoration in userland. lol. fucking lmao
1 parent 1eb450f commit f03c734

File tree

3 files changed

+18
-122
lines changed

3 files changed

+18
-122
lines changed

app/hooks/use-scroll-restoration.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { useEffect, useRef } from 'react'
2+
import { useLocation, useNavigation } from 'react-router-dom'
3+
4+
export function useScrollRestoration(elementRef: React.RefObject<HTMLElement>) {
5+
const cache = useRef(new Map<string, number>())
6+
const { key } = useLocation()
7+
const { state } = useNavigation()
8+
useEffect(() => {
9+
if (state === 'loading') {
10+
cache.current.set(key, elementRef.current?.scrollTop ?? 0)
11+
} else if (state === 'idle' && cache.current.has(key)) {
12+
elementRef.current?.scrollTo(0, cache.current.get(key)!)
13+
}
14+
}, [key, state, elementRef])
15+
}

app/layouts/helpers.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
11
import { useRef } from 'react'
2-
import { Outlet, ScrollRestoration } from 'react-router-dom'
2+
import { Outlet } from 'react-router-dom'
33

44
import { apiQueryClient } from '@oxide/api'
55
import { Pagination } from '@oxide/pagination'
66
import { SkipLinkTarget } from '@oxide/ui'
77
import { classed } from '@oxide/util'
88

99
import { PageActionsTarget } from 'app/components/PageActions'
10+
import { useScrollRestoration } from 'app/hooks/use-scroll-restoration'
1011

1112
export const PageContainer = classed.div`grid h-screen grid-cols-[14.25rem,1fr] grid-rows-[60px,1fr]`
1213

1314
export function ContentPane() {
1415
const ref = useRef<HTMLDivElement>(null)
16+
useScrollRestoration(ref)
1517
return (
1618
<div ref={ref} className="flex flex-col overflow-auto">
17-
<ScrollRestoration elementRef={ref} />
1819
<div className="flex flex-grow flex-col pb-8">
1920
<SkipLinkTarget />
2021
<main className="[&>*]:gutter">

patches/react-router-dom+6.11.2.patch

Lines changed: 0 additions & 120 deletions
This file was deleted.

0 commit comments

Comments
 (0)