|
| 1 | +import React, { useEffect, useState, useCallback, useRef } from "react"; |
| 2 | +import styles from "./styles.module.css"; |
| 3 | + |
| 4 | +const MIN_REMAINING_MINUTES = 1; |
| 5 | + |
| 6 | +interface Props { |
| 7 | + totalReadTime: number; // in minutes |
| 8 | + authorCardRef: React.RefObject<HTMLElement | null>; |
| 9 | +} |
| 10 | + |
| 11 | +export default function ReadingTimeIndicator({ |
| 12 | + totalReadTime, |
| 13 | + authorCardRef, |
| 14 | +}: Props): JSX.Element | null { |
| 15 | + const [visible, setVisible] = useState(false); |
| 16 | + const [remainingTime, setRemainingTime] = useState(totalReadTime); |
| 17 | + const rafRef = useRef<number | null>(null); |
| 18 | + |
| 19 | + const computeState = useCallback(() => { |
| 20 | + const scrollY = window.scrollY; |
| 21 | + const winHeight = window.innerHeight; |
| 22 | + const docHeight = document.documentElement.scrollHeight; |
| 23 | + |
| 24 | + const maxScroll = docHeight - winHeight; |
| 25 | + const pageScrollPercent = maxScroll > 0 ? (scrollY / maxScroll) * 100 : 0; |
| 26 | + |
| 27 | + // Hide when the author card has entered the viewport, |
| 28 | + // or fall back to hiding at 90% page scroll when there is no author card |
| 29 | + let authorCardReached = false; |
| 30 | + if (authorCardRef.current) { |
| 31 | + const rect = authorCardRef.current.getBoundingClientRect(); |
| 32 | + authorCardReached = rect.top < winHeight; |
| 33 | + } else { |
| 34 | + authorCardReached = pageScrollPercent >= 90; |
| 35 | + } |
| 36 | + |
| 37 | + const shouldBeVisible = pageScrollPercent >= 15 && !authorCardReached; |
| 38 | + setVisible(shouldBeVisible); |
| 39 | + |
| 40 | + // Calculate remaining time proportional to how far through the content the |
| 41 | + // user has scrolled. Use author card position when available; otherwise use |
| 42 | + // overall page scroll percentage as fallback. |
| 43 | + if (shouldBeVisible) { |
| 44 | + let readProgress = 0; |
| 45 | + if (authorCardRef.current) { |
| 46 | + const authorCardAbsTop = |
| 47 | + authorCardRef.current.getBoundingClientRect().top + scrollY; |
| 48 | + readProgress = |
| 49 | + authorCardAbsTop > 0 |
| 50 | + ? Math.max(0, Math.min(1, scrollY / authorCardAbsTop)) |
| 51 | + : 0; |
| 52 | + } else { |
| 53 | + readProgress = Math.max(0, Math.min(1, pageScrollPercent / 90)); |
| 54 | + } |
| 55 | + const remaining = Math.max( |
| 56 | + MIN_REMAINING_MINUTES, |
| 57 | + Math.ceil(totalReadTime * (1 - readProgress)) |
| 58 | + ); |
| 59 | + setRemainingTime(remaining); |
| 60 | + } |
| 61 | + }, [totalReadTime, authorCardRef]); |
| 62 | + |
| 63 | + const handleScroll = useCallback(() => { |
| 64 | + // Throttle via requestAnimationFrame to avoid expensive layout reads on |
| 65 | + // every scroll event. |
| 66 | + if (rafRef.current !== null) return; |
| 67 | + rafRef.current = requestAnimationFrame(() => { |
| 68 | + rafRef.current = null; |
| 69 | + computeState(); |
| 70 | + }); |
| 71 | + }, [computeState]); |
| 72 | + |
| 73 | + useEffect(() => { |
| 74 | + if (totalReadTime < MIN_REMAINING_MINUTES) return; |
| 75 | + window.addEventListener("scroll", handleScroll, { passive: true }); |
| 76 | + computeState(); |
| 77 | + return () => { |
| 78 | + window.removeEventListener("scroll", handleScroll); |
| 79 | + if (rafRef.current !== null) { |
| 80 | + cancelAnimationFrame(rafRef.current); |
| 81 | + rafRef.current = null; |
| 82 | + } |
| 83 | + }; |
| 84 | + }, [handleScroll, computeState, totalReadTime]); |
| 85 | + |
| 86 | + if (totalReadTime < MIN_REMAINING_MINUTES || !visible) return null; |
| 87 | + |
| 88 | + const minLabel = remainingTime === MIN_REMAINING_MINUTES ? "min" : "mins"; |
| 89 | + |
| 90 | + return ( |
| 91 | + <div |
| 92 | + className={styles.container} |
| 93 | + role="status" |
| 94 | + aria-label={`Estimated reading time: ${remainingTime} ${minLabel} remaining`} |
| 95 | + aria-live="polite" |
| 96 | + aria-atomic="true" |
| 97 | + > |
| 98 | + <svg |
| 99 | + xmlns="http://www.w3.org/2000/svg" |
| 100 | + viewBox="0 0 24 24" |
| 101 | + width="14" |
| 102 | + height="14" |
| 103 | + fill="none" |
| 104 | + stroke="currentColor" |
| 105 | + strokeWidth="2" |
| 106 | + strokeLinecap="round" |
| 107 | + strokeLinejoin="round" |
| 108 | + aria-hidden="true" |
| 109 | + className={styles.icon} |
| 110 | + > |
| 111 | + <circle cx="12" cy="12" r="10" /> |
| 112 | + <polyline points="12 6 12 12 16 14" /> |
| 113 | + </svg> |
| 114 | + <span>{remainingTime} {minLabel} remaining</span> |
| 115 | + </div> |
| 116 | + ); |
| 117 | +} |
0 commit comments