Skip to content

Commit

Permalink
fix(theme): ensure outline marker follows click
Browse files Browse the repository at this point in the history
closes #3878
  • Loading branch information
zqianem committed May 11, 2024
1 parent 99c0cec commit 9d9558a
Showing 1 changed file with 17 additions and 0 deletions.
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

0 comments on commit 9d9558a

Please sign in to comment.