Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Fixed Navbar on the top for Better User Experience #71

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@
},
"dependencies": {
"axios": "^1.7.7",
"clsx": "^2.1.1",
"framer-motion": "^7.3.5",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-icons": "^4.4.0",
"react-lottie-player": "1.5.4",
"smoothscroll-polyfill": "^0.4.4"
"smoothscroll-polyfill": "^0.4.4",
"tailwind-merge": "^2.5.4"
Comment on lines +13 to +20
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would avoid adding another two libraries for something that is just being used at one place and can be achieved otherwise as well.

So let's remove these and use the basics itself :)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah i add 2 library clsx and tailwind-merge because i think we need that, for smoothscroll-polyfill when i run the npm install after i clone this repo, its update but the version same idk why.

The package i install it for handle the className style tailwind, there new folder named lib and there utils.js file
image
i used that in the Navbar

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understood why you used them

But the same can be achieved without these libraries also. So lets avoid using that.

},
"devDependencies": {
"@types/react": "^18.0.17",
Expand Down
8 changes: 3 additions & 5 deletions src/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,9 @@ const App = () => {
animate={{ opacity: 1 }}
transition={{ duration: 0.75, delay: 0.5 }}
>
<div className={`${styles.paddingX} ${styles.flexCenter}`}>
<div className={`${styles.boxWidth}`}>
<Navbar />
</div>
</div>
{/* <div className={`${styles.paddingX} ${styles.flexCenter}`}> */}
<Navbar />
{/* </div> */}

<div className={`bg-primary ${styles.flexStart}`}>
<div className={`${styles.boxWidth}`}>
Expand Down
49 changes: 46 additions & 3 deletions src/components/Navbar.jsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,56 @@
import { useState } from "react";
import { useCallback, useEffect, useState } from "react";
import { motion } from "framer-motion";
import { close, parthmittal, menu } from "../assets";
import { navLinks } from "../constants";
import { scrollToSection } from "../lib/helperFunctions";
import { cn } from "../lib/utils";

const Navbar = () => {
const [toggle, setToggle] = useState(false);
// state for navbar
const [lastScrollY, setLastScrollY] = useState(0);
const [isNavbarVisible, setIsNavbarVisible] = useState(true);

// control navbar on scroll
const controlNavbar = useCallback(() => {
if (typeof window === "undefined") return;
const currentScrollY = window.scrollY;
const isScrollingUp = currentScrollY < lastScrollY;

setIsNavbarVisible(isScrollingUp);
setLastScrollY(currentScrollY);
}, [lastScrollY]);

// Add/remove scroll event listener
useEffect(() => {
// add scroll event listener
window.addEventListener("scroll", controlNavbar);
// cleanup function
return () => {
window.removeEventListener("scroll", controlNavbar);
};
}, [lastScrollY, controlNavbar]);
return (
<nav className="w-full flex justify-between items-center navbar">
<motion.nav
className={cn(
`w-full xl:max-w-[1280px] xl:mx-auto sm:px-16 px-6 flex justify-between items-center navbar ${
isNavbarVisible && lastScrollY === 0
? `translate-y-0`
: isNavbarVisible &&
"-translate-y-full bg-primary/50 backdrop-blur shadow-lg"
}`,
{
"fixed z-10": lastScrollY > 0,
}
)}
initial={{ y: 0, opacity: 0 }}
animate={{
y:
isNavbarVisible && lastScrollY === 0 ? 0 : isNavbarVisible ? 0 : -100,
opacity: isNavbarVisible ? 1 : 0,
}}
transition={{ duration: 0.3 }}
>
{/* Logo */}
<a href="#home">
<img
Expand Down Expand Up @@ -69,7 +112,7 @@ const Navbar = () => {
</ul>
</div>
</div>
</nav>
</motion.nav>
);
};

Expand Down
6 changes: 6 additions & 0 deletions src/lib/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { clsx } from "clsx";
import { twMerge } from "tailwind-merge";

export function cn(...inputs) {
return twMerge(clsx(inputs));
}