@@ -146,7 +146,7 @@ export const defaultGetScrollRestorationKey = (location: ParsedLocation) => {
146146 return location . state . __TSR_key ! || location . href
147147}
148148
149- export function getCssSelector ( el : any ) : string {
149+ function getCssSelector ( el : any ) : string {
150150 const path = [ ]
151151 let parent : HTMLElement
152152 while ( ( parent = el . parentNode ) ) {
@@ -158,6 +158,41 @@ export function getCssSelector(el: any): string {
158158 return `${ path . reverse ( ) . join ( ' > ' ) } ` . toLowerCase ( )
159159}
160160
161+ export function getElementScrollRestorationEntry (
162+ router : AnyRouter ,
163+ options : (
164+ | {
165+ id : string
166+ getElement ?: ( ) => Window | Element | undefined | null
167+ }
168+ | {
169+ id ?: string
170+ getElement : ( ) => Window | Element | undefined | null
171+ }
172+ ) & {
173+ getKey ?: ( location : ParsedLocation ) => string
174+ } ,
175+ ) : ScrollRestorationEntry | undefined {
176+ const getKey = options . getKey || defaultGetScrollRestorationKey
177+
178+ let elementSelector = ''
179+
180+ if ( options . id ) {
181+ elementSelector = `[${ scrollRestorationIdAttribute } ="${ options . id } "]`
182+ } else {
183+ const element = options . getElement ?.( )
184+ if ( ! element ) {
185+ return
186+ }
187+ elementSelector =
188+ element instanceof Window ? windowScrollTarget : getCssSelector ( element )
189+ }
190+
191+ const restoreKey = getKey ( router . latestLocation )
192+ const byKey = scrollRestorationCache ?. state [ restoreKey ]
193+ return byKey ?. [ elementSelector ]
194+ }
195+
161196let ignoreScroll = false
162197const windowScrollTarget = 'window'
163198const scrollRestorationIdAttribute = 'data-scroll-restoration-id'
@@ -397,26 +432,3 @@ export function setupScrollRestoration(router: AnyRouter, force?: boolean) {
397432 }
398433 } )
399434}
400-
401- /**
402- * @private
403- * Handles hash-based scrolling after navigation completes.
404- * To be used in framework-specific <Transitioner> components during the onResolved event.
405- *
406- * Provides hash scrolling for programmatic navigation when default browser handling is prevented.
407- * @param router The router instance containing current location and state
408- */
409- export function handleHashScroll ( router : AnyRouter ) {
410- if ( typeof document !== 'undefined' && ( document as any ) . querySelector ) {
411- const location = router . stores . location . state
412- const hashScrollIntoViewOptions =
413- location . state . __hashScrollIntoViewOptions ?? true
414-
415- if ( hashScrollIntoViewOptions && location . hash !== '' ) {
416- const el = document . getElementById ( location . hash )
417- if ( el ) {
418- el . scrollIntoView ( hashScrollIntoViewOptions )
419- }
420- }
421- }
422- }
0 commit comments