feat(theme): scroll toc if active item is out of view (#5377)

pull/5388/head
Bjorn Lu 3 weeks ago committed by GitHub
parent c197979993
commit 72534af30e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -37,6 +37,8 @@ defineProps<{
font-size: 0.875rem; font-size: 0.875rem;
font-weight: 400; font-weight: 400;
color: var(--vp-c-text-2); color: var(--vp-c-text-2);
scroll-margin-top: calc(var(--vp-nav-height) + var(--vp-layout-top-height, 0px) + var(--vp-doc-top-height, 0px) + 2rem);
scroll-margin-bottom: 3rem;
white-space: nowrap; white-space: nowrap;
overflow: hidden; overflow: hidden;
text-overflow: ellipsis; text-overflow: ellipsis;

@ -171,19 +171,17 @@ export function useActiveAnchor(
} }
function activateLink(hash: string | null) { function activateLink(hash: string | null) {
if (prevActiveLink) { const activeLink =
prevActiveLink.classList.remove('active') hash != null
} ? container.value.querySelector<HTMLAnchorElement>(
if (hash == null) {
prevActiveLink = null
} else {
prevActiveLink = container.value.querySelector(
`a[href$="${decodeURIComponent(hash)}"]` `a[href$="${decodeURIComponent(hash)}"]`
) )
} : null
if (activeLink === prevActiveLink) return
const activeLink = prevActiveLink prevActiveLink?.classList.remove('active')
prevActiveLink = activeLink
if (activeLink) { if (activeLink) {
activeLink.classList.add('active') activeLink.classList.add('active')
@ -195,6 +193,7 @@ export function useActiveAnchor(
(activeLink.offsetHeight - marker.value.offsetHeight) / 2 + (activeLink.offsetHeight - marker.value.offsetHeight) / 2 +
'px' 'px'
marker.value.style.opacity = '1' marker.value.style.opacity = '1'
activeLink.scrollIntoView({ block: 'nearest', behavior: 'smooth' })
} else { } else {
marker.value.style.top = '' marker.value.style.top = ''
marker.value.style.opacity = '0' marker.value.style.opacity = '0'

Loading…
Cancel
Save