# Scroll Progress

> A spring-animated progress bar that tracks scroll position — works for the full page or a scrollable container element.

Docs: https://ui.cnippet.dev/motion/scroll-animations/scroll-progress

```tsx
"use client";

import { ScrollProgress } from "@/registry/default/motion/scroll-progress";

export default function ScrollProgressFixed() {
  return (
    <div className="relative flex min-h-50 flex-col items-center justify-start overflow-y-auto px-6">
      <div className="sticky top-0 z-10 w-full">
        <ScrollProgress className="fixed top-0 right-0 left-0 h-1 bg-primary" />
      </div>
      <div className="w-full max-w-lg space-y-4 py-6">
        <h2 className="font-bold text-foreground text-xl">
          Scroll to see progress
        </h2>
        {Array.from({ length: 6 }).map((_, i) => (
          <p className="text-muted-foreground text-sm leading-relaxed" key={i}>
            Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do
            eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim
            ad minim veniam, quis nostrud exercitation ullamco laboris.
          </p>
        ))}
      </div>
    </div>
  );
}
```

## Installation

**CLI**

```bash
npx shadcn@latest add @cnippet/scroll-progress
```

**Manual**

1. Install the following dependencies:

```bash
npm install motion
```

2. Copy and paste the following code into your project.

`components/motion/scroll-progress.tsx`

```tsx
"use client";

import {
  motion,
  type SpringOptions,
  useReducedMotion,
  useScroll,
  useSpring,
} from "motion/react";
import type { RefObject } from "react";
import { cn } from "@/lib/utils";

export type ScrollProgressProps = {
  className?: string;
  springOptions?: SpringOptions;
  containerRef?: RefObject<HTMLDivElement | null>;
};

const DEFAULT_SPRING_OPTIONS: SpringOptions = {
  damping: 50,
  restDelta: 0.001,
  stiffness: 200,
};

export function ScrollProgress({
  className,
  springOptions,
  containerRef,
}: ScrollProgressProps) {
  const reduceMotion = useReducedMotion();
  const { scrollYProgress } = useScroll({
    container: containerRef,
  });

  const scaleX = useSpring(scrollYProgress, {
    ...DEFAULT_SPRING_OPTIONS,
    ...(springOptions ?? {}),
  });

  return (
    <motion.div
      className={cn("inset-x-0 top-0 h-1 origin-left", className)}
      style={{ scaleX: reduceMotion ? scrollYProgress : scaleX }}
    />
  );
}
```

3. Update the import paths to match your project setup.

## Usage

```tsx
import { ScrollProgress } from "@/components/motion/scroll-progress";
```

```tsx
<ScrollProgress className="fixed inset-x-0 top-0 h-1 bg-primary" />
```

## Anatomy

```tsx
<ScrollProgress className="fixed inset-x-0 top-0 h-1 bg-primary" /> {/* bar scaled by scroll progress */}
```

## Good to know

- It has no position or color of its own. Place it with `className`, for example `fixed inset-x-0 top-0`, and give it a background color.
- By default it tracks the whole page. To track a scrollable element instead, pass that element's ref as `containerRef`.
- Change how smoothly it follows scrolling with `springOptions` (Motion spring settings).
- When the user has turned on reduced motion, the bar follows the scroll position directly, without the spring.

## Props

| Prop | Type | Default | Description |
|------|------|---------|-------------|
| `className` | `string` | — | Class applied to the progress bar element — use this to set colour, height, and position |
| `springOptions` | `SpringOptions` | `{ stiffness: 200, damping: 50, restDelta: 0.001 }` | Spring physics for smoothing the progress value |
| `containerRef` | `RefObject<HTMLDivElement>` | — | When provided, tracks scroll inside that container instead of the window |

## Examples

### Fixed Reading Progress Bar

A 1 px bar fixed to the top of the viewport, coloured with the primary theme token. Tracks window scroll.

```tsx
"use client";

import { ScrollProgress } from "@/registry/default/motion/scroll-progress";

export default function ScrollProgressFixed() {
  return (
    <div className="relative flex min-h-50 flex-col items-center justify-start overflow-y-auto px-6">
      <div className="sticky top-0 z-10 w-full">
        <ScrollProgress className="fixed top-0 right-0 left-0 h-1 bg-primary" />
      </div>
      <div className="w-full max-w-lg space-y-4 py-6">
        <h2 className="font-bold text-foreground text-xl">
          Scroll to see progress
        </h2>
        {Array.from({ length: 6 }).map((_, i) => (
          <p className="text-muted-foreground text-sm leading-relaxed" key={i}>
            Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do
            eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim
            ad minim veniam, quis nostrud exercitation ullamco laboris.
          </p>
        ))}
      </div>
    </div>
  );
}
```

### In-Container Progress

A scrollable changelog card with a gradient progress bar along the bottom of the header. Pass a `containerRef` to scope tracking to the card's scroll area.

```tsx
"use client";

import { useRef } from "react";
import { ScrollProgress } from "@/registry/default/motion/scroll-progress";

export default function ScrollProgressContainer() {
  const containerRef = useRef<HTMLDivElement>(null);

  return (
    <div className="flex min-h-50 items-center justify-center px-6">
      <div className="w-full max-w-sm overflow-hidden rounded-xl border border-border bg-card shadow-sm">
        <div className="relative border-border border-b bg-card/80 px-4 py-3">
          <p className="font-semibold text-foreground text-sm">Changelog</p>
          <ScrollProgress
            className="absolute right-0 bottom-0 left-0 h-0.5 bg-gradient-to-r from-violet-500 to-pink-500"
            containerRef={containerRef}
          />
        </div>
        <div
          className="h-48 overflow-y-auto p-4 text-muted-foreground text-sm"
          ref={containerRef}
        >
          {["v1.3", "v1.2", "v1.1", "v1.0"].map((v) => (
            <div className="mb-4" key={v}>
              <p className="mb-1 font-semibold text-foreground">{v}</p>
              <p className="leading-relaxed">
                Added new animated components, improved accessibility, fixed
                scroll-jank in Safari, and bumped peer deps to Motion 12.
              </p>
            </div>
          ))}
        </div>
      </div>
    </div>
  );
}
```

### Styled Bar with Percentage Label

A rounded gradient bar with a live percentage counter rendered next to it using `useTransform`. The label updates in sync with the smooth spring value.

```tsx
"use client";

import { motion, useScroll, useSpring, useTransform } from "motion/react";
import { ScrollProgress } from "@/registry/default/motion/scroll-progress";

export default function ScrollProgressStyled() {
  const { scrollYProgress } = useScroll();
  const smoothProgress = useSpring(scrollYProgress, {
    damping: 50,
    restDelta: 0.001,
    stiffness: 200,
  });
  const percentage = useTransform(smoothProgress, [0, 1], [0, 100]);

  return (
    <div className="relative flex min-h-50 flex-col items-center justify-start overflow-y-auto px-6">
      <div className="sticky top-0 z-10 flex w-full items-center gap-3 border-border border-b bg-background/80 px-4 py-2 backdrop-blur">
        <ScrollProgress className="h-1.5 flex-1 rounded-full bg-gradient-to-r from-blue-500 via-violet-500 to-pink-500" />
        <motion.span className="shrink-0 text-muted-foreground text-xs tabular-nums">
          {useTransform(percentage, (v) => `${Math.round(v)}%`)}
        </motion.span>
      </div>
      <div className="w-full max-w-lg space-y-4 py-6">
        <h2 className="font-bold text-foreground text-xl">Reading progress</h2>
        {Array.from({ length: 7 }).map((_, i) => (
          <p className="text-muted-foreground text-sm leading-relaxed" key={i}>
            Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris
            nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in
            reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla.
          </p>
        ))}
      </div>
    </div>
  );
}
```
