Skip to content

Commit 9f549da

Browse files
feat: scrollRestorationBehavior per navigation
1 parent 11b0fc9 commit 9f549da

5 files changed

Lines changed: 19 additions & 1 deletion

File tree

packages/history/src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ export type ParsedHistoryState = HistoryState & {
7272
__TSR_index: number
7373
/** Whether to reset scroll position on this navigation (default: true) */
7474
__TSR_resetScroll?: boolean
75+
/** Scroll restoration behavior override for this navigation */
76+
__TSR_scrollRestorationBehavior?: ScrollBehavior
7577
/** Session id for cached TSR internals */
7678
__TSR_sessionId?: string
7779
/** Match snapshot for fast-path on back/forward navigation */

packages/router-core/src/RouterProvider.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ export interface CommitLocationOptions {
2020
**/
2121
startTransition?: boolean
2222
ignoreBlocker?: boolean
23+
scrollRestorationBehavior?: ScrollBehavior
2324
}
2425

2526
export type NavigateFn = <

packages/router-core/src/link.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,12 @@ export interface NavigateOptionProps {
348348
* @link [API Docs](https://tanstack.com/router/latest/docs/framework/react/api/router/NavigateOptionsType#reloaddocument)
349349
*/
350350
reloadDocument?: boolean
351+
/**
352+
* The scroll behavior to use for scroll restoration on this navigation.
353+
* If provided, this will override `router.options.scrollRestorationBehavior` for this navigation only.
354+
* @link [API Docs](https://tanstack.com/router/latest/docs/framework/react/api/router/NavigateOptionsType#scrollrestorationbehavior)
355+
*/
356+
scrollRestorationBehavior?: ScrollBehavior
351357
/**
352358
* This can be used instead of `to` to navigate to a fully built href, e.g. pointing to an external target.
353359
* @link [API Docs](https://tanstack.com/router/latest/docs/framework/react/api/router/NavigateOptionsType#href)

packages/router-core/src/router.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1960,6 +1960,7 @@ export class RouterCore<
19601960
commitLocation: CommitLocationFn = async ({
19611961
viewTransition,
19621962
ignoreBlocker,
1963+
scrollRestorationBehavior,
19631964
...next
19641965
}) => {
19651966
const isSameState = () => {
@@ -2035,6 +2036,10 @@ export class RouterCore<
20352036
nextHistory.state.__hashScrollIntoViewOptions =
20362037
hashScrollIntoView ?? this.options.defaultHashScrollIntoView ?? true
20372038

2039+
// Store scrollRestorationBehavior in history state so it survives back/forward navigation
2040+
nextHistory.state.__TSR_scrollRestorationBehavior =
2041+
scrollRestorationBehavior
2042+
20382043
// Store resetScroll in history state so it survives back/forward navigation
20392044
nextHistory.state.__TSR_resetScroll = next.resetScroll ?? true
20402045

@@ -2102,6 +2107,7 @@ export class RouterCore<
21022107
hashScrollIntoView,
21032108
viewTransition,
21042109
ignoreBlocker,
2110+
scrollRestorationBehavior,
21052111
href,
21062112
...rest
21072113
}: BuildNextOptions & CommitLocationOptions = {}) => {
@@ -2140,6 +2146,7 @@ export class RouterCore<
21402146
resetScroll,
21412147
hashScrollIntoView,
21422148
ignoreBlocker,
2149+
scrollRestorationBehavior,
21432150
})
21442151

21452152
// Clear pending location after commit starts

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,9 @@ export function setupScrollRestoration(router: AnyRouter, force?: boolean) {
356356
restoreScroll({
357357
storageKey,
358358
key: cacheKey,
359-
behavior: router.options.scrollRestorationBehavior,
359+
behavior:
360+
event.toLocation.state.__TSR_scrollRestorationBehavior ??
361+
router.options.scrollRestorationBehavior,
360362
shouldScrollRestoration: router.isScrollRestoring,
361363
scrollToTopSelectors: router.options.scrollToTopSelectors,
362364
location: router.history.location,

0 commit comments

Comments
 (0)