Skip to content

Commit 644d550

Browse files
authored
Merge pull request #269 from filecoin-project/feat/scroll-to-view
feat: auto-scroll sidebar active item into view
2 parents efb778b + 2b557d9 commit 644d550

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

docs/.vuepress/theme/components/ScrollPatch.vue

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<script>
33
export default {
44
methods: {
5-
scrollTop: function() {
5+
scrollTop: function () {
66
// FireFox has a problem setting the correct scroll postion on route change this patch will fix it for now
77
if (navigator.userAgent.toLowerCase().indexOf('firefox') > -1) {
88
window.scrollTo(0, 0)
@@ -13,8 +13,24 @@ export default {
1313
}
1414
},
1515
watch: {
16-
'$route.path': function(path) {
16+
'$route.path': function (path) {
1717
this.scrollTop()
18+
},
19+
$route: function () {
20+
// activates on every route change
21+
const items = document.querySelectorAll('.sidebar-links .active')
22+
if (items.length && document.documentElement.scrollIntoView) {
23+
const lastItem = items.item(items.length - 1)
24+
try {
25+
lastItem.scrollIntoView({
26+
behavior: 'smooth',
27+
block: 'center',
28+
inline: 'nearest'
29+
})
30+
} catch (e) {
31+
lastItem.scrollIntoView(false)
32+
}
33+
}
1834
}
1935
}
2036
}

0 commit comments

Comments
 (0)