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

Highlighted active heading erratic when near document end #2

Closed
janosh opened this issue Oct 7, 2021 · 3 comments
Closed

Highlighted active heading erratic when near document end #2

janosh opened this issue Oct 7, 2021 · 3 comments
Labels
bug Something isn't working help wanted Extra attention is needed

Comments

@janosh
Copy link
Owner

janosh commented Oct 7, 2021

Find out why IntersectionObserver appears to have trouble identifying the active heading when scrolling to near the page bottom. Highlighted heading seems to jump erratically between last two items in ToC.

@janosh janosh added bug Something isn't working help wanted Extra attention is needed labels Oct 7, 2021
@thedivtagguy
Copy link

Trying to figure out why this is happening too. The demo site does not have this issue though, any ideas why?

@janosh
Copy link
Owner Author

janosh commented Dec 30, 2021

The problem is svelte-toc currently only looks at the first intersection entry returned by the IntersectionObserver and always makes that the active heading:

([entry]) => {
activeHeading = entry.target as HTMLHeadingElement // assign intersecting node to activeHeading
},

When two headings change visibility simultaneously (or at least close in time to be within one update cycle of the observer), both are passed into the callback function. Maybe the order of entries depends on scroll direction or something like that. In any case which of the two becomes the active heading appears to be up to chance at that point. I think that's why the active heading jumps around erratically. So a solution might be to compare the intersectionRatio of all returned entries and pick the one with the highest value as active. Or pick the one closest to the center of the screen.

@janosh janosh closed this as completed in 559b99d Dec 31, 2021
@janosh
Copy link
Owner Author

janosh commented Dec 31, 2021

This should be fixed now in v0.2.0 which is a rather big rewrite. It also adds the feature to auto-scroll within the ToC to always keep the currently active heading in view.

@thedivtagguy Would be great if you could give it a try and report back issues if any. There might be some due to the amount of changes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

2 participants