fix(web): restore scroll position when navigating back
This commit is contained in:
2 files changed
+114
-1
No files matched your search
@@ -5,6 +5,8 @@ import { PlayerBar } from "@/components/player-bar"
|
||||
import { LazyQueuePanel } from "@/components/lazy-queue-panel"
|
||||
import { SidebarInset, SidebarProvider } from "@/components/ui/sidebar"
|
||||
import { AppSidebar } from "@/components/app-sidebar"
|
||||
import { ScrollRestoration } from "@/components/scroll-restoration"
|
||||
import { Suspense } from "react"
|
||||
|
||||
export default function AppLayout({
|
||||
children,
|
||||
@@ -18,7 +20,11 @@ export default function AppLayout({
|
||||
<FavoritesProvider>
|
||||
<AppSidebar />
|
||||
<div className="flex min-h-svh min-w-0 flex-1">
|
||||
<SidebarInset className="min-w-0 flex-1">{children}</SidebarInset>
|
||||
<SidebarInset className="min-w-0 flex-1">
|
||||
<Suspense>
|
||||
<ScrollRestoration>{children}</ScrollRestoration>
|
||||
</Suspense>
|
||||
</SidebarInset>
|
||||
<LazyQueuePanel />
|
||||
</div>
|
||||
<PlayerBar />
|
||||
|
||||
@@ -0,0 +1,107 @@
|
||||
"use client"
|
||||
|
||||
import { useLayoutEffect, useRef } from "react"
|
||||
import { usePathname, useSearchParams } from "next/navigation"
|
||||
|
||||
const historyKey = "otunesScrollKey"
|
||||
const scrollSelector = '[data-slot="scroll-area-viewport"], .overflow-y-auto'
|
||||
type Position = { top: number; left: number }
|
||||
|
||||
// Track page containers independently of the window, including split library views.
|
||||
export function ScrollRestoration({ children }: { children: React.ReactNode }) {
|
||||
const pathname = usePathname()
|
||||
const searchParams = useSearchParams()
|
||||
const route = `${pathname}?${searchParams.toString()}`
|
||||
const rootRef = useRef<HTMLDivElement>(null)
|
||||
const positions = useRef(new Map<string, Map<number, Position>>())
|
||||
useLayoutEffect(() => {
|
||||
const root = rootRef.current
|
||||
if (!root) return
|
||||
|
||||
const key =
|
||||
typeof history.state?.[historyKey] === "string"
|
||||
? (history.state[historyKey] as string)
|
||||
: crypto.randomUUID()
|
||||
history.replaceState({ ...history.state, [historyKey]: key }, "")
|
||||
const saved = positions.current.get(key)
|
||||
const pending = new Map(saved)
|
||||
if (!positions.current.has(key)) positions.current.set(key, new Map())
|
||||
// Bound memory during long listening sessions.
|
||||
if (positions.current.size > 100) {
|
||||
const oldest = positions.current.keys().next().value
|
||||
if (oldest !== undefined) positions.current.delete(oldest)
|
||||
}
|
||||
|
||||
const containers = () =>
|
||||
Array.from(root.querySelectorAll<HTMLElement>(scrollSelector)).filter(
|
||||
(element) => element.getClientRects().length > 0
|
||||
)
|
||||
const restore = () => {
|
||||
for (const [index, element] of containers().entries()) {
|
||||
const position = pending.get(index)
|
||||
if (!position) continue
|
||||
element.scrollTo({
|
||||
top: position.top,
|
||||
left: position.left,
|
||||
behavior: "instant",
|
||||
})
|
||||
// Keep waiting if asynchronous content is still too short.
|
||||
if (
|
||||
Math.abs(element.scrollTop - position.top) < 1 &&
|
||||
Math.abs(element.scrollLeft - position.left) < 1
|
||||
) {
|
||||
pending.delete(index)
|
||||
}
|
||||
}
|
||||
}
|
||||
const record = (event: Event) => {
|
||||
if (!(event.target instanceof HTMLElement)) return
|
||||
const index = containers().indexOf(event.target)
|
||||
if (index < 0 || pending.has(index)) return
|
||||
positions.current.get(key)?.set(index, {
|
||||
top: event.target.scrollTop,
|
||||
left: event.target.scrollLeft,
|
||||
})
|
||||
}
|
||||
const stopRestoring = () => {
|
||||
pending.clear()
|
||||
observer.disconnect()
|
||||
resizeObserver.disconnect()
|
||||
}
|
||||
const observer = new MutationObserver(() => {
|
||||
restore()
|
||||
for (const element of containers()) {
|
||||
resizeObserver.observe(element)
|
||||
for (const child of element.children) resizeObserver.observe(child)
|
||||
}
|
||||
})
|
||||
const resizeObserver = new ResizeObserver(restore)
|
||||
if (pending.size) {
|
||||
observer.observe(root, { childList: true, subtree: true })
|
||||
for (const element of containers()) {
|
||||
resizeObserver.observe(element)
|
||||
for (const child of element.children) resizeObserver.observe(child)
|
||||
}
|
||||
restore()
|
||||
}
|
||||
const timeout = window.setTimeout(stopRestoring, 10_000)
|
||||
root.addEventListener("scroll", record, true)
|
||||
root.addEventListener("wheel", stopRestoring, { passive: true })
|
||||
root.addEventListener("touchstart", stopRestoring, { passive: true })
|
||||
root.addEventListener("keydown", stopRestoring)
|
||||
return () => {
|
||||
window.clearTimeout(timeout)
|
||||
stopRestoring()
|
||||
root.removeEventListener("scroll", record, true)
|
||||
root.removeEventListener("wheel", stopRestoring)
|
||||
root.removeEventListener("touchstart", stopRestoring)
|
||||
root.removeEventListener("keydown", stopRestoring)
|
||||
}
|
||||
}, [route])
|
||||
|
||||
return (
|
||||
<div ref={rootRef} className="contents">
|
||||
{children}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
Reference in new issue
Block a user