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 1.1k
/
default-router.options.ts
60 lines (52 loc) · 1.75 KB
/
default-router.options.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import type { RouterConfig } from '@nuxt/schema'
import { nextTick } from 'vue'
import { useNuxtApp } from '#app'
// https://router.vuejs.org/api/#routeroptions
export default <RouterConfig>{
scrollBehavior (to, from, savedPosition) {
const nuxtApp = useNuxtApp()
// If the returned position is falsy or an empty object, will retain current scroll position
let position
const isRouteChanged = to !== from
// savedPosition is only available for popstate navigations (back button)
if (savedPosition) {
position = savedPosition
} else if (isRouteChanged) {
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 }
} else if (to.hash) {
return {
el: to.hash,
top: getHashElementScrollMarginTop()
}
}
}
// if either to or from has no transition then wait for page:finish
const hasTransition = to.meta.pageTransition !== false && from.meta.pageTransition !== false
const hookAwait = hasTransition ? 'page:transition:finish' : 'page:finish'
function getHashElementScrollMarginTop () {
// vue-router does not incorporate scroll-margin-top on its own.
const elem = document.querySelector(to.hash)
if (elem) {
return parseFloat(getComputedStyle(elem).scrollMarginTop)
}
return 0
}
return new Promise((resolve) => {
nuxtApp.hooks.hookOnce(hookAwait, async () => {
await nextTick()
if (to.hash) {
position = {
el: to.hash,
top: getHashElementScrollMarginTop()
}
}
resolve(position)
})
})
}
}