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: toc correctly reflects headings in a viewport #701

Merged
merged 1 commit into from
Dec 1, 2022

Conversation

Zerro97
Copy link
Contributor

@Zerro97 Zerro97 commented Dec 1, 2022

Fix

This fixes #700 by adding deep watch option in useScrollspy.ts:

from

watch(visibleHeadings, (val, oldVal) => {
    if (val.length === 0) { activeHeadings.value = oldVal } else { activeHeadings.value = val }
})

to

watch(visibleHeadings, (val, oldVal) => {
    if (val.length === 0) { activeHeadings.value = oldVal } else { activeHeadings.value = val }
}, { deep: true })

Problem Previously

useScrollspy.ts

While visibleHeadings were correctly updated when it came into a viewport (by chekcing if entry.isIntersecting using IntersectionObserver API), the watch function below did not updated activeHeadings in some circumstances (like when there were no activeHeadings in a viewport)

I tried adding deep option to the watch function and it fixed the problem (might be bad for performance?)

Note

It's my first time contributing to open source. Hope I'm not making any mistakes haha

…iveHeadings

Even though visibleHeadings are correctly updated using Intersection Observer API, watching visibleHeadings does not get executed when visibleHeadings change. Changing to deep watcher fixes this
@vercel
Copy link

vercel bot commented Dec 1, 2022

@Zerro97 is attempting to deploy a commit to the Nuxt Labs Team on Vercel.

A member of the Team first needs to authorize it.

@vercel
Copy link

vercel bot commented Dec 1, 2022

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Updated
docus ✅ Ready (Inspect) Visit Preview Dec 1, 2022 at 1:12PM (UTC)

@atinux atinux merged commit ab5e8d4 into nuxt-themes:dev Dec 1, 2022
@Zerro97 Zerro97 deleted the fix/toc-scroll-spy-bug branch December 1, 2022 23:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Toc] headings are not detected correctly when there are no headings in a viewport
2 participants