Skip to content
This repository has been archived by the owner on Jan 20, 2024. It is now read-only.

feat: Navbar fixed when user scrolls #322

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
33 changes: 31 additions & 2 deletions src/components/Header/Header.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ReactNode, Fragment } from 'react';
import { ReactNode, Fragment, useState, useCallback, useEffect } from 'react';
import Link from 'next/link';
import { Popover, Transition } from '@headlessui/react';
import clsx from 'clsx';
Expand Down Expand Up @@ -77,8 +77,37 @@ function MobileNavigation() {
}

export function Header({ session }: { session: Session | null | undefined }) {
const [y, setY] = useState(0);
const [headerClass, setHeaderClass] = useState('py-10');

const handleNavigation = useCallback(
(e: any) => {
const window = e.currentTarget;

if (y < 50) {
setHeaderClass('py-10');
}
if (y > 50) {
setHeaderClass('py-5 border-b border-slate-200 bg-slate-50');
}
setY(window.scrollY);
},
[y],
);

useEffect(() => {
setY(window.scrollY);
window.addEventListener('scroll', handleNavigation);

return () => {
window.removeEventListener('scroll', handleNavigation);
};
}, [handleNavigation]);

return (
<header className='py-10'>
<header
className={`sticky top-0 z-10 bg-opacity-60 backdrop-blur-xl backdrop-filter transition-all duration-300 ease-in-out ${headerClass}`}
>
<Container>
<nav className='relative z-50 flex justify-between'>
<div className='flex items-center md:gap-x-12'>
Expand Down