File tree Expand file tree Collapse file tree 3 files changed +22
-6
lines changed
docs/router/framework/react/guide Expand file tree Collapse file tree 3 files changed +22
-6
lines changed Original file line number Diff line number Diff line change @@ -17,6 +17,19 @@ const router = createRouter({
17
17
})
18
18
```
19
19
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
+
20
33
These selectors are handled ** in addition to ` window ` ** which cannot be disabled currently.
21
34
22
35
## Scroll Restoration
Original file line number Diff line number Diff line change @@ -390,7 +390,7 @@ export interface RouterOptions<
390
390
*
391
391
* @default ['window']
392
392
*/
393
- scrollToTopSelectors ?: Array < string >
393
+ scrollToTopSelectors ?: Array < string | ( ( ) => Element | null | undefined ) >
394
394
}
395
395
396
396
export interface RouterState <
@@ -882,7 +882,6 @@ export class RouterCore<
882
882
}
883
883
884
884
if (
885
- // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
886
885
! this . history ||
887
886
( this . options . history && this . options . history !== this . history )
888
887
) {
@@ -901,7 +900,6 @@ export class RouterCore<
901
900
this . buildRouteTree ( )
902
901
}
903
902
904
- // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
905
903
if ( ! this . __store ) {
906
904
this . __store = new Store ( getInitialRouterState ( this . latestLocation ) , {
907
905
onUpdate : ( ) => {
@@ -920,7 +918,6 @@ export class RouterCore<
920
918
if (
921
919
typeof window !== 'undefined' &&
922
920
'CSS' in window &&
923
- // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
924
921
typeof window . CSS ?. supports === 'function'
925
922
) {
926
923
this . isViewTransitionTypesSupported = window . CSS . supports (
Original file line number Diff line number Diff line change @@ -105,7 +105,9 @@ export function restoreScroll(
105
105
key : string | undefined ,
106
106
behavior : ScrollToOptions [ 'behavior' ] | undefined ,
107
107
shouldScrollRestoration : boolean | undefined ,
108
- scrollToTopSelectors : Array < string > | undefined ,
108
+ scrollToTopSelectors :
109
+ | Array < string | ( ( ) => Element | null | undefined ) >
110
+ | undefined ,
109
111
) {
110
112
let byKey : ScrollRestorationByKey
111
113
@@ -174,7 +176,11 @@ export function restoreScroll(
174
176
...( scrollToTopSelectors ?. filter ( ( d ) => d !== 'window' ) ?? [ ] ) ,
175
177
] . forEach ( ( selector ) => {
176
178
const element =
177
- selector === 'window' ? window : document . querySelector ( selector )
179
+ selector === 'window'
180
+ ? window
181
+ : typeof selector === 'function'
182
+ ? selector ( )
183
+ : document . querySelector ( selector )
178
184
if ( element ) {
179
185
element . scrollTo ( {
180
186
top : 0 ,
You can’t perform that action at this time.
0 commit comments