A hook for scroll-driven motion without a scroll library. useScrollStage maps the scroll through a section to a progress from 0 to 1 and hands it to your render function, which writes transforms and opacity. Every stage on a page shares one frame loop that reads before it writes and skips what is off screen, and under reduced motion the final state shows at once.
On this page
- A pinned product story
- A self-drawing path
- A run replayed by scroll
- A camera pan across a floor
- Beats inside a preview pane
Examples
Inside a scroll container
The demo above scrolls inside its own frame. Pass the container as root so the progress is measured against it and the stage listens to its scroll, not the window's.
From a smooth-scroll library
The hook never changes how the page scrolls. If your app already runs a smooth-scroll library, pass its scroll event as subscribe: a function that takes a listener and returns the unsubscribe. Keep it stable (define it once, outside the component or in a memo).
// Defined once, next to where the library starts.
const onSmoothScroll = (fn: () => void) => {
smooth.on("scroll", fn);
return () => smooth.off("scroll", fn);
};
useScrollStage(track, render, { subscribe: onSmoothScroll });Pass mode
mode: "pass" measures an ordinary element from the moment its top enters at the bottom of the view (0) to the moment its bottom leaves at the top (1). Use it for a path or a figure that changes as it goes by, with nothing pinned.
Installation
npx knowns-ui@latest add scroll-stagePaste this into your coding agent (Claude Code, Cursor, Codex). It checks your project, sets up the registry, runs the install and verifies it.
Install Scroll stage from Knowns UI into this project. Work step by step, show me each change, and ask before you edit files outside this project.
Context: Knowns UI (https://ui.knowns.dev) is a shadcn registry. Items land in the project as source files. Docs: https://ui.knowns.dev/docs/scroll-stage
1. Check the project first. Stop and tell me if any of these is missing:
- React 19 and Tailwind CSS v4.
- A components.json at the project root. If there is none, ask me before running `npx knowns-ui@latest init --no-key` (it runs shadcn init when needed, then adds the registries).
2. Run:
```bash
npx knowns-ui@latest add scroll-stage
```
It writes these files:
- lib/knowns/run.ts
- lib/knowns/json.tsx
- lib/knowns/status.ts
- lib/knowns/tone.ts
- lib/knowns/types.ts
- lib/knowns/use-abortable.ts
- lib/knowns/utils.ts
- lib/knowns/motion.tsx
- lib/knowns/motion-utils.ts
- lib/knowns/scroll-stage.ts
- lib/knowns/scroll-stage-utils.ts
and installs these npm packages: clsx@^2.1.1, tailwind-merge@^3.7.0.
3. While it runs:
- It also adds a few @keyframes (all named kn-*) to the global CSS file (globals.css or the file components.json points at). Let it: they only add animations.
- If it asks to overwrite files under lib/knowns/: these are shared by every Knowns UI item. List them, say whether I changed them (check git), and ask me before you answer.
- If it asks to overwrite any other file, show me the diff and ask me.
4. Verify:
- Every file in the list above exists.
- The project type-checks (for example `npx tsc --noEmit`) and the dev build has no new errors.
5. Use it: follow the examples and props on https://ui.knowns.dev/docs/scroll-stage, and ask me if anything about my data is unclear.
If the Knowns UI MCP server is connected (https://ui.knowns.dev/docs/ai-and-mcp), use get_component({ name: "scroll-stage" }) or search_docs for details.
Finish with a short summary: the files you added or changed, the packages you installed, and anything I still need to do.Props
- refrequired
RefObject<HTMLElement>- The track (pin mode: a tall element with a sticky child) or the element (pass mode).
- renderrequired
(progress, { reduced, el }) => void- Writes the stage for a progress from 0 to 1. Write transform, opacity or a CSS variable; read nothing from the layout here.
- options.root
RefObject<HTMLElement>- Default
window - A scroll container to measure against and listen to.
- options.subscribe
(onScroll) => () => void- An outside scroll source. Must be a stable function.
- options.mode
"pin"|"pass"- Default
"pin" - How progress is measured.
- options.disabled
boolean- Default
false - Pause the stage.
- useScrollStep(ref, steps, options)
number- Default
0 - The step index from a count or a list of thresholds; re-renders only when it changes. The last step under reduced motion.
- useReducedScroll()
boolean- Default
false - True after mount when reduced motion is on: switch a pinned layout to a stacked one.
- beat(p, from, to, ease?)
number- 0 before the window, 1 after it, eased between (SCROLL_EASE.out by default).
- SCROLL_EASE
{ out, inOut, linear }- The motion tokens as easing functions. bezierEase(x1, y1, x2, y2) builds another from a cubic-bezier.
- stageProgress, stepIndex, mix
functions- The pure maths, exported for tests and custom stages.
| Prop | Type | Default | Description |
|---|---|---|---|
| refrequired | RefObject<HTMLElement> | required | The track (pin mode: a tall element with a sticky child) or the element (pass mode). |
| renderrequired | (progress, { reduced, el }) => void | required | Writes the stage for a progress from 0 to 1. Write transform, opacity or a CSS variable; read nothing from the layout here. |
| options.root | RefObject<HTMLElement> | window | A scroll container to measure against and listen to. |
| options.subscribe | (onScroll) => () => void | - | An outside scroll source. Must be a stable function. |
| options.mode | "pin" | "pass" | "pin" | How progress is measured. |
| options.disabled | boolean | false | Pause the stage. |
| useScrollStep(ref, steps, options) | number | 0 | The step index from a count or a list of thresholds; re-renders only when it changes. The last step under reduced motion. |
| useReducedScroll() | boolean | false | True after mount when reduced motion is on: switch a pinned layout to a stacked one. |
| beat(p, from, to, ease?) | number | - | 0 before the window, 1 after it, eased between (SCROLL_EASE.out by default). |
| SCROLL_EASE | { out, inOut, linear } | - | The motion tokens as easing functions. bezierEase(x1, y1, x2, y2) builds another from a cubic-bezier. |
| stageProgress, stepIndex, mix | functions | - | The pure maths, exported for tests and custom stages. |
Accessibility
- Keep the content in DOM order and visible without JavaScript: hide a beat only from the render function, never in the server markup.
- Under reduced motion the hook renders the final state once. Unpin the stage and stack its beats with useReducedScroll, so nothing depends on scrolling.
- A scroll container in the page needs a name and tabIndex 0, so keyboard users can scroll it.
- Numbers written from the render function are aria-hidden; announce a step change with a polite live region at most.
What it handles
- 01Turns the scroll through a section into one progress number from 0 to 1, for a tall track with a pinned child or for an element passing through the view.
- 02Runs one animation frame loop per scroll source for every stage on it: all rects are read first, then every stage whose progress moved is rendered.
- 03Skips stages off screen, and gives a stage one last frame as it leaves so a fast fling still lands it on its first or last state.
- 04Writes beats as windows of progress, eased on the motion tokens with bezierEase, never as time.
- 05Steps discrete changes with useScrollStep, which re-renders only when the step changes.
- 06Works inside a scroll container (a docs frame, a preview pane) and from an outside scroll source, without changing how the page scrolls.
States
- First render
- The stage paints at its current progress, with no entrance
- none
- Scroll
- render(progress) writes transforms and opacity in the next frame
- one frame, no easing in time
- Off screen
- Not measured and not rendered
- none
- Leaving the view
- One last frame at 0 or 1
- one frame
- Reduced motion
- render(1) once with reduced: true; no listener. Unpin and stack the beats in your layout
- 0ms
| State | What moves | Timing |
|---|---|---|
| First render | The stage paints at its current progress, with no entrance | none |
| Scroll | render(progress) writes transforms and opacity in the next frame | one frame, no easing in time |
| Off screen | Not measured and not rendered | none |
| Leaving the view | One last frame at 0 or 1 | one frame |
| Reduced motion | render(1) once with reduced: true; no listener. Unpin and stack the beats in your layout | 0ms |
Works with
When to use
When to use
- A pinned section that tells a product story in beats as the visitor scrolls: a release rolling out, a run replayed, a camera moving across a floor.
- A figure that changes as it passes through the view, such as a path drawing itself or a chart growing, inside the page or a scroll container.
When not to use
- A section that only needs to appear once as it comes into view: a scroll reveal is enough.
- A pinned title with a product window rising under it, ready made: use Scroll-over Hero.
Using with AI
Use Copy page for AI above to give your assistant this page, Copy prompt under the install command to have it installed and wired to your data, or connect the Knowns UI MCP server and just ask:
- Add the Knowns UI Scroll Stage to my app and wire it to my own data.
- I need a pinned section that tells a product story in beats as the visitor scrolls: a release rolling out, a run replayed, a camera moving across a floor. Set that up with the Knowns UI Scroll Stage.
- Which Scroll Stage props do I need for a minimal setup? Show me a short example.
With the server connected, your assistant calls get_component({ name: "scroll-stage" }) and search_docs({ query: "scroll stage" }).