diff --git a/apps/client/src/components/scroll-spy/index.tsx b/apps/client/src/components/scroll-spy/index.tsx index 5863763..a9e33b7 100644 --- a/apps/client/src/components/scroll-spy/index.tsx +++ b/apps/client/src/components/scroll-spy/index.tsx @@ -40,6 +40,7 @@ const ScrollSpy = ({ children, offset: globalOffset }: PropsWithChildren({}); const lastScrollTopRef = useRef(0); + const ticking = useRef(false); const isInView = ({ element, @@ -105,13 +106,25 @@ const ScrollSpy = ({ children, offset: globalOffset }: PropsWithChildren { + 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 (