Commit 3d556c0bdd

3d556c0bddf989372e50e961caa291c4f2097f9f

parent: 7ddff27a9c

Unsigned

Duck <57977673+woducku@users.noreply.github.com> · 2026-07-08 15:52 UTC

toc highlight fix

Layout: unified · split

docs/.vitepress/theme/Layout.vue +53 −3
@@ -13,7 +13,7 @@ import {
1313 useThrottleFn
1414} from '@vueuse/core'
1515import { usePreferredReducedMotion } from '@vueuse/core'
16import { useData, useRoute } from 'vitepress'
16import { getScrollOffset, useData, useRoute } from 'vitepress'
1717import type { DefaultTheme as Theme } from 'vitepress'
1818import VPSidebarGroup from 'vitepress/dist/client/theme-default/components/VPSidebarGroup.vue'
1919import { getSidebarGroups } from 'vitepress/dist/client/theme-default/support/sidebar'
@@ -567,6 +567,51 @@ const rewriteAnchorLinksDeferred = () => nextTick(() => rewriteAnchorLinks())
567567let outlineFollowObserver: MutationObserver | null = null
568568let outlineFollowRaf = 0
569569
570const syncMobileOutlineActive = () => {
571 const dropdownLinks = Array.from(
572 document.querySelectorAll<HTMLAnchorElement>(
573 '.VPLocalNavOutlineDropdown .outline a.outline-link'
574 )
575 )
576 if (!dropdownLinks.length) return
577
578 const scrollY = window.scrollY
579 const isBottom =
580 Math.abs(scrollY + window.innerHeight - document.body.offsetHeight) < 1
581
582 const entries = dropdownLinks
583 .map(link => {
584 const href = link.getAttribute('href') ?? ''
585 const id = href.startsWith('#') ? decodeURIComponent(href.slice(1)) : ''
586 const el = id ? document.getElementById(id) : null
587 return el ? { href, top: el.getBoundingClientRect().top + scrollY } : null
588 })
589 .filter((e): e is { href: string; top: number } => e !== null)
590 .sort((a, b) => a.top - b.top)
591
592 let activeHref: string | null = null
593 if (entries.length && scrollY >= 1) {
594 if (isBottom) {
595 activeHref = entries[entries.length - 1].href
596 } else {
597 for (const { href, top } of entries) {
598 if (top > scrollY + getScrollOffset() + 4) break
599 activeHref = href
600 }
601 }
602 }
603
604 dropdownLinks.forEach(link => {
605 link.classList.toggle('active', link.getAttribute('href') === activeHref)
606 })
607}
608
609let mobileOutlineScrollRaf = 0
610const onMobileOutlineScroll = () => {
611 cancelAnimationFrame(mobileOutlineScrollRaf)
612 mobileOutlineScrollRaf = requestAnimationFrame(syncMobileOutlineActive)
613}
614
570615const followActiveOutlineLink = () => {
571616 cancelAnimationFrame(outlineFollowRaf)
572617
@@ -653,7 +698,7 @@ function addBookmarkBtnToLi(li: HTMLElement, path: string) {
653698 btn.classList.toggle('is-bookmarked', isBookmarked(anchor, path))
654699 })
655700
656 li.appendChild(btn)
701 link.appendChild(btn)
657702}
658703
659704let tocMutationObserver: MutationObserver | null = null
@@ -675,7 +720,10 @@ function setupTocBookmarks() {
675720 tocMutationObserver?.disconnect()
676721 tocMutationObserver = new MutationObserver(() => {
677722 cancelAnimationFrame(tocRaf)
678 tocRaf = requestAnimationFrame(() => scanTocItems(route.path))
723 tocRaf = requestAnimationFrame(() => {
724 scanTocItems(route.path)
725 syncMobileOutlineActive()
726 })
679727 })
680728 tocMutationObserver.observe(document.body, { childList: true, subtree: true })
681729}
@@ -684,6 +732,8 @@ onMounted(tryOpenAnchoredContent)
684732onMounted(rewriteAnchorLinksDeferred)
685733onMounted(() => nextTick(setupOutlineFollow))
686734onMounted(() => nextTick(setupTocBookmarks))
735onMounted(() => window.addEventListener('scroll', onMobileOutlineScroll, { passive: true }))
736onUnmounted(() => window.removeEventListener('scroll', onMobileOutlineScroll))
687737watch(() => route.data, () => {
688738 void tryOpenAnchoredContent()
689739 rewriteAnchorLinksDeferred()
docs/.vitepress/theme/styles/style.scss +14 −5
@@ -880,11 +880,16 @@ table tr:nth-child(odd) {
880880
881881 & > a {
882882 &:hover,
883 &:focus {
883 &:focus,
884 &.active {
884885 @apply rounded-xl;
885886 background-color: var(--vp-c-brand-nav-active);
886887 color: var(--vp-c-brand);
887888 }
889
890 &.active {
891 @apply font-semibold;
892 }
888893 }
889894
890895 & > ul {
@@ -899,11 +904,16 @@ table tr:nth-child(odd) {
899904
900905 & > a {
901906 &:hover,
902 &:focus {
907 &:focus,
908 &.active {
903909 @apply rounded-lg;
904910 background-color: var(--vp-c-brand-nav-active);
905911 color: var(--vp-c-brand);
906912 }
913
914 &.active {
915 @apply font-semibold;
916 }
907917 }
908918 }
909919 }
@@ -1232,7 +1242,6 @@ table tr:nth-child(odd) {
12321242 }
12331243
12341244 .VPDocOutlineItem.root > li {
1235 position: relative;
12361245
12371246 &:hover .toc-bookmark-btn,
12381247 & .toc-bookmark-btn.is-bookmarked {
@@ -1240,6 +1249,7 @@ table tr:nth-child(odd) {
12401249 }
12411250
12421251 & > .outline-link {
1252 position: relative;
12431253 padding-right: 20px;
12441254 @apply text-neutral-600/80 dark:text-neutral-300/80 transition-all
12451255 dark:hover:text-neutral-300 dark:focus:text-neutral-300;
@@ -1311,9 +1321,8 @@ table tr:nth-child(odd) {
13111321}
13121322
13131323.VPLocalNavOutlineDropdown .outline > .VPDocOutlineItem > li {
1314 position: relative;
1315
13161324 & > .outline-link {
1325 position: relative;
13171326 padding-right: 28px !important;
13181327 overflow: hidden !important;
13191328 text-overflow: ellipsis !important;