diff --git a/frontend/src/components/organisms/header/index.tsx b/frontend/src/components/organisms/header/index.tsx index 3e04e83..5917ab2 100644 --- a/frontend/src/components/organisms/header/index.tsx +++ b/frontend/src/components/organisms/header/index.tsx @@ -19,6 +19,7 @@ const Header: FC = ({ links }): JSX.Element => { const isMediumScreen = useScreenCondition('(max-width: 768px)') const [header, setHeader] = useState(false) + const [activeSection, setActiveSection] = useState('Home') useEffect(() => { window.addEventListener('scroll', () => { @@ -36,6 +37,34 @@ const Header: FC = ({ links }): JSX.Element => { } } + const handleScroll = () => { + const scrollPosition = window.scrollY || document.documentElement.scrollTop + + // Determine the active section based on scroll position + for (const link of links) { + const targetElement = document.getElementById(link.href.substring(1)) + if (targetElement) { + const offsetTop = targetElement.getBoundingClientRect().top - 50 // Adjust this offset as needed + const offsetBottom = offsetTop + targetElement.clientHeight + + if (scrollPosition >= offsetTop && scrollPosition < offsetBottom) { + setActiveSection(link.href) + break + } + } + } + } + + useEffect(() => { + // Attach the scroll event listener when the component mounts + window.addEventListener('scroll', handleScroll) + + // Remove the event listener when the component unmounts + return () => { + window.removeEventListener('scroll', handleScroll) + } + }, []) // Empty dependency array ensures the effect runs only once on mount + return (
= ({ links }): JSX.Element => { {!isMediumScreen && (