Skip to content

Commit 350fcd8

Browse files
committed
Add smooth scroll options in blog
Signed-off-by: nurRiyad <[email protected]>
1 parent 95a27d9 commit 350fcd8

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

app/router.options.ts

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import type { RouterConfig } from '@nuxt/schema'
2+
3+
// https://router.vuejs.org/api/#routeroptions
4+
export default <RouterConfig>{
5+
scrollBehavior: (to, from, savedPosition) => {
6+
// scroll to hash, useful for using to="#some-id" in NuxtLink
7+
// ex: <NuxtLink to="#top"> To Top </ NuxtLink>
8+
if (to.hash) {
9+
// console.log('to.hash: ', to.hash)
10+
return {
11+
el: to.hash,
12+
top: 100,
13+
behavior: 'smooth',
14+
}
15+
}
16+
17+
// The remainder is optional but maybe useful as well
18+
19+
// if link is to same page, scroll to top with smooth behavior
20+
if (to === from) {
21+
return {
22+
left: 0,
23+
top: 0,
24+
behavior: 'smooth',
25+
}
26+
}
27+
28+
// this will use saved scroll position on browser forward/back navigation
29+
return new Promise((resolve) => {
30+
setTimeout(() => {
31+
resolve({
32+
left: savedPosition?.left || 0,
33+
top: savedPosition?.top || 0,
34+
})
35+
}, 500)
36+
})
37+
},
38+
}

components/blog/Toc.vue

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
const { path } = useRoute()
33
const articles = await queryContent(path).findOne()
44
5-
const links = articles.body.toc.links
5+
const links = articles?.body?.toc?.links || []
66
</script>
77

88
<template>

0 commit comments

Comments
 (0)