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