Skip to content

Commit

Permalink
set up navbar to hide when scrolling
Browse files Browse the repository at this point in the history
  • Loading branch information
alyssaongyx committed Oct 10, 2024
1 parent b32c3a2 commit fe611da
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 56 deletions.
30 changes: 22 additions & 8 deletions frontend/src/components/main-header/main-header.module.css
Original file line number Diff line number Diff line change
@@ -1,17 +1,31 @@
.navbarItem {
color: #A1A1AA;
font-weight: 500;
color: #a1a1aa;
font-weight: 500;

&:hover {
font-weight: 600;
}
&:hover {
font-weight: 600;
}
}

.logo {
display: flex
display: flex;
}

.logo img {
width: 8em;
height: auto;
width: 8em;
height: auto;
}

.navbar {
transition: top 0.3s;
position: fixed;
top: 0;
width: 100%;
z-index: 1000;
background-color: white;
box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
}

.hidden {
top: -60px;
}
134 changes: 86 additions & 48 deletions frontend/src/components/main-header/main-header.tsx
Original file line number Diff line number Diff line change
@@ -1,55 +1,93 @@
'use client';
"use client";
import { useEffect, useState } from "react"; // Import useEffect and useState
import Link from "next/link";
import Image from "next/image";
import logoImg from "@/assets/logo.jpg"
import classes from './main-header.module.css';
import { Navbar, NavbarBrand, NavbarContent, NavbarItem } from "@nextui-org/navbar";
import { usePathname } from "next/navigation";
import logoImg from "@/assets/logo.jpg";
import classes from "./main-header.module.css";
import {
Navbar,
NavbarBrand,
NavbarContent,
NavbarItem,
} from "@nextui-org/navbar";
import { usePathname, useRouter } from "next/navigation";

type NavbarItem = {
label: string,
href: string
}
label: string;
href: string;
};

export default function MainHeader() {
const pathname = usePathname();

const navbarItems: NavbarItem[] = [
{ label: "Home", href: "/" },
{ label: "About", href: "/about" },
{ label: "Feedback", href: "/feedback" }
]

return (
<>
<Navbar
classNames={{
item: [
"flex",
"relative",
"h-full",
"items-center",
"data-[active=true]:after:bg-primary",
"data-[active=true]:after:text-blue-600",
"data-[active=true]:after:font-extrabold",
],
}}
>
<NavbarBrand>
<Link className={classes.logo} href="/">
<Image src={logoImg} alt="Schemes SG logo" width={120} height={30} priority/>
</Link>
</NavbarBrand>
<NavbarContent className="hidden sm:flex gap-4" justify="end">
{navbarItems.map((item, idx) => {
return (
<NavbarItem className={classes.navbarItem} key={idx} isActive={pathname === item.href ? true : undefined}>
<Link href={item.href}>{item.label}</Link>
</NavbarItem>
)
})}
</NavbarContent>
</Navbar>
</>
)
const pathname = usePathname();
const router = useRouter();
const [hidden, setHidden] = useState(false);
let lastScrollTop = 0;

const navbarItems: NavbarItem[] = [
{ label: "Home", href: "/" },
{ label: "About", href: "/about" },
{ label: "Feedback", href: "/feedback" },
];

useEffect(() => {
const handleScroll = () => {
const currentScroll =
window.pageYOffset || document.documentElement.scrollTop;

if (currentScroll > lastScrollTop) {
// Scrolling down
setHidden(true);
} else {
// Scrolling up
setHidden(false);
}
lastScrollTop = currentScroll <= 0 ? 0 : currentScroll;
};

window.addEventListener("scroll", handleScroll);
return () => {
window.removeEventListener("scroll", handleScroll);
};
}, []);

const handleLinkClick = (href: string) => {
router.push(href);
window.scrollTo(0, 0);
};

return (
<>
<Navbar className={`${classes.navbar} ${hidden ? "hidden" : ""}`}>
<NavbarBrand>
<Link className={classes.logo} href="/">
<Image
src={logoImg}
alt="Schemes SG logo"
width={120}
height={30}
priority
/>
</Link>
</NavbarBrand>
<NavbarContent className="hidden sm:flex gap-4" justify="end">
{navbarItems.map((item, idx) => {
return (
<NavbarItem
className={classes.navbarItem}
key={idx}
isActive={pathname === item.href ? true : undefined}
>
<Link
href={item.href}
onClick={() => handleLinkClick(item.href)}
>
{item.label}
</Link>
</NavbarItem>
);
})}
</NavbarContent>
</Navbar>
</>
);
}

0 comments on commit fe611da

Please sign in to comment.