|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import { useEffect, useMemo, useRef } from "react"; |
| 4 | + |
| 5 | +import { usePathname, useSearchParams } from "next/navigation"; |
| 6 | + |
| 7 | +interface ScrollPosition { |
| 8 | + x: number; |
| 9 | + y: number; |
| 10 | +} |
| 11 | + |
| 12 | +const TOP_POSITION: ScrollPosition = { x: 0, y: 0 }; |
| 13 | + |
| 14 | +function getScrollPosition(): ScrollPosition { |
| 15 | + return { x: window.scrollX, y: window.scrollY }; |
| 16 | +} |
| 17 | + |
| 18 | +function scrollToPosition(position: ScrollPosition) { |
| 19 | + window.scrollTo({ |
| 20 | + left: position.x, |
| 21 | + top: position.y, |
| 22 | + behavior: "auto", |
| 23 | + }); |
| 24 | +} |
| 25 | + |
| 26 | +export default function ScrollManager() { |
| 27 | + const pathname = usePathname(); |
| 28 | + const searchParams = useSearchParams(); |
| 29 | + const routeKey = useMemo(() => { |
| 30 | + const queryString = searchParams.toString(); |
| 31 | + return queryString ? `${pathname}?${queryString}` : pathname; |
| 32 | + }, [pathname, searchParams]); |
| 33 | + const routeKeyRef = useRef(routeKey); |
| 34 | + const positionsRef = useRef<Record<string, ScrollPosition>>({}); |
| 35 | + const isPopNavigationRef = useRef(false); |
| 36 | + const frameRef = useRef<number | null>(null); |
| 37 | + |
| 38 | + useEffect(() => { |
| 39 | + if (!("scrollRestoration" in window.history)) return; |
| 40 | + |
| 41 | + const previousScrollRestoration = window.history.scrollRestoration; |
| 42 | + window.history.scrollRestoration = "manual"; |
| 43 | + |
| 44 | + return () => { |
| 45 | + window.history.scrollRestoration = previousScrollRestoration; |
| 46 | + }; |
| 47 | + }, []); |
| 48 | + |
| 49 | + useEffect(() => { |
| 50 | + const handlePopState = () => { |
| 51 | + isPopNavigationRef.current = true; |
| 52 | + }; |
| 53 | + |
| 54 | + window.addEventListener("popstate", handlePopState); |
| 55 | + return () => window.removeEventListener("popstate", handlePopState); |
| 56 | + }, []); |
| 57 | + |
| 58 | + useEffect(() => { |
| 59 | + const positions = positionsRef.current; |
| 60 | + |
| 61 | + const handleScroll = () => { |
| 62 | + if (frameRef.current !== null) return; |
| 63 | + |
| 64 | + frameRef.current = window.requestAnimationFrame(() => { |
| 65 | + positions[routeKeyRef.current] = getScrollPosition(); |
| 66 | + frameRef.current = null; |
| 67 | + }); |
| 68 | + }; |
| 69 | + |
| 70 | + window.addEventListener("scroll", handleScroll, { passive: true }); |
| 71 | + return () => { |
| 72 | + window.removeEventListener("scroll", handleScroll); |
| 73 | + if (frameRef.current !== null) { |
| 74 | + window.cancelAnimationFrame(frameRef.current); |
| 75 | + } |
| 76 | + positions[routeKeyRef.current] = getScrollPosition(); |
| 77 | + }; |
| 78 | + }, []); |
| 79 | + |
| 80 | + useEffect(() => { |
| 81 | + const previousRouteKey = routeKeyRef.current; |
| 82 | + if (previousRouteKey === routeKey) return; |
| 83 | + |
| 84 | + positionsRef.current[previousRouteKey] ??= getScrollPosition(); |
| 85 | + routeKeyRef.current = routeKey; |
| 86 | + |
| 87 | + const shouldRestore = isPopNavigationRef.current; |
| 88 | + const nextPosition = shouldRestore |
| 89 | + ? (positionsRef.current[routeKey] ?? TOP_POSITION) |
| 90 | + : TOP_POSITION; |
| 91 | + isPopNavigationRef.current = false; |
| 92 | + |
| 93 | + window.requestAnimationFrame(() => { |
| 94 | + window.requestAnimationFrame(() => scrollToPosition(nextPosition)); |
| 95 | + }); |
| 96 | + }, [routeKey]); |
| 97 | + |
| 98 | + return null; |
| 99 | +} |
0 commit comments