~/components/scroll-progress
Scroll Progress
A thin accent bar that fills as the page or a scroll container is scrolled.
$ tail -n 40 app.log # scroll me
[01] log line 1: all systems nominal
[02] log line 2: all systems nominal
[03] log line 3: all systems nominal
[04] log line 4: all systems nominal
[05] log line 5: all systems nominal
[06] log line 6: all systems nominal
[07] log line 7: all systems nominal
[08] log line 8: all systems nominal
[09] log line 9: all systems nominal
[10] log line 10: all systems nominal
[11] log line 11: all systems nominal
[12] log line 12: all systems nominal
[13] log line 13: all systems nominal
[14] log line 14: all systems nominal
[15] log line 15: all systems nominal
[16] log line 16: all systems nominal
[17] log line 17: all systems nominal
[18] log line 18: all systems nominal
[19] log line 19: all systems nominal
[20] log line 20: all systems nominal
[21] log line 21: all systems nominal
[22] log line 22: all systems nominal
[23] log line 23: all systems nominal
[24] log line 24: all systems nominal
[25] log line 25: all systems nominal
[26] log line 26: all systems nominal
[27] log line 27: all systems nominal
[28] log line 28: all systems nominal
[29] log line 29: all systems nominal
[30] log line 30: all systems nominal
[31] log line 31: all systems nominal
[32] log line 32: all systems nominal
[33] log line 33: all systems nominal
[34] log line 34: all systems nominal
[35] log line 35: all systems nominal
[36] log line 36: all systems nominal
[37] log line 37: all systems nominal
[38] log line 38: all systems nominal
[39] log line 39: all systems nominal
[40] log line 40: all systems nominal
"use client";
import { useEffect, useRef, type RefObject } from "react";
export type ScrollProgressProps = {
/** Scroll container to track. Leave empty to track the page. @default undefined (the window) */
target?: RefObject<HTMLElement | null>;
/** Pin the bar to the viewport. false places it in the nearest positioned parent instead (use with `target`). @default true */
fixed?: boolean;
/** Edge the bar sits on. @default "top" */
position?: "top" | "bottom";
/** Accent the bar is drawn in. @default "blue" */
color?: "blue" | "amber";
/** Bar thickness in px. @default 2 */
thickness?: number;
/** Extra classes for the bar. */
className?: string;
};
const BG = { blue: "bg-mpk-blue", amber: "bg-mpk-amber" } as const;
/**
* A thin bar that fills from left to right as the page (or a scroll container)
* is scrolled. It follows the scroll position directly, with no easing, so it
* reads the same under reduced motion. Decorative: hidden from assistive technology.
*/
export function ScrollProgress({
target,
fixed = true,
position = "top",
color = "blue",
thickness = 2,
className = "",
}: ScrollProgressProps) {
const ref = useRef<HTMLDivElement>(null);
useEffect(() => {
const el = target?.current ?? null;
const source: HTMLElement | Window = el ?? window;
let raf = 0;
const update = () => {
raf = 0;
const max = el ? el.scrollHeight - el.clientHeight : document.documentElement.scrollHeight - window.innerHeight;
const top = el ? el.scrollTop : window.scrollY;
const p = max > 0 ? Math.min(1, Math.max(0, top / max)) : 0;
if (ref.current) ref.current.style.transform = `scaleX(${p})`;
};
const schedule = () => {
if (!raf) raf = requestAnimationFrame(update);
};
update();
source.addEventListener("scroll", schedule, { passive: true });
window.addEventListener("resize", schedule);
// Content that grows or shrinks changes the scroll range.
const ro = new ResizeObserver(schedule);
ro.observe(el ?? document.documentElement);
if (el?.firstElementChild) ro.observe(el.firstElementChild);
return () => {
source.removeEventListener("scroll", schedule);
window.removeEventListener("resize", schedule);
ro.disconnect();
cancelAnimationFrame(raf);
};
}, [target]);
return (
<div
ref={ref}
aria-hidden="true"
style={{ height: thickness, transform: "scaleX(0)" }}
className={`pointer-events-none inset-x-0 z-[60] origin-left ${fixed ? "fixed" : "absolute"} ${
position === "top" ? "top-0" : "bottom-0"
} ${BG[color]} ${className}`}
/>
);
}Installation
npx mpkcn add scroll-progress01Add mpkcn-theme.css if you haven't yet (see Installation)
02Copy the source into your components folder
"use client";
import { useEffect, useRef, type RefObject } from "react";
export type ScrollProgressProps = {
/** Scroll container to track. Leave empty to track the page. @default undefined (the window) */
target?: RefObject<HTMLElement | null>;
/** Pin the bar to the viewport. false places it in the nearest positioned parent instead (use with `target`). @default true */
fixed?: boolean;
/** Edge the bar sits on. @default "top" */
position?: "top" | "bottom";
/** Accent the bar is drawn in. @default "blue" */
color?: "blue" | "amber";
/** Bar thickness in px. @default 2 */
thickness?: number;
/** Extra classes for the bar. */
className?: string;
};
const BG = { blue: "bg-mpk-blue", amber: "bg-mpk-amber" } as const;
/**
* A thin bar that fills from left to right as the page (or a scroll container)
* is scrolled. It follows the scroll position directly, with no easing, so it
* reads the same under reduced motion. Decorative: hidden from assistive technology.
*/
export function ScrollProgress({
target,
fixed = true,
position = "top",
color = "blue",
thickness = 2,
className = "",
}: ScrollProgressProps) {
const ref = useRef<HTMLDivElement>(null);
useEffect(() => {
const el = target?.current ?? null;
const source: HTMLElement | Window = el ?? window;
let raf = 0;
const update = () => {
raf = 0;
const max = el ? el.scrollHeight - el.clientHeight : document.documentElement.scrollHeight - window.innerHeight;
const top = el ? el.scrollTop : window.scrollY;
const p = max > 0 ? Math.min(1, Math.max(0, top / max)) : 0;
if (ref.current) ref.current.style.transform = `scaleX(${p})`;
};
const schedule = () => {
if (!raf) raf = requestAnimationFrame(update);
};
update();
source.addEventListener("scroll", schedule, { passive: true });
window.addEventListener("resize", schedule);
// Content that grows or shrinks changes the scroll range.
const ro = new ResizeObserver(schedule);
ro.observe(el ?? document.documentElement);
if (el?.firstElementChild) ro.observe(el.firstElementChild);
return () => {
source.removeEventListener("scroll", schedule);
window.removeEventListener("resize", schedule);
ro.disconnect();
cancelAnimationFrame(raf);
};
}, [target]);
return (
<div
ref={ref}
aria-hidden="true"
style={{ height: thickness, transform: "scaleX(0)" }}
className={`pointer-events-none inset-x-0 z-[60] origin-left ${fixed ? "fixed" : "absolute"} ${
position === "top" ? "top-0" : "bottom-0"
} ${BG[color]} ${className}`}
/>
);
}03Update the import paths to match your project
Usage
Render it once, in your root layout or on long pages. It pins a thin bar to the top of the viewport that fills as the page is scrolled.
import { ScrollProgress } from "@/components/mpkcn/scroll-progress";
export default function ArticleLayout({ children }: { children: React.ReactNode }) {
return (
<>
<ScrollProgress />
<article>{children}</article>
</>
);
}Updates are batched to one per frame, and the bar moves with transform, so it never triggers layout. It also follows content that grows or shrinks.
Examples
Track a scroll container
Pass a ref as target and fixed={false} to place the bar in the nearest positioned parent.
$ tail -n 40 app.log # scroll me
[01] log line 1: all systems nominal
[02] log line 2: all systems nominal
[03] log line 3: all systems nominal
[04] log line 4: all systems nominal
[05] log line 5: all systems nominal
[06] log line 6: all systems nominal
[07] log line 7: all systems nominal
[08] log line 8: all systems nominal
[09] log line 9: all systems nominal
[10] log line 10: all systems nominal
[11] log line 11: all systems nominal
[12] log line 12: all systems nominal
[13] log line 13: all systems nominal
[14] log line 14: all systems nominal
[15] log line 15: all systems nominal
[16] log line 16: all systems nominal
[17] log line 17: all systems nominal
[18] log line 18: all systems nominal
[19] log line 19: all systems nominal
[20] log line 20: all systems nominal
[21] log line 21: all systems nominal
[22] log line 22: all systems nominal
[23] log line 23: all systems nominal
[24] log line 24: all systems nominal
[25] log line 25: all systems nominal
[26] log line 26: all systems nominal
[27] log line 27: all systems nominal
[28] log line 28: all systems nominal
[29] log line 29: all systems nominal
[30] log line 30: all systems nominal
[31] log line 31: all systems nominal
[32] log line 32: all systems nominal
[33] log line 33: all systems nominal
[34] log line 34: all systems nominal
[35] log line 35: all systems nominal
[36] log line 36: all systems nominal
[37] log line 37: all systems nominal
[38] log line 38: all systems nominal
[39] log line 39: all systems nominal
[40] log line 40: all systems nominal
import { useRef } from "react";
import { ScrollProgress } from "@/components/mpkcn/scroll-progress";
export function LogBox() {
const ref = useRef<HTMLDivElement>(null);
return (
<div className="relative h-[300px] border border-mpk-line">
<ScrollProgress target={ref} fixed={false} />
<div ref={ref} className="h-full overflow-y-auto p-5">
…
</div>
</div>
);
}Amber, at the bottom
$ tail -n 40 app.log # scroll me
[01] log line 1: all systems nominal
[02] log line 2: all systems nominal
[03] log line 3: all systems nominal
[04] log line 4: all systems nominal
[05] log line 5: all systems nominal
[06] log line 6: all systems nominal
[07] log line 7: all systems nominal
[08] log line 8: all systems nominal
[09] log line 9: all systems nominal
[10] log line 10: all systems nominal
[11] log line 11: all systems nominal
[12] log line 12: all systems nominal
[13] log line 13: all systems nominal
[14] log line 14: all systems nominal
[15] log line 15: all systems nominal
[16] log line 16: all systems nominal
[17] log line 17: all systems nominal
[18] log line 18: all systems nominal
[19] log line 19: all systems nominal
[20] log line 20: all systems nominal
[21] log line 21: all systems nominal
[22] log line 22: all systems nominal
[23] log line 23: all systems nominal
[24] log line 24: all systems nominal
[25] log line 25: all systems nominal
[26] log line 26: all systems nominal
[27] log line 27: all systems nominal
[28] log line 28: all systems nominal
[29] log line 29: all systems nominal
[30] log line 30: all systems nominal
[31] log line 31: all systems nominal
[32] log line 32: all systems nominal
[33] log line 33: all systems nominal
[34] log line 34: all systems nominal
[35] log line 35: all systems nominal
[36] log line 36: all systems nominal
[37] log line 37: all systems nominal
[38] log line 38: all systems nominal
[39] log line 39: all systems nominal
[40] log line 40: all systems nominal
<ScrollProgress color="amber" position="bottom" thickness={3} />Props
| Prop | Type | Default | Description |
|---|---|---|---|
| target | RefObject<HTMLElement | null> | window | Scroll container to track. Leave empty to track the page. |
| fixed | boolean | true | Pin the bar to the viewport. false places it in the nearest positioned parent instead (use with target). |
| position | "top" | "bottom" | "top" | Edge the bar sits on. |
| color | "blue" | "amber" | "blue" | Accent the bar is drawn in. |
| thickness | number | 2 | Bar thickness in px. |
| className | string | — | Extra classes for the bar, e.g. a different z-index (default z-[60]). |
Accessibility
- The bar is decorative:
aria-hiddenandpointer-events: none. The browser's own scrollbar still carries the position for assistive technology. - It follows the scroll position directly, with no easing or animation, so nothing changes under reduced motion.