Skip to content

Commit 3f858cb

Browse files
authored
fix: scroll to top selectors function (#4281)
1 parent 18e24a7 commit 3f858cb

File tree

3 files changed

+22
-6
lines changed

3 files changed

+22
-6
lines changed

docs/router/framework/react/guide/scroll-restoration.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,19 @@ const router = createRouter({
1717
})
1818
```
1919

20+
For complex selectors that cannot be simply resolved using `document.querySelector(selector)`, you can pass functions that return HTML elements to `routerOptions.scrollToTopSelectors`:
21+
22+
```tsx
23+
const selector = () =>
24+
document
25+
.querySelector('#shadowRootParent')
26+
?.shadowRoot?.querySelector('#main-scrollable-area')
27+
28+
const router = createRouter({
29+
scrollToTopSelectors: [selector],
30+
})
31+
```
32+
2033
These selectors are handled **in addition to `window`** which cannot be disabled currently.
2134

2235
## Scroll Restoration

packages/router-core/src/router.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,7 @@ export interface RouterOptions<
390390
*
391391
* @default ['window']
392392
*/
393-
scrollToTopSelectors?: Array<string>
393+
scrollToTopSelectors?: Array<string | (() => Element | null | undefined)>
394394
}
395395

396396
export interface RouterState<
@@ -882,7 +882,6 @@ export class RouterCore<
882882
}
883883

884884
if (
885-
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
886885
!this.history ||
887886
(this.options.history && this.options.history !== this.history)
888887
) {
@@ -901,7 +900,6 @@ export class RouterCore<
901900
this.buildRouteTree()
902901
}
903902

904-
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
905903
if (!this.__store) {
906904
this.__store = new Store(getInitialRouterState(this.latestLocation), {
907905
onUpdate: () => {
@@ -920,7 +918,6 @@ export class RouterCore<
920918
if (
921919
typeof window !== 'undefined' &&
922920
'CSS' in window &&
923-
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
924921
typeof window.CSS?.supports === 'function'
925922
) {
926923
this.isViewTransitionTypesSupported = window.CSS.supports(

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,9 @@ export function restoreScroll(
105105
key: string | undefined,
106106
behavior: ScrollToOptions['behavior'] | undefined,
107107
shouldScrollRestoration: boolean | undefined,
108-
scrollToTopSelectors: Array<string> | undefined,
108+
scrollToTopSelectors:
109+
| Array<string | (() => Element | null | undefined)>
110+
| undefined,
109111
) {
110112
let byKey: ScrollRestorationByKey
111113

@@ -174,7 +176,11 @@ export function restoreScroll(
174176
...(scrollToTopSelectors?.filter((d) => d !== 'window') ?? []),
175177
].forEach((selector) => {
176178
const element =
177-
selector === 'window' ? window : document.querySelector(selector)
179+
selector === 'window'
180+
? window
181+
: typeof selector === 'function'
182+
? selector()
183+
: document.querySelector(selector)
178184
if (element) {
179185
element.scrollTo({
180186
top: 0,

0 commit comments

Comments
 (0)