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 {
13 useThrottleFn 13 useThrottleFn
14} from '@vueuse/core' 14} from '@vueuse/core'
15import { usePreferredReducedMotion } from '@vueuse/core' 15import { usePreferredReducedMotion } from '@vueuse/core'
16import { useData, useRoute } from 'vitepress' 16import { getScrollOffset, useData, useRoute } from 'vitepress'
17import type { DefaultTheme as Theme } from 'vitepress' 17import type { DefaultTheme as Theme } from 'vitepress'
18import VPSidebarGroup from 'vitepress/dist/client/theme-default/components/VPSidebarGroup.vue' 18import VPSidebarGroup from 'vitepress/dist/client/theme-default/components/VPSidebarGroup.vue'
19import { getSidebarGroups } from 'vitepress/dist/client/theme-default/support/sidebar' 19import { getSidebarGroups } from 'vitepress/dist/client/theme-default/support/sidebar'
@@ -567,6 +567,51 @@ const rewriteAnchorLinksDeferred = () => nextTick(() => rewriteAnchorLinks())
567let outlineFollowObserver: MutationObserver | null = null 567let outlineFollowObserver: MutationObserver | null = null
568let outlineFollowRaf = 0 568let outlineFollowRaf = 0
569 569
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
570const followActiveOutlineLink = () => { 615const followActiveOutlineLink = () => {
571 cancelAnimationFrame(outlineFollowRaf) 616 cancelAnimationFrame(outlineFollowRaf)
572 617
@@ -653,7 +698,7 @@ function addBookmarkBtnToLi(li: HTMLElement, path: string) {
653 btn.classList.toggle('is-bookmarked', isBookmarked(anchor, path)) 698 btn.classList.toggle('is-bookmarked', isBookmarked(anchor, path))
654 }) 699 })
655 700
656 li.appendChild(btn) 701 link.appendChild(btn)
657} 702}
658 703
659let tocMutationObserver: MutationObserver | null = null 704let tocMutationObserver: MutationObserver | null = null
@@ -675,7 +720,10 @@ function setupTocBookmarks() {
675 tocMutationObserver?.disconnect() 720 tocMutationObserver?.disconnect()
676 tocMutationObserver = new MutationObserver(() => { 721 tocMutationObserver = new MutationObserver(() => {
677 cancelAnimationFrame(tocRaf) 722 cancelAnimationFrame(tocRaf)
678 tocRaf = requestAnimationFrame(() => scanTocItems(route.path)) 723 tocRaf = requestAnimationFrame(() => {
724 scanTocItems(route.path)
725 syncMobileOutlineActive()
726 })
679 }) 727 })
680 tocMutationObserver.observe(document.body, { childList: true, subtree: true }) 728 tocMutationObserver.observe(document.body, { childList: true, subtree: true })
681} 729}
@@ -684,6 +732,8 @@ onMounted(tryOpenAnchoredContent)
684onMounted(rewriteAnchorLinksDeferred) 732onMounted(rewriteAnchorLinksDeferred)
685onMounted(() => nextTick(setupOutlineFollow)) 733onMounted(() => nextTick(setupOutlineFollow))
686onMounted(() => nextTick(setupTocBookmarks)) 734onMounted(() => nextTick(setupTocBookmarks))
735onMounted(() => window.addEventListener('scroll', onMobileOutlineScroll, { passive: true }))
736onUnmounted(() => window.removeEventListener('scroll', onMobileOutlineScroll))
687watch(() => route.data, () => { 737watch(() => route.data, () => {
688 void tryOpenAnchoredContent() 738 void tryOpenAnchoredContent()
689 rewriteAnchorLinksDeferred() 739 rewriteAnchorLinksDeferred()
docs/.vitepress/theme/styles/style.scss +14 −5
@@ -880,11 +880,16 @@ table tr:nth-child(odd) {
880 880
881 & > a { 881 & > a {
882 &:hover, 882 &:hover,
883 &:focus { 883 &:focus,
884 &.active {
884 @apply rounded-xl; 885 @apply rounded-xl;
885 background-color: var(--vp-c-brand-nav-active); 886 background-color: var(--vp-c-brand-nav-active);
886 color: var(--vp-c-brand); 887 color: var(--vp-c-brand);
887 } 888 }
889
890 &.active {
891 @apply font-semibold;
892 }
888 } 893 }
889 894
890 & > ul { 895 & > ul {
@@ -899,11 +904,16 @@ table tr:nth-child(odd) {
899 904
900 & > a { 905 & > a {
901 &:hover, 906 &:hover,
902 &:focus { 907 &:focus,
908 &.active {
903 @apply rounded-lg; 909 @apply rounded-lg;
904 background-color: var(--vp-c-brand-nav-active); 910 background-color: var(--vp-c-brand-nav-active);
905 color: var(--vp-c-brand); 911 color: var(--vp-c-brand);
906 } 912 }
913
914 &.active {
915 @apply font-semibold;
916 }
907 } 917 }
908 } 918 }
909 } 919 }
@@ -1232,7 +1242,6 @@ table tr:nth-child(odd) {
1232 } 1242 }
1233 1243
1234 .VPDocOutlineItem.root > li { 1244 .VPDocOutlineItem.root > li {
1235 position: relative;
1236 1245
1237 &:hover .toc-bookmark-btn, 1246 &:hover .toc-bookmark-btn,
1238 & .toc-bookmark-btn.is-bookmarked { 1247 & .toc-bookmark-btn.is-bookmarked {
@@ -1240,6 +1249,7 @@ table tr:nth-child(odd) {
1240 } 1249 }
1241 1250
1242 & > .outline-link { 1251 & > .outline-link {
1252 position: relative;
1243 padding-right: 20px; 1253 padding-right: 20px;
1244 @apply text-neutral-600/80 dark:text-neutral-300/80 transition-all 1254 @apply text-neutral-600/80 dark:text-neutral-300/80 transition-all
1245 dark:hover:text-neutral-300 dark:focus:text-neutral-300; 1255 dark:hover:text-neutral-300 dark:focus:text-neutral-300;
@@ -1311,9 +1321,8 @@ table tr:nth-child(odd) {
1311} 1321}
1312 1322
1313.VPLocalNavOutlineDropdown .outline > .VPDocOutlineItem > li { 1323.VPLocalNavOutlineDropdown .outline > .VPDocOutlineItem > li {
1314 position: relative;
1315
1316 & > .outline-link { 1324 & > .outline-link {
1325 position: relative;
1317 padding-right: 28px !important; 1326 padding-right: 28px !important;
1318 overflow: hidden !important; 1327 overflow: hidden !important;
1319 text-overflow: ellipsis !important; 1328 text-overflow: ellipsis !important;