mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-22 05:35:13 +00:00
feat(nuxt): allow dynamic scrollToTop
page meta (#21741)
This commit is contained in:
parent
fc10da37fb
commit
2616aadda6
@ -32,6 +32,7 @@ interface PageMeta {
|
||||
keepalive?: boolean | KeepAliveProps
|
||||
layout?: false | LayoutKey | Ref<LayoutKey> | ComputedRef<LayoutKey>
|
||||
middleware?: MiddlewareKey | NavigationGuard | Array<MiddlewareKey | NavigationGuard>
|
||||
scrollToTop?: boolean | ((to: RouteLocationNormalizedLoaded, from: RouteLocationNormalizedLoaded) => boolean)
|
||||
[key: string]: unknown
|
||||
}
|
||||
```
|
||||
@ -98,6 +99,12 @@ interface PageMeta {
|
||||
|
||||
Validate whether a given route can validly be rendered with this page. Return true if it is valid, or false if not. If another match can't be found, this will mean a 404. You can also directly return an object with `statusCode`/`statusMessage` to respond immediately with an error (other matches will not be checked).
|
||||
|
||||
**`scrollTopTop`**
|
||||
|
||||
- **Type**: `boolean | (to: RouteLocationNormalized, from: RouteLocationNormalized) => boolean`
|
||||
|
||||
Tell Nuxt to scroll to the top before rendering the page or not. If you want to overwrite the default scroll behavior of Nuxt, you can do so in `~/app/router.options.ts` (see [docs](/docs/guide/directory-structure/pages/#router-options)) for more info.
|
||||
|
||||
**`[key: string]`**
|
||||
|
||||
- **Type**: `any`
|
||||
|
@ -36,7 +36,7 @@ export interface PageMeta {
|
||||
/** You may define a path matcher, if you have a more complex pattern than can be expressed with the file name. */
|
||||
path?: string
|
||||
/** Set to `false` to avoid scrolling to top on page navigations */
|
||||
scrollToTop?: boolean
|
||||
scrollToTop?: boolean | ((to: RouteLocationNormalizedLoaded, from: RouteLocationNormalizedLoaded) => boolean)
|
||||
}
|
||||
|
||||
declare module 'vue-router' {
|
||||
|
@ -20,8 +20,10 @@ export default <RouterConfig> {
|
||||
// savedPosition is only available for popstate navigations (back button)
|
||||
let position: ScrollPosition = savedPosition || undefined
|
||||
|
||||
const routeAllowsScrollToTop = typeof to.meta.scrollToTop === 'function' ? to.meta.scrollToTop(to, from) : to.meta.scrollToTop
|
||||
|
||||
// Scroll to top if route is changed by default
|
||||
if (!position && from && to && to.meta.scrollToTop !== false && _isDifferentRoute(from, to)) {
|
||||
if (!position && from && to && routeAllowsScrollToTop !== false && _isDifferentRoute(from, to)) {
|
||||
position = { left: 0, top: 0 }
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user