Skip to content

Commit 08b9e9a

Browse files
zh-lxmeteorlxy
andauthored
feat(theme-default): scroll active sidebar item into view (#698)
Co-authored-by: meteorlxy <[email protected]>
1 parent b06ca39 commit 08b9e9a

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

packages/@vuepress/theme-default/src/client/components/SidebarItems.vue

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,46 @@
11
<script setup lang="ts">
22
import SidebarItem from '@theme/SidebarItem.vue'
3+
import { watch } from 'vue'
4+
import { useRoute } from 'vue-router'
35
import { useSidebarItems } from '../composables'
46
7+
const route = useRoute()
58
const sidebarItems = useSidebarItems()
9+
10+
watch(
11+
() => route.hash,
12+
(hash) => {
13+
// get the sidebar DOM
14+
const sidebar = document.querySelector('.sidebar')
15+
if (!sidebar) return
16+
17+
// get the active sidebar item DOM, whose href equals to the current route
18+
const activeSidebarItem = document.querySelector(
19+
`.sidebar a.sidebar-item[href="${route.path}${hash}"]`
20+
)
21+
if (!activeSidebarItem) return
22+
23+
// get the top and height of the sidebar
24+
const sidebarTop = sidebar.getBoundingClientRect().top
25+
const sidebarHeight = sidebar.getBoundingClientRect().height
26+
27+
// get the top and height of the active sidebar item
28+
const activeSidebarItemTop = activeSidebarItem.getBoundingClientRect().top
29+
const activeSidebarItemHeight =
30+
activeSidebarItem.getBoundingClientRect().height
31+
32+
if (activeSidebarItemTop < sidebarTop) {
33+
// scroll to the top edge of sidebar when the active sidebar item overflows the top edge of sidebar
34+
activeSidebarItem.scrollIntoView(true)
35+
} else if (
36+
activeSidebarItemTop + activeSidebarItemHeight >
37+
sidebarTop + sidebarHeight
38+
) {
39+
// scroll to the bottom edge of sidebar when the active sidebar item overflows the bottom edge of sidebar
40+
activeSidebarItem.scrollIntoView(false)
41+
}
42+
}
43+
)
644
</script>
745

846
<template>

0 commit comments

Comments
 (0)