Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Improve Sidebar and Aside Link Visibility on Mount and Route Change #3901

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/client/theme-default/components/VPDocAside.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@
import { useData } from '../composables/data'
import VPDocAsideOutline from './VPDocAsideOutline.vue'
import VPDocAsideCarbonAds from './VPDocAsideCarbonAds.vue'
import { ref, provide } from 'vue'

const { theme } = useData()
const aside = ref(null)

provide('aside', aside)
</script>

<template>
<div class="VPDocAside">
<div class="VPDocAside" ref="aside">
<slot name="aside-top" />

<slot name="aside-outline-before" />
Expand Down
5 changes: 3 additions & 2 deletions src/client/theme-default/components/VPDocAsideOutline.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<script setup lang="ts">
import { onContentUpdated } from 'vitepress'
import { ref, shallowRef } from 'vue'
import { inject, ref, type Ref, shallowRef } from 'vue'
import { useData } from '../composables/data'
import {
getHeaders,
Expand All @@ -18,10 +18,11 @@ onContentUpdated(() => {
headers.value = getHeaders(frontmatter.value.outline ?? theme.value.outline)
})

const aside = inject('aside') as Ref<HTMLElement>
const container = ref()
const marker = ref()

useActiveAnchor(container, marker)
useActiveAnchor(aside, container, marker)
</script>

<template>
Expand Down
32 changes: 29 additions & 3 deletions src/client/theme-default/components/VPSidebar.vue
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<script lang="ts" setup>
import { useScrollLock } from '@vueuse/core'
import { inBrowser } from 'vitepress'
import { ref, watch } from 'vue'
import { inBrowser, useRoute } from 'vitepress'
import { nextTick, onMounted, ref, watch } from 'vue'
import { useSidebar } from '../composables/sidebar'
import VPSidebarItem from './VPSidebarItem.vue'

Expand All @@ -14,6 +14,7 @@ const props = defineProps<{
// a11y: focus Nav element when menu has opened
const navEl = ref<HTMLElement | null>(null)
const isLocked = useScrollLock(inBrowser ? document.body : null)
const route = useRoute()

watch(
[props, navEl],
Expand All @@ -25,6 +26,31 @@ watch(
},
{ immediate: true, flush: 'post' }
)

function scrollActiveLinkIntoView() {
const activeLink = navEl.value?.querySelector('.is-active')
if (!activeLink) return
const rect = activeLink.getBoundingClientRect()
const isInViewPort = rect.top >= 65 && rect.bottom <= innerHeight
if (!isInViewPort) {
activeLink.scrollIntoView({ block: 'center' })
}
}

function handleScroll() {
nextTick(() => scrollActiveLinkIntoView())
}


onMounted(handleScroll)
watch([props, navEl, route], () => {
if (props.open) {
isLocked.value = true
navEl.value?.focus()
} else isLocked.value = false

handleScroll()
}, { immediate: true, flush: 'post' })
</script>

<template>
Expand Down Expand Up @@ -78,7 +104,7 @@ watch(
visibility: visible;
transform: translateX(0);
transition: opacity 0.25s,
transform 0.5s cubic-bezier(0.19, 1, 0.22, 1);
transform 0.5s cubic-bezier(0.19, 1, 0.22, 1);
}

.dark .VPSidebar {
Expand Down
8 changes: 7 additions & 1 deletion src/client/theme-default/composables/outline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ export function resolveHeaders(
}

export function useActiveAnchor(
aside: Ref<HTMLElement>,
container: Ref<HTMLElement>,
marker: Ref<HTMLElement>
) {
Expand Down Expand Up @@ -194,14 +195,19 @@ export function useActiveAnchor(
}

const activeLink = prevActiveLink

if (activeLink) {
const rect = activeLink.getBoundingClientRect()
const isInViewPort = rect.top >= 65 && rect.bottom <= innerHeight
activeLink.classList.add('active')
marker.value.style.top = activeLink.offsetTop + 39 + 'px'
marker.value.style.opacity = '1'
if (!isInViewPort) {
activeLink.scrollIntoView({ block: 'center' })
}
} else {
marker.value.style.top = '33px'
marker.value.style.opacity = '0'
aside.value.scrollTop = 0
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello, may I ask what this line of code is doing? I saw in the developer tools that the aside itself does not generate a scroll bar, so scrollTop remains 0 all the time. Can this piece of code and related parts be removed?

}
}
}
Expand Down