-
-
Notifications
You must be signed in to change notification settings - Fork 78
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f1dc660
commit 93d4e40
Showing
1 changed file
with
33 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
'use client'; | ||
|
||
import React, { useState, useEffect } from 'react'; | ||
import { usePathname } from 'next/navigation'; | ||
|
||
const ScrollProgressBar = () => { | ||
const [scroll, setScroll] = useState(0); | ||
const pathname = usePathname(); // Use usePathname to get the current path | ||
|
||
const handleScroll = () => { | ||
const scrolled = document.documentElement.scrollTop; | ||
const totalScroll = document.documentElement.scrollHeight - document.documentElement.clientHeight; | ||
const scrolledPercentage = `${(scrolled / totalScroll) * 100}%`; | ||
setScroll(scrolledPercentage); | ||
}; | ||
|
||
useEffect(() => { | ||
window.addEventListener('scroll', handleScroll); | ||
return () => window.removeEventListener('scroll', handleScroll); | ||
}, []); | ||
|
||
useEffect(() => { | ||
setScroll(0); // Reset scroll progress on path change | ||
}, [pathname]); | ||
|
||
return ( | ||
<div className="scroll-progress-container"> | ||
<div className="scroll-progress-bar" style={{ width: scroll }} /> | ||
</div> | ||
); | ||
}; | ||
|
||
export default ScrollProgressBar; |