From 51cfd7da1606064578a6fba1adef76680f67c399 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dorien=20Gr=C3=B6nwald?= Date: Mon, 16 Sep 2024 08:35:06 +0200 Subject: [PATCH] feat: add background to header while scrolling down --- src/tsx/components/navigation/NavItem.tsx | 2 +- src/tsx/layout/Header.tsx | 27 ++++++++++++++++++----- 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/src/tsx/components/navigation/NavItem.tsx b/src/tsx/components/navigation/NavItem.tsx index 095586e..73edc4a 100644 --- a/src/tsx/components/navigation/NavItem.tsx +++ b/src/tsx/components/navigation/NavItem.tsx @@ -12,7 +12,7 @@ const NavItem: React.FC = ({ label, url, isExternalLink = false })

{label} diff --git a/src/tsx/layout/Header.tsx b/src/tsx/layout/Header.tsx index 19375f8..a42c595 100644 --- a/src/tsx/layout/Header.tsx +++ b/src/tsx/layout/Header.tsx @@ -1,16 +1,17 @@ -import * as React from 'react'; +import { useState, useEffect } from 'react'; import Lottie from "lottie-react"; import MainNavigation from '../components/navigation/MainNavigation'; import logoAnimation from '../../json/logoAnimation.json' function Header() { - const [open, setOpen] = React.useState(false); + const [open, setOpen] = useState(false); + const [isScrolled, setIsScrolled] = useState(false); function toggleNavigation(state: boolean) { setOpen(state); } - React.useEffect(() => { + useEffect(() => { function handleResize() { if (window.matchMedia('(min-width: 1024px)').matches) { setOpen(false); @@ -22,7 +23,7 @@ function Header() { return () => { window.removeEventListener('resize', handleResize) }; }, []); - React.useEffect(() => { + useEffect(() => { open ? document.body.classList.add('overflow-hidden') : document.body.classList.remove('overflow-hidden'); @@ -30,14 +31,28 @@ function Header() { return () => { document.body.classList.remove('overflow-hidden') }; }, [open]); + useEffect(() => { + const handleScroll = () => { + const scrollTop = window.scrollY; + scrollTop > 50 ? setIsScrolled(true) : setIsScrolled(false); + }; + + window.addEventListener("scroll", handleScroll); + + return () => window.removeEventListener("scroll", handleScroll); + }, []); + return ( -

+
-

+

Green Ecolution