Skip to content

Commit

Permalink
feat(layout): add dynamic navbar highlight on scrolling
Browse files Browse the repository at this point in the history
  • Loading branch information
ladunjexa committed May 16, 2024
1 parent 4481281 commit ab7cfc0
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 8 deletions.
29 changes: 23 additions & 6 deletions components/layout/navbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,39 @@ import React, { useEffect, useState } from "react";
import { brainwave } from "@/public/assets/index";
import { cn } from "@/lib/utils";
import { navigation } from "@/constants";
import { useParams } from "next/navigation";
import Button from "../atoms/button";
import MenuSvg from "../svg/menu-svg";
import { HamburgerMenu } from "../design/navbar";

type Props = {};
type TSection = "hero" | "features" | "collaboration" | "how-to-use" | "pricing";

const Navbar = (props: Props) => {
const params = useParams();
const [hash, setHash] = useState<TSection>("hero");
const [hash, setHash] = useState<string>("hero");
const [openNavigation, setOpenNavigation] = useState<boolean>(false);

useEffect(() => {
setHash(window.location.hash as TSection);
}, [params]);
const dynamicNavbarHighlight = () => {
const sections = document.querySelectorAll("section[id]");

sections.forEach((current) => {
if (current === null) return;

const sectionId = current.getAttribute("id");
// @ts-ignore
const sectionHeight = current.offsetHeight;
const sectionTop = current.getBoundingClientRect().top - sectionHeight * 0.2;

if (sectionTop < 0 && sectionTop + sectionHeight > 0 && hash !== sectionId) {
setHash(`#${sectionId as string}`);
}
});
};

window.addEventListener("scroll", dynamicNavbarHighlight);

return () => window.removeEventListener("scroll", dynamicNavbarHighlight);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);

const toggleNavigation = () => setOpenNavigation(!openNavigation);
const handleClick = () => {
Expand Down
4 changes: 2 additions & 2 deletions components/layout/section.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ type Props = {

const Section = ({ className, id, crosses, crossesOffset, customPaddings, children }: Props) => {
return (
<div
<section
{...(id && { id })}
className={cn(
"relative",
Expand Down Expand Up @@ -46,7 +46,7 @@ const Section = ({ className, id, crosses, crossesOffset, customPaddings, childr
<SectionSvg crossesOffset={crossesOffset} />
</>
)}
</div>
</section>
);
};

Expand Down

0 comments on commit ab7cfc0

Please sign in to comment.