Skip to content

Commit fbe6ef4

Browse files
restructure
1 parent 335572d commit fbe6ef4

6 files changed

Lines changed: 66 additions & 92 deletions

File tree

‎packages/react-router/src/ScrollRestoration.tsx‎

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
import {
2-
defaultGetScrollRestorationKey,
3-
getCssSelector,
4-
scrollRestorationCache,
2+
getElementScrollRestorationEntry,
53
setupScrollRestoration,
64
} from '@tanstack/router-core'
75
import { useRouter } from './useRouter'
@@ -47,23 +45,5 @@ export function useElementScrollRestoration(
4745
): ScrollRestorationEntry | undefined {
4846
useScrollRestoration()
4947

50-
const router = useRouter()
51-
const getKey = options.getKey || defaultGetScrollRestorationKey
52-
53-
let elementSelector = ''
54-
55-
if (options.id) {
56-
elementSelector = `[data-scroll-restoration-id="${options.id}"]`
57-
} else {
58-
const element = options.getElement?.()
59-
if (!element) {
60-
return
61-
}
62-
elementSelector =
63-
element instanceof Window ? 'window' : getCssSelector(element)
64-
}
65-
66-
const restoreKey = getKey(router.latestLocation)
67-
const byKey = scrollRestorationCache?.state[restoreKey]
68-
return byKey?.[elementSelector]
48+
return getElementScrollRestorationEntry(useRouter(), options)
6949
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import type { AnyRouter } from './router'
2+
3+
/**
4+
* @private
5+
* Handles hash-based scrolling after navigation completes.
6+
* To be used in framework-specific <Transitioner> components during the onResolved event.
7+
*/
8+
export function handleHashScroll(router: AnyRouter) {
9+
if (typeof document !== 'undefined' && (document as any).querySelector) {
10+
const location = router.stores.location.state
11+
const hashScrollIntoViewOptions =
12+
location.state.__hashScrollIntoViewOptions ?? true
13+
14+
if (hashScrollIntoViewOptions && location.hash !== '') {
15+
const el = document.getElementById(location.hash)
16+
if (el) {
17+
el.scrollIntoView(hashScrollIntoViewOptions)
18+
}
19+
}
20+
}
21+
}

‎packages/router-core/src/index.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -398,13 +398,14 @@ export { isNotFound, notFound } from './not-found'
398398
export {
399399
defaultGetScrollRestorationKey,
400400
getScrollRestorationScriptForRouter,
401+
getElementScrollRestorationEntry,
401402
storageKey,
402-
getCssSelector,
403403
scrollRestorationCache,
404404
setupScrollRestoration,
405-
handleHashScroll,
406405
} from './scroll-restoration'
407406

407+
export { handleHashScroll } from './hash-scroll'
408+
408409
export type {
409410
ScrollRestorationOptions,
410411
ScrollRestorationEntry,

‎packages/router-core/src/scroll-restoration.ts‎

Lines changed: 36 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
161196
let ignoreScroll = false
162197
const windowScrollTarget = 'window'
163198
const 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-
}

‎packages/solid-router/src/ScrollRestoration.tsx‎

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
import {
2-
defaultGetScrollRestorationKey,
3-
getCssSelector,
4-
scrollRestorationCache,
2+
getElementScrollRestorationEntry,
53
setupScrollRestoration,
64
} from '@tanstack/router-core'
75
import { useRouter } from './useRouter'
@@ -47,23 +45,5 @@ export function useElementScrollRestoration(
4745
): ScrollRestorationEntry | undefined {
4846
useScrollRestoration()
4947

50-
const router = useRouter()
51-
const getKey = options.getKey || defaultGetScrollRestorationKey
52-
53-
let elementSelector = ''
54-
55-
if (options.id) {
56-
elementSelector = `[data-scroll-restoration-id="${options.id}"]`
57-
} else {
58-
const element = options.getElement?.()
59-
if (!element) {
60-
return
61-
}
62-
elementSelector =
63-
element instanceof Window ? 'window' : getCssSelector(element)
64-
}
65-
66-
const restoreKey = getKey(router.latestLocation)
67-
const byKey = scrollRestorationCache?.state[restoreKey]
68-
return byKey?.[elementSelector]
48+
return getElementScrollRestorationEntry(useRouter(), options)
6949
}

‎packages/vue-router/src/ScrollRestoration.tsx‎

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
import {
2-
defaultGetScrollRestorationKey,
3-
getCssSelector,
4-
scrollRestorationCache,
2+
getElementScrollRestorationEntry,
53
setupScrollRestoration,
64
} from '@tanstack/router-core'
75
import { useRouter } from './useRouter'
@@ -47,23 +45,5 @@ export function useElementScrollRestoration(
4745
): ScrollRestorationEntry | undefined {
4846
useScrollRestoration()
4947

50-
const router = useRouter()
51-
const getKey = options.getKey || defaultGetScrollRestorationKey
52-
53-
let elementSelector = ''
54-
55-
if (options.id) {
56-
elementSelector = `[data-scroll-restoration-id="${options.id}"]`
57-
} else {
58-
const element = options.getElement?.()
59-
if (!element) {
60-
return
61-
}
62-
elementSelector =
63-
element instanceof Window ? 'window' : getCssSelector(element)
64-
}
65-
66-
const restoreKey = getKey(router.latestLocation)
67-
const byKey = scrollRestorationCache?.state[restoreKey]
68-
return byKey?.[elementSelector]
48+
return getElementScrollRestorationEntry(useRouter(), options)
6949
}

0 commit comments

Comments
 (0)