diff --git a/src/components/Menu/Menu.tsx b/src/components/Menu/Menu.tsx index fa19dbf8..056520b0 100644 --- a/src/components/Menu/Menu.tsx +++ b/src/components/Menu/Menu.tsx @@ -1,13 +1,50 @@ +import { useEffect, useState } from "react" import Link from "next/link" -import colors from "../../styles/colors" import styles from "./SideMenu.module.css" import typographyStyles from "../../styles/typography.module.css" -import { useRouter } from "next/router" +import colors from "../../styles/colors" import { Pages } from "../../types/types" function Menu({ pages = [] }: { pages: Pages }) { - const router = useRouter() - const { asPath: pathname } = router + const [activeSection, setActiveSection] = useState("") + + useEffect(() => { + const handleScroll = () => { + const sections = pages.flatMap((page) => + [page, ...(page.pages || [])].map((p) => ({ + id: p.pathname.replace("#", ""), + top: + document.getElementById(p.pathname.replace("#", ""))?.offsetTop || + 0, + })) + ) + + const currentSection = sections.reduce((acc, section) => { + const { id, top } = section + if (window.scrollY >= top - 100) { + console.log(id) + return id + } + return acc + }, "") + + setActiveSection(currentSection) + } + + window.addEventListener("scroll", handleScroll) + + handleScroll() + + return () => window.removeEventListener("scroll", handleScroll) + }, [pages]) + + const scrollToSection = (sectionId: string, event: React.MouseEvent) => { + event.preventDefault() + const element = document.getElementById(sectionId.replace("#", "")) + if (element) { + element.scrollIntoView({ behavior: "smooth" }) + } + } return (