Installation
Add the component
npx shadcn@latest add https://prequeluianimate.terravidhal.me/r/scroll-progress.jsonFirst component? Set up your project first
Frameworks
The live preview is React. Each tab below is the file that ships for that framework, with the same durations, springs and reduced-motion rule.
"use client";
import * as React from "react";
import { motion, useMotionValue, useSpring, type Transition } from "motion/react";
import { cn } from "@/lib/utils";
import { scrollSpring, useTempoTransition } from "./motion";
export type ScrollProgressProps = React.HTMLAttributes<HTMLDivElement> & {
containerRef?: React.RefObject<HTMLElement | null>;
barClassName?: string;
transition?: Transition;
};
export function ScrollProgress({
containerRef,
barClassName,
transition,
className,
...props
}: ScrollProgressProps) {
const progress = useMotionValue(0);
const spring = useTempoTransition(transition ?? scrollSpring);
const smooth = useSpring(progress, spring);
React.useEffect(() => {
const target = containerRef?.current;
const read = () => {
if (target) {
const max = target.scrollHeight - target.clientHeight;
progress.set(max > 0 ? Math.min(1, Math.max(0, target.scrollTop / max)) : 0);
} else {
const max = document.documentElement.scrollHeight - window.innerHeight;
progress.set(max > 0 ? Math.min(1, Math.max(0, window.scrollY / max)) : 0);
}
};
read();
const node: HTMLElement | Window = target ?? window;
node.addEventListener("scroll", read, { passive: true });
window.addEventListener("resize", read);
return () => {
node.removeEventListener("scroll", read);
window.removeEventListener("resize", read);
};
}, [containerRef, progress]);
return (
<div
role="progressbar"
aria-label="Scroll progress"
data-slot="scroll-progress"
className={cn("h-1 w-full overflow-hidden bg-accent", className)}
{...props}
>
<motion.div
style={{ scaleX: smooth }}
className={cn("h-full origin-left bg-foreground", barClassName)}
/>
</div>
);
}Install
npx shadcn@latest add https://prequeluianimate.terravidhal.me/r/scroll-progress.jsonDependencies
motion lucide-reactThe registry keeps the component source, its motion tokens and the reduced-motion branch together, so the install never pulls a different animation from the preview.
Usage
Import the component and compose it exactly like the primitive it wraps. Motion is enabled by default; the tempo prop scales every duration and softens every spring.
import { useRef } from "react";
import { ScrollProgress } from "@/components/prequelui/scroll-progress";
export default function Demo() {
const ref = useRef<HTMLDivElement>(null);
return (
<div className="w-[320px]">
<ScrollProgress containerRef={ref} />
<div ref={ref} className="h-40 overflow-y-auto">
{/* long content */}
</div>
</div>
);
}import { ScrollProgress } from "@/components/prequelui/scroll-progress";
export function Variant() {
return (
<ScrollProgress tempo={1.2}>
…
</ScrollProgress>
);
}Animation
The bar follows a container or the page through a spring that is damped to zero bounce. Scroll events only write the raw ratio; the spring is what gives the bar its short, smooth catch-up.
- Spring: 250 / 40 with bounce 0.
- A ref target measures its own scroll range; no ref targets the page.
- The bar scales on X from a left origin, never animating width.
- A resize listener re-measures the range.
- Scroll updates are passive and never block the page.
API Reference
Each part accepts the usual HTML props in addition to the motion props below. No accessibility prop is hidden behind the animation layer.
ScrollProgress
| Prop | Type | Default |
|---|---|---|
| containerRef | RefObject<HTMLElement> | — |
| barClassName | string | — |
| transition | Transition | component spring or tween |
| className | string | — |