# Scroll progress

> A progress bar driven by the scroll position of the page or a specific scroll container.

- Kind: ui · Category: Feedback · Interactivity: script
- Install: `npx shadcn@latest add @haze/scroll-progress`
- File: `src/components/haze/ui/scroll-progress.astro`
- npm dependencies: none
- Registry dependencies: @haze/utils
- Docs page: https://hazeui.com/ui/scroll-progress

## Use when
- Readers want to see how far they have moved through a long article
- An embedded scroll panel needs a progress indicator outside its viewport

## Avoid when
- The percentage comes from an upload, task, or saving operation. Use progress.

## Alternatives
- [progress](https://hazeui.com/ui/progress.md)

## Props

| Name | Type | Default | Required | Description |
|---|---|---|---|---|
| target | `string` |  | no | ID of a scroll container. Defaults to the page's scroll element. |
| label | `string` | "Reading progress" | no | Accessible name of the indicator. |
| class | `string` |  | no | Additional classes on the progress element. |

## Accessibility
- The indicator is named and reports a numeric value without a live region.
- Make an embedded scroll region focusable and give it an accessible name.

## Agent prompts

### Install and use

Install the Haze UI component "Scroll progress" with `npx shadcn@latest add @haze/scroll-progress`. If components.json has no "@haze" registry yet, add "registries": { "@haze": "https://hazeui.com/r/{name}.json" }. If src/styles/haze.css does not exist yet, run `npx shadcn@latest add @haze/theme` once and import it from the global stylesheet after Tailwind. The CLI stops at an overwrite question when a Haze file already exists and differs, even with --yes, and then installs nothing after it: read its output. If the user has not edited their Haze files, re-run with --overwrite to take the registry versions; if they have, ask them before overwriting. Then import ScrollProgress from "@/components/haze/ui/scroll-progress.astro" (or the relative path) and render it where it fits the page. Keep Haze token classes (bg-background, text-muted-foreground, border-border...) and never hardcode colors. Docs: https://hazeui.com/ui/scroll-progress.md

### Customize with real content

Adapt the Haze scroll-progress ui for our product. Replace the demo values of these props with our real content: target, label, class. Keep the layout, the heading level rules and these accessibility details: The indicator is named and reports a numeric value without a live region. Make an embedded scroll region focusable and give it an accessible name. Do not add raw color classes; use the Haze tokens.

### Choose the right block

Before using scroll-progress, check it fits: use it when readers want to see how far they have moved through a long article; an embedded scroll panel needs a progress indicator outside its viewport. Avoid it when the percentage comes from an upload, task, or saving operation. use progress.. Alternatives: @haze/progress.

## Example requests that should lead here
- Add a reading progress bar for the scrollable release notes panel.

## Source

```astro
---
import { cn } from "../../../lib/haze/utils";

interface Props {
  target?: string;
  label?: string;
  class?: string;
}

const { target, label = "Reading progress", class: className } = Astro.props;
---

<progress data-haze-scroll-progress data-scroll-target={target} aria-label={label} value="0" max="100"
  class={cn(
    "hidden h-1.5 w-full overflow-hidden rounded-full border-0 bg-muted",
    "data-[overflow]:block [&::-webkit-progress-bar]:bg-transparent [&::-webkit-progress-value]:rounded-full [&::-webkit-progress-value]:bg-primary",
    "[&::-moz-progress-bar]:rounded-full [&::-moz-progress-bar]:bg-primary forced-colors:appearance-auto",
    className,
  )}></progress>

<script>
  function sync(progress: HTMLProgressElement) {
    const target = progress.dataset.scrollTarget;
    const source = target ? document.getElementById(target) : document.scrollingElement;
    if (!source) {
      progress.removeAttribute("data-overflow");
      return;
    }
    const distance = source.scrollHeight - source.clientHeight;
    progress.toggleAttribute("data-overflow", distance > 0);
    const value = distance > 0 ? Math.min(100, Math.max(0, Math.round(source.scrollTop / distance * 100))) : 0;
    if (progress.value !== value) progress.value = value;
    progress.setAttribute("data-enhanced", "");
  }

  const root = document.documentElement;
  if (!root.hasAttribute("data-haze-scroll-progress-listener")) {
    root.setAttribute("data-haze-scroll-progress-listener", "");
    let frame = 0;
    const schedule = () => {
      if (frame) return;
      frame = requestAnimationFrame(() => {
        frame = 0;
        document.querySelectorAll<HTMLProgressElement>("[data-haze-scroll-progress]").forEach(sync);
      });
    };
    document.addEventListener("scroll", schedule, { capture: true, passive: true });
    document.addEventListener("load", schedule, { capture: true });
    window.addEventListener("resize", schedule, { passive: true });
    document.addEventListener("astro:page-load", schedule);
    schedule();
  }
</script>
```
