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

fix: add click and scroll logic to fix anchor link behaviour issue #119

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
48 changes: 43 additions & 5 deletions src/vitepress/composables/outline.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { computed, inject, onMounted, onUnmounted, onUpdated, Ref } from 'vue'
import { computed, inject, onMounted, onUnmounted, onUpdated, ref, Ref } from 'vue'
import { Header, useData } from 'vitepress'
import { useMediaQuery } from '@vueuse/core'
import { MenuItemWithLink } from '../../core'
Expand Down Expand Up @@ -39,9 +39,31 @@ export function useActiveAnchor(
bg: Ref<HTMLElement>
) {
const isOutlineEnabled = useMediaQuery('(min-width: 1280px)')
const onScroll = throttleAndDebounce(setActiveLink, 100)
const clickedHash = ref<string | null>(null)
const scrollTimer = ref<any>(null)

function setActiveLink(): void {
const onScroll = throttleAndDebounce(() => {
scrollTimer.value && clearTimeout(scrollTimer.value)

scrollTimer.value = setTimeout(() => setActiveLink('scroll'), 150)
}, 100)

const handleAnchorLinkClick = (event: Event) => {
const { target } = event as MouseEvent
const anchor = target as HTMLAnchorElement

if (!anchor.classList.contains('outline-link')) return

clickedHash.value = anchor.hash
setActiveLink('click')

if (scrollTimer.value) {
clearTimeout(scrollTimer.value)
scrollTimer.value = null
}
}

function setActiveLink(actionType: 'scroll' | 'click'): void {
if (!isOutlineEnabled.value) {
return
}
Expand All @@ -58,23 +80,34 @@ export function useActiveAnchor(
)
) as HTMLAnchorElement[]

if (actionType === 'click' && clickedHash.value) {
activateLink(clickedHash.value)
return
}

// page bottom - highlight last one
if (
actionType === 'scroll' &&
anchors.length &&
// https://github.com/vuejs/theme/pull/74
window.scrollY + window.innerHeight >= document.body.offsetHeight - 1
) {
activateLink(anchors[anchors.length - 1].hash)
// if there was a click, and there was no scroll after that, keep the clicked element active
const targetHash = clickedHash.value || anchors[anchors.length - 1].hash
activateLink(targetHash)

return
}

// regular behavior during scroll
for (let i = 0; i < anchors.length; i++) {
const anchor = anchors[i]
const nextAnchor = anchors[i + 1]

const [isActive, hash] = isAnchorActive(i, anchor, nextAnchor)

if (isActive) {
clickedHash.value = null
activateLink(hash)
return
}
Expand Down Expand Up @@ -105,8 +138,10 @@ export function useActiveAnchor(
}

onMounted(() => {
requestAnimationFrame(setActiveLink)
requestAnimationFrame(() => setActiveLink('scroll'))

window.addEventListener('scroll', onScroll)
container.value.addEventListener('click', handleAnchorLinkClick)
})

onUpdated(() => {
Expand All @@ -116,6 +151,9 @@ export function useActiveAnchor(

onUnmounted(() => {
window.removeEventListener('scroll', onScroll)
container.value.removeEventListener('click', handleAnchorLinkClick)

scrollTimer.value && clearTimeout(scrollTimer.value)
})
}

Expand Down