feat: add scroll progress indicator
This commit is contained in:
33
components/ui/ScrollProgress.tsx
Normal file
33
components/ui/ScrollProgress.tsx
Normal file
@@ -0,0 +1,33 @@
|
||||
"use client";
|
||||
|
||||
import { useEffect, useState } from "react";
|
||||
|
||||
export function ScrollProgress() {
|
||||
const [progress, setProgress] = useState(0);
|
||||
|
||||
useEffect(() => {
|
||||
const update = () => {
|
||||
const total = document.documentElement.scrollHeight - window.innerHeight;
|
||||
setProgress(total > 0 ? (window.scrollY / total) * 100 : 0);
|
||||
};
|
||||
|
||||
if (window.CSS?.supports("animation-timeline", "scroll()")) {
|
||||
return;
|
||||
}
|
||||
|
||||
update();
|
||||
window.addEventListener("scroll", update, { passive: true });
|
||||
return () => window.removeEventListener("scroll", update);
|
||||
}, []);
|
||||
|
||||
return (
|
||||
<div
|
||||
className="scroll-progress"
|
||||
style={{ width: `${progress}%` }}
|
||||
role="progressbar"
|
||||
aria-valuenow={Math.round(progress)}
|
||||
aria-valuemin={0}
|
||||
aria-valuemax={100}
|
||||
/>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user