Become a Pro / website · becomeapro.ai / src / components / tutorial / reading-progress.tsx
| 1 | "use client"; |
| 2 | |
| 3 | import { useEffect, useRef } from "react"; |
| 4 | |
| 5 | // A thin bar along the top of the window that fills as you read |
| 6 | export 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 | } |