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(theme): ensure outline marker follows click #3879

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
17 changes: 17 additions & 0 deletions src/client/theme-default/composables/outline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,12 @@ export function useActiveAnchor(
const onScroll = throttleAndDebounce(setActiveLink, 100)

let prevActiveLink: HTMLAnchorElement | null = null
let ignoreScrollOnce: boolean = false

onMounted(() => {
requestAnimationFrame(setActiveLink)
window.addEventListener('scroll', onScroll)
container.value.addEventListener('click', onClick)
})

onUpdated(() => {
Expand All @@ -130,13 +132,28 @@ export function useActiveAnchor(

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

function onClick(e: MouseEvent) {
if (!isAsideEnabled.value || !(e.target instanceof HTMLAnchorElement)) {
return
}

activateLink(location.hash)
ignoreScrollOnce = true
}

function setActiveLink() {
if (!isAsideEnabled.value) {
return
}

if (ignoreScrollOnce) {
ignoreScrollOnce = false
return
}

const scrollY = window.scrollY
const innerHeight = window.innerHeight
const offsetHeight = document.body.offsetHeight
Expand Down