diff --git a/src/Components/Navbar/Navbar.tsx b/src/Components/Navbar/Navbar.tsx index c77d6a8..60d1506 100644 --- a/src/Components/Navbar/Navbar.tsx +++ b/src/Components/Navbar/Navbar.tsx @@ -1,4 +1,4 @@ -import { useCallback, useEffect, } from "react"; +import { useCallback, useEffect, useRef, useState, } from "react"; import { useAppDispatch, useAppSelector, useCurrentSection, useMediaQuery, useResizeListener } from "src/utils/hooks"; import { setNavHeight } from "src/redux/features/ui.slice"; import NavDesktop from "./NavDesktop"; @@ -7,6 +7,8 @@ import { FiArrowRight } from "react-icons/fi"; import { BiRocket } from "react-icons/bi"; import { Link } from "react-router-dom"; import { PAGES_ROUTES } from "src/utils/routing"; +import { motion } from "framer-motion"; +import throttle from "lodash.throttle"; export const navLinks = []; @@ -16,9 +18,13 @@ export default function Navbar() { const dispatch = useAppDispatch(); const theme = useAppSelector(state => state.ui.theme) - const isLargeScreen = useMediaQuery(MEDIA_QUERIES.isLarge) + const [navTransparent, setNavTransparent] = useState(true); + const isNavTransparentRef = useRef(true); + + isNavTransparentRef.current = navTransparent; + const updateNavHeight = useCallback(() => { const nav = document.querySelector("nav"); if (nav) { @@ -28,11 +34,26 @@ export default function Navbar() { document.documentElement.style.setProperty('--navHeight', nav.clientHeight + 'px') } } - }, [dispatch]) + }, [dispatch]); + + const handleScroll = useCallback(() => { + const inTop = window.scrollY < 10; + if (inTop && !isNavTransparentRef.current) setNavTransparent(true); + if (!inTop && isNavTransparentRef.current) setNavTransparent(false); + }, []) useEffect(() => { updateNavHeight(); - }, [updateNavHeight]); + handleScroll(); + + const throttledScrollHandler = throttle(handleScroll, 500) + + document.addEventListener('scroll', throttledScrollHandler) + + return () => { + document.removeEventListener('scroll', throttledScrollHandler); + } + }, [handleScroll, updateNavHeight]); useResizeListener(updateNavHeight) @@ -52,13 +73,19 @@ export default function Navbar() { + const bgColor = theme === 'dark' ? "rgba(16 24 40,1)" : "rgba(255 255 255,1)"; + const bgColorTrans = theme === 'dark' ? "rgba(16 24 40,0)" : "rgba(255 255 255,0)"; + return ( -
{subtitle}
+{subtitle}
} {!filtersEmpty &&filtered by