This repository has been archived by the owner on Apr 6, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(nuxt): default router scroll behavior (#3851)
Co-authored-by: joel <[email protected]> Co-authored-by: Pooya Parsa <[email protected]>
- Loading branch information
1 parent
66de87a
commit ba3a118
Showing
6 changed files
with
91 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import type { RouterConfig } from '@nuxt/schema' | ||
import type { RouterScrollBehavior } from 'vue-router' | ||
import { nextTick } from 'vue' | ||
import { useNuxtApp } from '#app' | ||
|
||
type ScrollPosition = Awaited<ReturnType<RouterScrollBehavior>> | ||
|
||
// Default router options | ||
// https://router.vuejs.org/api/#routeroptions | ||
export default <RouterConfig> { | ||
scrollBehavior (to, from, savedPosition) { | ||
const nuxtApp = useNuxtApp() | ||
|
||
// By default when the returned position is falsy or an empty object, vue-router will retain the current scroll position | ||
// savedPosition is only available for popstate navigations (back button) | ||
let position: ScrollPosition = savedPosition || undefined | ||
|
||
// Scroll to top if route is changed by default | ||
if ( | ||
!position && | ||
(from && to && from.matched[0] !== to.matched[0]) && | ||
to.meta.scrollToTop !== false | ||
) { | ||
position = { left: 0, top: 0 } | ||
} | ||
|
||
// Hash routes on the same page, no page hook is fired so resolve here | ||
if (to.path !== from.path) { | ||
if (from.hash && !to.hash) { | ||
return { left: 0, top: 0 } | ||
} | ||
if (to.hash) { | ||
return { el: to.hash, top: _getHashElementScrollMarginTop(to.hash) } | ||
} | ||
} | ||
|
||
// Wait for `page:transition:finish` or `page:finish` depending on if transitions are enabled or not | ||
const hasTransition = to.meta.pageTransition !== false && from.meta.pageTransition !== false | ||
const hookToWait = hasTransition ? 'page:transition:finish' : 'page:finish' | ||
return new Promise((resolve) => { | ||
nuxtApp.hooks.hookOnce(hookToWait, async () => { | ||
await nextTick() | ||
if (to.hash) { | ||
position = { el: to.hash, top: _getHashElementScrollMarginTop(to.hash) } | ||
} | ||
resolve(position) | ||
}) | ||
}) | ||
} | ||
} | ||
|
||
function _getHashElementScrollMarginTop (selector: string): number { | ||
const elem = document.querySelector(selector) | ||
if (elem) { | ||
return parseFloat(getComputedStyle(elem).scrollMarginTop) | ||
} | ||
return 0 | ||
} |