@@ -2,13 +2,22 @@ import * as React from 'react'
22import { useCallback , useEffect } from 'react'
33import { useBeforeUnload , useLocation , useNavigation } from 'react-router-dom'
44
5+ type Direction = "vertical" | "horizontal" ;
6+ type ScrollAttribute = "scrollTop" | "scrollLeft" ;
7+ const DIRECTION : { [ direction in Direction ] : ScrollAttribute } = {
8+ vertical : 'scrollTop' ,
9+ horizontal : 'scrollLeft' ,
10+ } ;
11+
512export function ElementScrollRestoration ( {
613 elementQuery,
14+ direction = "vertical" ,
715 ...props
8- } : { elementQuery : string } & React . HTMLProps < HTMLScriptElement > ) {
16+ } : { elementQuery : string ; direction ?: Direction } & React . HTMLProps < HTMLScriptElement > ) {
917 const STORAGE_KEY = `position:${ elementQuery } `
1018 const navigation = useNavigation ( )
1119 const location = useLocation ( )
20+ const scrollAttribute = DIRECTION [ direction ]
1221
1322 const updatePositions = useCallback ( ( ) => {
1423 const element = document . querySelector ( elementQuery )
@@ -26,7 +35,7 @@ export function ElementScrollRestoration({
2635 }
2736 const newPositions = {
2837 ...positions ,
29- [ location . key ] : element . scrollTop ,
38+ [ location . key ] : element [ scrollAttribute ] ,
3039 }
3140 sessionStorage . setItem ( STORAGE_KEY , JSON . stringify ( newPositions ) )
3241 } , [ STORAGE_KEY , elementQuery , location . key ] )
@@ -39,9 +48,9 @@ export function ElementScrollRestoration({
3948 const positions = JSON . parse (
4049 sessionStorage . getItem ( STORAGE_KEY ) || '{}' ,
4150 ) as any
42- const storedY = positions [ window . history . state . key ]
43- if ( typeof storedY === 'number' ) {
44- element . scrollTop = storedY
51+ const stored = positions [ window . history . state . key ]
52+ if ( typeof stored === 'number' ) {
53+ element [ scrollAttribute ] = stored
4554 }
4655 } catch ( error : unknown ) {
4756 console . error ( error )
@@ -56,7 +65,7 @@ export function ElementScrollRestoration({
5665 updatePositions ( )
5766 } )
5867
59- function restoreScroll ( storageKey : string , elementQuery : string ) {
68+ function restoreScroll ( storageKey : string , elementQuery : string , scrollAttribute : ScrollAttribute ) {
6069 const element = document . querySelector ( elementQuery )
6170 if ( ! element ) {
6271 console . warn ( `Element not found: ${ elementQuery } . Cannot restore scroll.` )
@@ -70,9 +79,9 @@ export function ElementScrollRestoration({
7079 const positions = JSON . parse (
7180 sessionStorage . getItem ( storageKey ) || '{}' ,
7281 ) as any
73- const storedY = positions [ window . history . state . key ]
74- if ( typeof storedY === 'number' ) {
75- element . scrollTop = storedY
82+ const stored = positions [ window . history . state . key ]
83+ if ( typeof stored === 'number' ) {
84+ element [ scrollAttribute ] = stored
7685 }
7786 } catch ( error : unknown ) {
7887 console . error ( error )
@@ -86,7 +95,7 @@ export function ElementScrollRestoration({
8695 dangerouslySetInnerHTML = { {
8796 __html : `(${ restoreScroll } )(${ JSON . stringify (
8897 STORAGE_KEY ,
89- ) } , ${ JSON . stringify ( elementQuery ) } )`,
98+ ) } , ${ JSON . stringify ( elementQuery ) } , ${ JSON . stringify ( scrollAttribute ) } )`,
9099 } }
91100 />
92101 )
0 commit comments