# Scroll story

> A how-it-works section that tells itself as you scroll: steps light up at the middle, a rail fills, and one card reshapes into each step’s picture.

Every product page has a "how it works", and it’s usually four numbered boxes nobody reads in order. Here the steps scroll past a picture that stays put. Whichever step crosses the middle of the view lights up while the rest wait at a third of their strength; a hairline down the side fills with the scroll and each step’s dot fills as the line reaches it. The picture is one card that reshapes to fit each step: its size eases from one to the next while the old picture blurs out and the new one rises in, and since each picture mounts when its step arrives, whatever it does (a switch flipping, a count rolling, a name typing itself) plays right then. "02 / 04" rolls above it. On a phone the picture sticks to the top and the steps slide under it. Tap a step to scroll it into place.

## Install

```bash
npx shadcn@latest add https://hairlineui.com/r/scroll-story.json
```

Needs React 19, Tailwind CSS v4 and shadcn/ui theme variables (any style, light and dark). Icons from lucide-react.

- npm: none
- Hairline UI components it needs (the CLI installs them too): `number-roll`
- Docs and live preview: https://hairlineui.com/ui/scroll-story
- Registry item (source): https://hairlineui.com/r/scroll-story.json

## Usage

```tsx
import { ScrollStory } from "@/components/scroll-story";

<ScrollStory
  steps={[
    { title: "Pick a template", body: "Start from a page that already moves.", visual: <PickTemplate /> },
    { title: "Make it yours", body: "Your name, your colour.", visual: <MakeYours /> },
    { title: "Go live", body: "Push it and watch the first visitors arrive.", visual: <GoLive /> },
  ]}
/>
```

## Pictures that play when their step arrives

Each visual mounts when its step becomes active, so a timer or a count inside it starts right then; no scroll code needed in the picture itself.

```tsx
"use client";
import { useEffect, useState } from "react";
import { ScrollStory } from "@/components/scroll-story";
import { NumberRoll } from "@/components/number-roll";

function Visitors() {
  const [count, setCount] = useState(0);
  useEffect(() => {
    const t = setTimeout(() => setCount(128), 300); // rolls up as the step arrives
    return () => clearTimeout(t);
  }, []);
  return (
    <div className="w-[240px] p-4">
      <p className="text-[28px] font-semibold tabular-nums"><NumberRoll value={count} /></p>
      <p className="text-[12px] text-muted-foreground">visitors in the first hour</p>
    </div>
  );
}

export function HowItWorks() {
  return (
    <section className="mx-auto max-w-5xl px-6">
      <ScrollStory
        side="end"
        steps={[
          { title: "Connect your store", body: "Two clicks, no code.", visual: <Connect /> },
          { title: "Go live", body: "Watch the first visitors arrive.", visual: <Visitors /> },
        ]}
      />
    </section>
  );
}
```

## Props

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `steps` | `{ title: string; body?: ReactNode; visual: ReactNode }[]` | – | In order. Give visuals a set width (the card sizes itself to them); keep them under about 280px wide for phones. |
| `side` | `"start" \| "end"` | `"end"` | Which side the picture sits on when there’s room for two columns. |
| `onStepChange` | `(index: number) => void` | – | Runs when a different step becomes active. |

## States

- **reading line**: The middle of the view (wide), or 42% down what the sticky picture leaves (narrow). The last step whose top has crossed it is the active one.
- **rail**: A 1px track from the first dot to the last; the fill follows the reading line on every scroll frame (written straight to the DOM, no re-render). Dots fill and the active one grows to 1.15× with a slight overshoot.
- **steps**: Inactive steps at 35% opacity, the active one at full (400ms).
- **picture**: The card eases its width and height to the new picture (460ms); the old one blurs out and shrinks to 0.97 (240ms) while the new one rises 10px out of a 4px blur (420ms, 90ms in). The counter rolls.
- **narrow**: Under 520px of width the picture sticks to the top at 46% of the view with a hairline under it, and the steps scroll beneath.

## What it handles

Every product page has a “how it works”, and it is usually four numbered boxes nobody reads in order. Here the steps scroll past a picture that stays put and changes with them.

- **The step in the middle lights up**: Whichever step crosses the middle of the view is at full strength, and the others wait at about a third.
- **A rail that fills**: A hairline down the side fills with the scroll and each step’s dot fills as the line reaches it.
- **One picture that reshapes**: The card eases to the size of each step’s picture while the old one blurs out and the new one rises in.
- **Pictures that play when their step arrives**: Each picture mounts when its step becomes active, so a switch flipping or a count rolling plays right then.
- **A phone layout**: On narrow screens the picture sticks to the top and the steps scroll beneath it.
- **Tap a step to go there**: Titles are buttons that scroll their step into place, and all the words stay in the page for reading and search.

## Replaces

scroll-spy code, a sticky layout, a progress rail, picture swapping, a mobile layout

## Where it fits

- **How-it-works sections**: Explain a product in steps on a landing page.
- **Product tours**: A sticky picture that changes as each step comes into view.
- **Long-form explainers**: Case studies and feature write-ups with a picture for each part.
- **Onboarding and docs pages**: Walk through a setup in the order it should happen.

## Notes

- It finds the nearest scrolling box itself, so it works in the page or inside a scrolling panel; give the box a height.
- The active step has aria-current="step"; each title is a button that scrolls its step into place.
- Every step’s words stay in the page for reading and search; only the pictures come and go.
- Installs Number roll. With reduced motion the pictures swap in place and jumps don’t smooth-scroll.

License: MIT (https://hairlineui.com/license#components)
