From 416dda000d07702773328508dceb4553e884d7fa Mon Sep 17 00:00:00 2001 From: anishshobithps Date: Wed, 17 Apr 2024 13:53:48 +0530 Subject: [PATCH] feat(navbar): hide while scrolling down --- src/components/widgets/Header.tsx | 30 ++++++++++++++++++++++++------ 1 file changed, 24 insertions(+), 6 deletions(-) diff --git a/src/components/widgets/Header.tsx b/src/components/widgets/Header.tsx index 15895d0..97f361d 100644 --- a/src/components/widgets/Header.tsx +++ b/src/components/widgets/Header.tsx @@ -1,19 +1,19 @@ "use client"; +import React, { useState, useEffect } from "react"; import SJECLogo from "@/assets/sjeclogo.png"; -import { Button, buttonVariants } from "@/components/ui/button"; +import { Button } from "@/components/ui/button"; import Image from "next/image"; import Link from "next/link"; import { signIn, signOut, useSession } from "next-auth/react"; import { HamburgerMenuIcon, Cross1Icon } from "@radix-ui/react-icons"; import { EncryptButton } from "@/components/ui/hover/button"; -import { Item } from "@radix-ui/react-navigation-menu"; interface NavItem { label: string; href: string; } -const NavItems: NavItem[] = [ +const navItems: NavItem[] = [ { label: "home", href: "/", @@ -37,10 +37,29 @@ const NavItems: NavItem[] = [ ]; export function Header() { + const [prevScrollPos, setPrevScrollPos] = useState(0); + const [visible, setVisible] = useState(true); const { data: session } = useSession(); + + useEffect(() => { + const handleScroll = () => { + const currentScrollPos = window.pageYOffset; + setVisible(prevScrollPos > currentScrollPos || currentScrollPos < 10); + setPrevScrollPos(currentScrollPos); + }; + + window.addEventListener("scroll", handleScroll); + + return () => window.removeEventListener("scroll", handleScroll); + }, [prevScrollPos, visible]); + return ( <> -
+