Skip to content

Commit

Permalink
do scroll restoration in userland. lol. fucking lmao
Browse files Browse the repository at this point in the history
  • Loading branch information
david-crespo committed Jun 3, 2023
1 parent 1eb450f commit f03c734
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 122 deletions.
15 changes: 15 additions & 0 deletions app/hooks/use-scroll-restoration.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
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 { state } = useNavigation()
useEffect(() => {
if (state === 'loading') {
cache.current.set(key, elementRef.current?.scrollTop ?? 0)
} else if (state === 'idle' && cache.current.has(key)) {
elementRef.current?.scrollTo(0, cache.current.get(key)!)
}
}, [key, state, elementRef])
}
5 changes: 3 additions & 2 deletions app/layouts/helpers.tsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
import { useRef } from 'react'
import { Outlet, ScrollRestoration } from 'react-router-dom'
import { Outlet } from 'react-router-dom'

import { apiQueryClient } from '@oxide/api'
import { Pagination } from '@oxide/pagination'
import { SkipLinkTarget } from '@oxide/ui'
import { classed } from '@oxide/util'

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

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

export function ContentPane() {
const ref = useRef<HTMLDivElement>(null)
useScrollRestoration(ref)
return (
<div ref={ref} className="flex flex-col overflow-auto">
<ScrollRestoration elementRef={ref} />
<div className="flex flex-grow flex-col pb-8">
<SkipLinkTarget />
<main className="[&>*]:gutter">
Expand Down
120 changes: 0 additions & 120 deletions patches/react-router-dom+6.11.2.patch

This file was deleted.

0 comments on commit f03c734

Please sign in to comment.