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

Change URL hash when user scrolls the page #903

Closed
brc-dd opened this issue Jul 3, 2022 · 1 comment · Fixed by #991
Closed

Change URL hash when user scrolls the page #903

brc-dd opened this issue Jul 3, 2022 · 1 comment · Fixed by #991
Labels
enhancement New feature or request stale theme Related to the theme

Comments

@brc-dd
Copy link
Member

brc-dd commented Jul 3, 2022

Currently this code changes the URL hash when users click some link to a fragment identifier causing unwanted issues like #890 and #788.

export function useActiveAnchor(
container: Ref<HTMLElement>,
marker: Ref<HTMLElement>
) {
const { isAsideEnabled } = useAside()
const onScroll = throttleAndDebounce(setActiveLink, 100)
let prevActiveLink: HTMLAnchorElement | null = null
onMounted(() => {
requestAnimationFrame(setActiveLink)
window.addEventListener('scroll', onScroll)
})
onUpdated(() => {
// sidebar update means a route change
activateLink(location.hash)
})
onUnmounted(() => {
window.removeEventListener('scroll', onScroll)
})
function setActiveLink() {
if (!isAsideEnabled.value) {
return
}
const links = [].slice.call(
container.value.querySelectorAll('.outline-link')
) as HTMLAnchorElement[]
const anchors = [].slice
.call(document.querySelectorAll('.content .header-anchor'))
.filter((anchor: HTMLAnchorElement) => {
return links.some((link) => {
return link.hash === anchor.hash && anchor.offsetParent !== null
})
}) as HTMLAnchorElement[]
const scrollY = window.scrollY
const innerHeight = window.innerHeight
const offsetHeight = document.body.offsetHeight
const isBottom = scrollY + innerHeight === offsetHeight
// page bottom - highlight last one
if (anchors.length && isBottom) {
activateLink(anchors[anchors.length - 1].hash)
return
}
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) {
history.replaceState(null, document.title, hash ? hash : ' ')
activateLink(hash)
return
}
}
}
function activateLink(hash: string | null) {
if (prevActiveLink) {
prevActiveLink.classList.remove('active')
}
if (hash !== null) {
prevActiveLink = container.value.querySelector(
`a[href="${decodeURIComponent(hash)}"]`
) as HTMLAnchorElement
}
const activeLink = prevActiveLink
if (activeLink) {
activeLink.classList.add('active')
marker.value.style.top = activeLink.offsetTop + 33 + 'px'
marker.value.style.opacity = '1'
} else {
marker.value.style.top = '33px'
marker.value.style.opacity = '0'
}
}
}

Related SO thread: https://stackoverflow.com/q/7035896/11613622

I am thinking lets remove below line. The outline will still highlight current header. Just the hash won't change on URL.

history.replaceState(null, document.title, hash ? hash : ' ')

Side note, we also need to support outline: false.

@brc-dd brc-dd added bug Something isn't working theme Related to the theme labels Jul 3, 2022
@brc-dd brc-dd changed the title Don't hijack scroll unless actually scrolled by user Don't change URL hash unless actually scrolled by user Jul 3, 2022
@brc-dd brc-dd added this to the v1.0.0 milestone Jul 14, 2022
@brc-dd brc-dd reopened this Jul 14, 2022
@brc-dd brc-dd removed this from the v1.0.0 milestone Jul 14, 2022
@brc-dd
Copy link
Member Author

brc-dd commented Jul 14, 2022

Removed that line for now, let's see if we can reintroduce that functionality properly.

@brc-dd brc-dd changed the title Don't change URL hash unless actually scrolled by user Change URL hash when user scrolls the page Jul 14, 2022
@brc-dd brc-dd added enhancement New feature or request and removed bug Something isn't working labels Jul 14, 2022
@github-actions github-actions bot added the stale label Aug 3, 2023
@brc-dd brc-dd closed this as not planned Won't fix, can't repro, duplicate, stale Aug 3, 2023
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 11, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement New feature or request stale theme Related to the theme
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant