diff --git a/src/components/FooterNav.tsx b/src/components/FooterNav.tsx new file mode 100644 index 0000000..12dc7cd --- /dev/null +++ b/src/components/FooterNav.tsx @@ -0,0 +1,144 @@ +"use client"; + +import { + HeartIcon, + MagnifyingGlassIcon, + UserCircleIcon, +} from "@heroicons/react/24/outline"; +import React, { useEffect, useRef } from "react"; +import { PathName } from "@/routers/types"; +import MenuBar from "@/shared/MenuBar"; +import isInViewport from "@/utils/isInViewport"; +import Link from "next/link"; +import { usePathname } from "next/navigation"; + +let WIN_PREV_POSITION = 0; +if (typeof window !== "undefined") { + WIN_PREV_POSITION = window.pageYOffset; +} + +interface NavItem { + name: string; + link?: PathName; + icon: any; +} + +const NAV: NavItem[] = [ + { + name: "Explore", + link: "/", + icon: MagnifyingGlassIcon, + }, + { + name: "Wishlists", + link: "/account-savelists", + icon: HeartIcon, + }, + { + name: "Log in", + link: "/account", + icon: UserCircleIcon, + }, + { + name: "Menu", + icon: MenuBar, + }, +]; + +const FooterNav = () => { + const containerRef = useRef(null); + + const pathname = usePathname(); + + useEffect(() => { + if (typeof window !== "undefined") { + window.addEventListener("scroll", handleEvent); + } + // eslint-disable-next-line react-hooks/exhaustive-deps + }, []); + + const handleEvent = () => { + if (typeof window !== "undefined") { + window.requestAnimationFrame(showHideHeaderMenu); + } + }; + + const showHideHeaderMenu = () => { + // if (typeof window === "undefined" || window?.innerWidth >= 768) { + // return null; + // } + + let currentScrollPos = window.pageYOffset; + if (!containerRef.current) return; + + // SHOW _ HIDE MAIN MENU + if (currentScrollPos > WIN_PREV_POSITION) { + if ( + isInViewport(containerRef.current) && + currentScrollPos - WIN_PREV_POSITION < 80 + ) { + return; + } + + containerRef.current.classList.add("FooterNav--hide"); + } else { + if ( + !isInViewport(containerRef.current) && + WIN_PREV_POSITION - currentScrollPos < 80 + ) { + return; + } + containerRef.current.classList.remove("FooterNav--hide"); + } + + WIN_PREV_POSITION = currentScrollPos; + }; + + const renderItem = (item: NavItem, index: number) => { + const isActive = pathname === item.link; + + return item.link ? ( + + + + {item.name} + + + ) : ( +
+ + {item.name} +
+ ); + }; + + return ( +
+
+ {/* MENU */} + {NAV.map(renderItem)} +
+
+ ); +}; + +export default FooterNav;