'use client' import { usePathname } from 'next/navigation' import React, { useEffect, useState } from 'react' const ScrollProgress = () => { const [progressPercentage, setProgressPercentage] = useState(0) const pathname = usePathname() const isBlogPost = /^\/blog\/[^/]+\/?$/.test(pathname ?? '') const handleScroll = () => { if (typeof document === 'undefined') return null const article = document?.querySelector('article') if (!article) return null const { top, height } = (article as any)?.getBoundingClientRect() let scrollDistance = -top let progressPercentage = (scrollDistance / (height - document.documentElement.clientHeight)) * 100 setProgressPercentage(progressPercentage) } useEffect(() => { if (!isBlogPost) return window.addEventListener('scroll', handleScroll) return () => { window.removeEventListener('scroll', handleScroll) } }, [isBlogPost]) if (!isBlogPost) return null let isActive = progressPercentage <= 100 return (
) } export default ScrollProgress