Skip to content
Draft
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
19 changes: 16 additions & 3 deletions apps/client/src/components/scroll-spy/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ const ScrollSpy = ({ children, offset: globalOffset }: PropsWithChildren<ScrollS
const [activeElemId, setActiveElemId] = useState('');
const sectionRefs = useRef<SectionRef>({});
const lastScrollTopRef = useRef(0);
const ticking = useRef(false);

const isInView = ({
element,
Expand Down Expand Up @@ -105,13 +106,25 @@ const ScrollSpy = ({ children, offset: globalOffset }: PropsWithChildren<ScrollS
}
}, [globalOffset]);

// Throttle scroll event using requestAnimationFrame to reduce reflows
const onScroll = useCallback(() => {
if (!ticking.current) {
window.requestAnimationFrame(() => {
spy();
ticking.current = false;
});

ticking.current = true;
}
}, [spy]);

useEffect(() => {
spy();
window.addEventListener('scroll', spy);
window.addEventListener('scroll', onScroll);
return () => {
window.removeEventListener('scroll', spy);
window.removeEventListener('scroll', onScroll);
};
}, [spy]);
}, [onScroll, spy]);

return (
<ScrollSpyContext
Expand Down