Skip to content
Merged
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
11 changes: 6 additions & 5 deletions src/components/sidebarTableOfContents/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -157,16 +157,17 @@ export function SidebarTableOfContents() {

// Mark the active item based on the scroll position
useEffect(() => {
if (!tocItems.length) {
const innerHeight = window.innerHeight;
if (!tocItems.length || !innerHeight) {
return () => {};
}
// account for the header height
const rootMarginTop = 100;
// element is consiered in view if it is in the top 1/3 of the screen
const rootMarginBottomRaw = (2 / 3) * window.innerHeight - rootMarginTop;
const rootMarginBottom = Math.floor(rootMarginBottomRaw);
const rootMarginBottomRaw = (2 / 3) * innerHeight - rootMarginTop;
const rootMarginBottom = Math.floor(rootMarginBottomRaw) * -1;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Strange that we still run into this, I thought the floor call here fixed this 🤔

const observerOptions = {
rootMargin: `${rootMarginTop}px 0px -${rootMarginBottom}px 0px`,
rootMargin: `${rootMarginTop}px 0px ${rootMarginBottom}px 0px`,
threshold: 1,
};
const observer = new IntersectionObserver(entries => {
Expand All @@ -183,7 +184,7 @@ export function SidebarTableOfContents() {
}, observerOptions);
const headings = tocItems.map(item => item.element);
headings.forEach(heading => observer.observe(heading));
return () => headings.forEach(heading => observer.unobserve(heading));
return () => observer.disconnect();
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is better, we can tear down the whole observer like this I believe? I can't even find unobserve here: https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver/disconnect 🤔

}, [tocItems]);

return (
Expand Down
Loading