import React, { FC, useEffect, useRef } from 'react' import Link from 'next/link' import { WeekDayProps, mainDays } from './data' import { cn } from 'ui' import { isBrowser } from 'common' import SectionContainer from '~/components/Layouts/SectionContainer' const LWXStickyNav: FC = () => { const days = mainDays() const OFFSET = 66 const anchors = useRef | null>(null) const links = useRef | null>(null) const handleScroll = () => { let newActiveAnchor: string = '' anchors.current?.forEach((anchor) => { const { y: offsetFromTop } = anchor.getBoundingClientRect() if (offsetFromTop - OFFSET < 0) { newActiveAnchor = anchor.id } }) links.current?.forEach((link) => { link.classList.remove('text-foreground!') const sanitizedHref = decodeURI(link.getAttribute('href') ?? '') .split('#') .splice(-1) .join('') const isMatch = sanitizedHref === newActiveAnchor if (isMatch) { link.classList.add('text-foreground!') } }) } useEffect(() => { if (!isBrowser) return anchors.current = document.querySelectorAll('.lw-nav-anchor') links.current = document.querySelectorAll('.lw-sticky-nav li a') window.addEventListener('scroll', handleScroll) return () => { window.removeEventListener('scroll', handleScroll) } }, []) return (
) } export default LWXStickyNav