Branchwell

AI Resource Pro / website · airesourcepro.com / src / components / tutorial / reading-progress.tsx

Raw 934 B on b7854f3692d64213a2fce644d0b5101d3e345b03
1"use client";
2
3import { useEffect, useRef } from "react";
4
5// A thin bar along the top of the window that fills as you read
6export function ReadingProgress() {
7 const bar = useRef<HTMLDivElement>(null);
8 useEffect(() => {
9 const update = () => {
10 const max = document.documentElement.scrollHeight - window.innerHeight;
11 if (bar.current) bar.current.style.transform = `scaleX(${max > 0 ? Math.min(window.scrollY / max, 1) : 0})`;
12 };
13 update();
14 window.addEventListener("scroll", update, { passive: true });
15 window.addEventListener("resize", update);
16 return () => {
17 window.removeEventListener("scroll", update);
18 window.removeEventListener("resize", update);
19 };
20 }, []);
21 return (
22 <div aria-hidden className="fixed inset-x-0 top-0 z-40 h-[3px]">
23 <div ref={bar} className="h-full origin-left bg-[var(--tc,var(--brand))]" style={{ transform: "scaleX(0)" }} />
24 </div>
25 );
26}