diff --git a/components/navigation/NavBar.tsx b/components/navigation/NavBar.tsx index c007d653e8c3..2cd7ee8b46c1 100644 --- a/components/navigation/NavBar.tsx +++ b/components/navigation/NavBar.tsx @@ -1,3 +1,4 @@ +import debounce from 'lodash/debounce'; import Link from 'next/link'; import type { NextRouter } from 'next/router'; import { useRouter } from 'next/router'; @@ -44,15 +45,18 @@ export default function NavBar({ className = '', hideLogo = false }: NavBarProps // Adding Scroll listener useEffect(() => { - const handleScroll = () => { + const handleScroll = debounce(() => { const scrollTop = window.scrollY; setIsScrolled(scrollTop > 50); - }; + }, 10); window.addEventListener('scroll', handleScroll); - return () => window.removeEventListener('scroll', handleScroll); + return () => { + window.removeEventListener('scroll', handleScroll); + handleScroll.cancel(); + }; }, []); /** @@ -162,8 +166,8 @@ export default function NavBar({ className = '', hideLogo = false }: NavBarProps <>
{/*
- +
diff --git a/components/navigation/StickyNavbar.tsx b/components/navigation/StickyNavbar.tsx index 26510fdaaf0e..88e34db6a273 100644 --- a/components/navigation/StickyNavbar.tsx +++ b/components/navigation/StickyNavbar.tsx @@ -1,3 +1,4 @@ +import debounce from 'lodash/debounce'; import type { ReactNode } from 'react'; import React, { useEffect, useState } from 'react'; @@ -16,20 +17,24 @@ export default function StickyNavbar({ children, className = '' }: StickyNavbarP const [isScrolled, setIsScrolled] = useState(false); // Adding Scroll listener useEffect(() => { - const handleScroll = () => { + const handleScroll = debounce(() => { const scrollTop = window.scrollY; setIsScrolled(scrollTop > 50); // Trigger after scrolling 50px - }; + }, 10); window.addEventListener('scroll', handleScroll); - return () => window.removeEventListener('scroll', handleScroll); // Clean up + return () => { + window.removeEventListener('scroll', handleScroll); // Clean up + handleScroll.cancel(); + }; }, []); return (
{children}