How to Show a Reading Progress Bar in React
A reading progress bar can be built by measuring the article element's position with getBoundingClientRect() and calculating the percentage as "0% when the top of the article reaches the top of the screen, 100% when the bottom of the article reaches the bottom of the screen." If you calculate it from the scroll amount of the whole page instead, then on a page with a comment section or footer below the article, the bar does not reach 100% even after the reader has finished the article.
Here I built a hook, useReadingProgress, which returns the percentage, and a bar component. I also checked whether the bar goes out of sync when images load later and change the article's height. In addition, I looked at how the same bar behaves when it is built with only CSS scroll-driven animations.
The test environment is Ubuntu 24.04, Node.js 22.22.0, and Chromium 153. I used react and react-dom 19.3.1, @types/react 19.3.0, TypeScript 7.0.2, esbuild 0.28.2, and Playwright 1.63.0. I type-checked the .tsx file with tsc, transformed it with esbuild, operated Chromium with Playwright, and read the bar's value at each scroll position. I did not test Firefox, Safari, or real smartphones.

- 1. A hook that calculates the read percentage from the article element's position
- 2. Using the whole page's percentage never reaches 100% after reading
- 3. When images load later, the scroll event alone leaves the bar out of sync
- 4. Conveying the percentage to screen readers
- 5. Building it with CSS scroll-driven animations alone
A hook that calculates the read percentage from the article element's position
The top value returned by getBoundingClientRect() is the position measured from the top-left corner of the viewport (the visible area of the browser window). As you read down the article, its top edge moves up off the screen, so top becomes negative. I divide -top by the part of the article that does not fit on the screen (the article height minus the viewport height) and clamp the result to the range 0 to 1.
import { useEffect, useState, type RefObject } from 'react';
// Returns how far the reader has read the article element, from 0 to 1.
// 0 when the top of the article reaches the top of the screen, 1 when the bottom of the article reaches the bottom of the screen
export function useReadingProgress(ref: RefObject<HTMLElement | null>) {
const [progress, setProgress] = useState(0);
useEffect(() => {
const article = ref.current;
if (!article) return;
const update = () => {
const rect = article.getBoundingClientRect();
// The part of the article's height that does not fit on the screen
const scrollable = rect.height - window.innerHeight;
// If the article fits on the screen, treat it as fully read
const value = scrollable <= 0 ? 1 : -rect.top / scrollable;
setProgress(Math.min(1, Math.max(0, value)));
};
update();
window.addEventListener('scroll', update, { passive: true });
window.addEventListener('resize', update);
// Recalculate when the article height changes, for example when images load
const observer = new ResizeObserver(update);
observer.observe(article);
return () => {
window.removeEventListener('scroll', update);
window.removeEventListener('resize', update);
observer.disconnect();
};
}, [ref]);
return progress;
}
JSX cannot attach listeners to window, so I call addEventListener inside useEffect and remove the listeners in the cleanup function. This follows the same pattern as the official React reference's example of listening to a global browser event in useEffect. I added passive: true to the scroll listener. According to MDN's explanation of addEventListener, this option tells the browser that preventDefault() will not be called. The same section also notes that you do not need to worry about the passive value for scroll events.
For the bar, I fix the outer frame to the top of the screen and stretch the inner fill with transform: scaleX(). Setting transform-origin to the left edge makes the fill grow from left to right.
export function ReadingProgressBar({ value }: { value: number }) {
return (
<div
className="progress"
role="progressbar"
aria-label="記事を読んだ割合"
aria-valuemin={0}
aria-valuemax={100}
aria-valuenow={Math.round(value * 100)}
>
<div className="progress-fill" style={{ transform: `scaleX(${value})` }} />
</div>
);
}
.progress { position: fixed; top: 0; left: 0; right: 0; height: 6px; background: #eefafd; z-index: 10; }
.progress-fill { height: 100%; background: #00c8e8; transform-origin: 0 50%; }
On the consuming side, I attach a ref to the article element, pass it to the hook, and pass the returned value to the bar.
const articleRef = useRef<HTMLElement>(null);
const progress = useReadingProgress(articleRef);
return (
<>
<ReadingProgressBar value={progress} />
<header className="site-header">サイトのヘッダー</header>
<article ref={articleRef} className="article">
{/* body */}
</article>
<section className="comments">コメント欄</section>
</>
);
Using the whole page's percentage never reaches 100% after reading
The verification page stacks a 60px-high header, an article (2700px high), a 600px-high comment section, and a 100px-high footer, in that order. I set the viewport height to 800px and read the bar's value at each scroll position. "Bar" means the scaleX value actually applied to the fill. "Whole-page percentage" is scrollY divided by the page's scrollable amount, which I included for comparison.
いちばん上: scrollY=0 記事の高さ=2700 バー=0.000 aria-valuenow=0 ページ全体の割合=0.000
記事の先頭が画面の上端: scrollY=60 記事の高さ=2700 バー=0.000 aria-valuenow=0 ページ全体の割合=0.023
記事の真ん中: scrollY=1010 記事の高さ=2700 バー=0.500 aria-valuenow=50 ページ全体の割合=0.380
記事の末尾が画面の下端: scrollY=1960 記事の高さ=2700 バー=1.000 aria-valuenow=100 ページ全体の割合=0.737
ページのいちばん下: scrollY=2660 記事の高さ=2700 バー=1.000 aria-valuenow=100 ページ全体の割合=1.000
When the bottom of the article reached the bottom of the screen, the bar read 100%. Calculated over the whole page, the same position is 73.7%. Even though the reader has finished the article, the whole-page calculation leaves the bar unfinished by the height of the comment section and footer. Also, while the header is visible, the reader has not started the article yet, so the bar stays at 0%.
When images load later, the scroll event alone leaves the bar out of sync
If the images in an article do not have width and height set, each image has a height of 0 until it arrives, and the article becomes longer when it does. In this case the user has not scrolled, so no scroll event fires.
To check this, I delayed an image in the middle of the article by 3 seconds on the server. When it arrives, it makes the article 640px taller. I scrolled to the position where the bar reads exactly 50% (above the image) using the article height before the image arrived, then compared the bar values before and after the image loaded. First is the version that recalculates only on scroll events, which is the hook above without resize and ResizeObserver.
画像が届く前: scrollY=690 記事の高さ=2060 バー=0.500 aria-valuenow=50 ページ全体の割合=0.342
画像が届いたあと(スクロールしていない): scrollY=690 記事の高さ=2700 バー=0.500 aria-valuenow=50 ページ全体の割合=0.259
ホイールで 1px スクロールしたあと: scrollY=691 記事の高さ=2700 バー=0.332 aria-valuenow=33 ページ全体の割合=0.260
The article grew from 2060px to 2700px, but the bar stayed at 50%. It only changed to 33% after I scrolled the wheel by 1px.
With useReadingProgress, which watches the article element with ResizeObserver, the value changed as soon as the image arrived.
画像が届く前: scrollY=690 記事の高さ=2060 バー=0.500 aria-valuenow=50 ページ全体の割合=0.342
画像が届いたあと(スクロールしていない): scrollY=690 記事の高さ=2700 バー=0.332 aria-valuenow=33 ページ全体の割合=0.259
ホイールで 1px スクロールしたあと: scrollY=691 記事の高さ=2700 バー=0.332 aria-valuenow=33 ページ全体の割合=0.260
ResizeObserver is an API that notifies you when an element's size changes. It is not limited to images. If the article's height changes while the user is not scrolling, a version that measures only on scroll cannot catch that change.
Calculate the read percentage from the article element's position, and use ResizeObserver in addition to scroll and resize as the triggers for recalculation. This keeps the bar in line with how far the reader has actually read, both on pages with other content below the article and on pages where the article's height changes after it first loads.
Conveying the percentage to screen readers
The bar's outer element has role="progressbar", and aria-valuenow holds a value from 0 to 100. The MDN progressbar role page says that aria-valuenow should hold the current value and be kept up to date, and that an accessible name must always be provided. When there is no visible label, use aria-label.
When I scrolled to the middle of the article and read the Chromium accessibility tree, it showed the following:
role=progressbar name=記事を読んだ割合 value=50 valuemin=0 valuemax=100
The Prefer HTML section on the same page recommends using the HTML <progress> element where possible. Here I gave a div the role because I stretch the fill with transform. If you have no strong preference about how the bar looks, <progress> is an option. I did not test how <progress> looks or how screen readers announce it in this article.
Building it with CSS scroll-driven animations alone
With CSS scroll-driven animations, you can build the same bar without calculating the percentage in JavaScript. Give the article element a name with view-timeline-name, then specify that name in the fill's animation-timeline. By default, a named timeline can only be referenced from the element's descendants (MDN's explanation of timeline-scope), so I placed the bar inside the article element.
<article className="article article-timeline">
<div className="progress" aria-hidden="true">
<div className="progress-fill progress-fill-css" />
</div>
{/* body */}
</article>
.article-timeline { view-timeline-name: --article; }
@keyframes grow { from { transform: scaleX(0); } to { transform: scaleX(1); } }
.progress-fill-css {
animation: grow linear both;
animation-timeline: --article;
animation-range: contain;
}
The contain value of animation-range covers the range from when an element taller than the screen starts to completely cover the screen until it stops covering it (MDN's explanation of timeline range names). When reading down the article, this runs from when the top of the article reaches the top of the screen to when the bottom of the article reaches the bottom of the screen. That is the same range the hook uses. Note that the animation shorthand resets animation-timeline to auto, so write animation-timeline after animation.
Using the same page and scroll positions as the hook, I read the scaleX value actually applied to the fill.
いちばん上: scrollY=0 記事の高さ=2700 バー=0.000 ページ全体の割合=0.000
記事の先頭が画面の上端: scrollY=60 記事の高さ=2700 バー=0.000 ページ全体の割合=0.023
記事の真ん中: scrollY=1010 記事の高さ=2700 バー=0.500 ページ全体の割合=0.380
記事の末尾が画面の下端: scrollY=1960 記事の高さ=2700 バー=1.000 ページ全体の割合=0.737
ページのいちばん下: scrollY=2660 記事の高さ=2700 バー=1.000 ページ全体の割合=1.000
The values matched the hook's. With the image delayed by 3 seconds, the bar changed to 0.332 as soon as the image arrived, without waiting for a scroll.
画像が届く前: scrollY=690 記事の高さ=2060 バー=0.500 ページ全体の割合=0.342
画像が届いたあと(スクロールしていない): scrollY=690 記事の高さ=2700 バー=0.332 ページ全体の割合=0.259
However, with CSS alone, the percentage is not available as a JavaScript value, so I cannot set aria-valuenow. For this reason, I gave the bar aria-hidden="true" and treat it as purely visual. If you want to convey the percentage to screen readers, or use it elsewhere, such as in a "time left" display, the hook approach is a better fit.
MDN's compatibility data lists animation-timeline as available from Chrome 115, Firefox 159, and Safari 26. According to the same data, Firefox 159 has a release date of October 27, 2026, and its status is beta. In this article, I tested only Chromium 153. If you need to support browsers whose versions do not support animation-timeline, use the hook approach.





