"use client"; import { useState, useEffect } from "react"; import { usePathname, useRouter } from "next/navigation"; import Link from "next/link"; import styles from "./header.module.scss"; import { Logo, LogoIcon } from "@/components/general/Logo"; import { Icon } from "../general/Icon"; import { useInView } from "react-intersection-observer"; import { getSearchPath } from "@/lib/common"; export const Header = () => { const { ref: observer, inView: isInView } = useInView({ triggerOnce: false, initialInView: true, }); const { replace } = useRouter(); const [showMenu, setShowMenu] = useState(false); function toggleMenu() { setShowMenu(!showMenu); } // remove ability to scroll when opening menu + keep previous scroll position when closing menu useEffect(() => { if (showMenu) { const scrollY = window.scrollY; document.body.style.position = "fixed"; document.body.style.top = `-${scrollY}px`; } else { const scrollY = parseInt(document.body.style.top || "0") * -1; document.body.style.position = ""; document.body.style.top = ""; window.scrollTo(0, scrollY); } }, [showMenu]); // reset scroll position on path change useEffect(() => { if (!showMenu) { window.scrollTo(0, 0); } }, [location.pathname, showMenu]); // hide menu and reset active menu item on path change const pathname = usePathname(); useEffect(() => { setShowMenu(false); setActiveMenuItem(undefined); }, [pathname]); const [activeMenuItem, setActiveMenuItem] = useState( undefined ); const handleSearch = (e: React.KeyboardEvent) => { if (e.key != "Enter") { return; } const query = e.currentTarget.value; if (query) { setShowMenu(false); replace(getSearchPath(query)); } }; return ( <>
{!isInView ? : }
); }; export const MenuIcon = ({ showMenu }: { showMenu: boolean }) => { return (
{showMenu ? ( ) : ( )}
); };