Scroll Reveal Text
A scroll-driven text reveal for React and Tailwind CSS: a paragraph whose words light up one by one as it scrolls through the viewport. Each word's opacity is a Motion value tied to scroll progress, so it never re-renders React, and the text stays real, selectable and SEO-friendly.
- Scroll-linked
- SEO-friendly real text
- No re-renders
- Scroll containers
- prefers-reduced-motion
"use client";
import * as React from "react";
import { ScrollRevealText } from "@/components/dusk/text/scroll-reveal-text";
export default function ScrollRevealTextDemo() {
const scroller = React.useRef<HTMLDivElement>(null);
return (
<div
ref={scroller}
tabIndex={0}
aria-label="Scrollable example"
className="relative h-80 w-full max-w-xl overflow-y-auto rounded-xl border border-white/10 bg-black/30 px-6 outline-none focus-visible:ring-2 focus-visible:ring-ring/50 [scrollbar-width:thin]"
>
<p className="sticky top-0 bg-linear-to-b from-black/60 to-transparent pb-6 pt-4 text-center text-xs text-white/55">
Scroll inside ↓
</p>
<div className="h-40" />
<ScrollRevealText container={scroller} className="text-2xl font-semibold leading-snug tracking-tight">
Good interfaces don't shout. They wait until you arrive, then reveal exactly what you need,
one idea at a time, and get out of the way the moment you understand.
</ScrollRevealText>
<div className="h-56" />
</div>
);
}Installation
$ npx shadcn@latest add https://duskui.com/r/scroll-reveal-text.json1. Install the dependencies.
npm install motion2. Copy the source into
components/dusk/text/scroll-reveal-text.tsx.scroll-reveal-text.tsx "use client"; import * as React from "react"; import { motion, useMotionValue, useReducedMotion, useScroll, useTransform, type MotionValue, type UseScrollOptions, } from "motion/react"; import { cn } from "@/lib/utils"; type ScrollRevealTag = "p" | "div" | "blockquote" | "span" | "h1" | "h2" | "h3" | "h4" | "h5" | "h6"; export interface ScrollRevealTextProps extends Omit<React.HTMLAttributes<HTMLElement>, "children"> { /** The text to reveal. Plain text only: it is split into words. */ children: string; /** Element to render. */ as?: ScrollRevealTag; /** Opacity of words that haven't been reached yet (0–1). */ baseOpacity?: number; /** * When the reveal starts and ends, as Motion `useScroll` offsets * (`"<element edge> <viewport edge>"`). Default: starts when the top of the * text is 85% down the viewport, ends when its bottom reaches the middle. */ offset?: UseScrollOptions["offset"]; /** The scrolling element, if the text lives in a scroll container instead of the page. */ container?: React.RefObject<HTMLElement | null>; /** Classes for each word, e.g. a highlight color. */ wordClassName?: string; } const DEFAULT_OFFSET: UseScrollOptions["offset"] = ["start 0.85", "end 0.5"]; /** * A paragraph whose words light up one by one as it scrolls through the * viewport (or a scroll container). * * Each word's opacity is a Motion value derived from scroll progress, so * scrolling never re-renders React. The words stay one real, selectable text * node sequence (no duplicated copy). The server, no-JS visitors and * reduced-motion users get the fully revealed paragraph. */ export function ScrollRevealText({ children, as = "p", baseOpacity = 0.18, offset = DEFAULT_OFFSET, container, wordClassName, className, ...props }: ScrollRevealTextProps) { const Comp = as as React.ElementType; const ref = React.useRef<HTMLElement>(null); const reduceMotion = useReducedMotion(); const { scrollYProgress } = useScroll({ target: ref, container, offset }); // 0 on the server and during hydration (everything fully visible), 1 once the reveal is live. const active = useMotionValue(0); React.useLayoutEffect(() => { active.set(reduceMotion ? 0 : 1); }, [reduceMotion, active]); // Split into words, keeping the original whitespace between them as plain text. const { tokens, wordCount } = React.useMemo(() => { let words = 0; const list = children .split(/(\s+)/) .filter(Boolean) .map((text) => (/^\s+$/.test(text) ? { text, word: -1 } : { text, word: words++ })); return { tokens: list, wordCount: words }; }, [children]); return ( <Comp ref={ref} data-slot="scroll-reveal-text" className={cn("relative", className)} {...props}> {tokens.map(({ text, word }, i) => word < 0 ? ( text ) : ( <Word key={i} progress={scrollYProgress} active={active} start={word / wordCount} end={(word + 1) / wordCount} base={baseOpacity} className={wordClassName} > {text} </Word> ) )} </Comp> ); } function Word({ progress, active, start, end, base, className, children, }: { progress: MotionValue<number>; active: MotionValue<number>; start: number; end: number; base: number; className?: string; children: string; }) { const opacity = useTransform([progress, active], ([p, on]: number[]) => { if (!on) return 1; const t = Math.min(1, Math.max(0, (p - start) / (end - start))); return base + (1 - base) * t; }); return ( <motion.span style={{ opacity }} className={className}> {children} </motion.span> ); }
Usage
import { ScrollRevealText } from "@/components/dusk/text/scroll-reveal-text"
export function Manifesto() {
return (
<ScrollRevealText className="text-3xl font-semibold">
We build tools for people who do their best work after dark.
</ScrollRevealText>
)
}Examples
Quote with custom timing
offset takes Motion useScroll offsets to control where the reveal starts and ends. baseOpacity sets how faint unread words are and wordClassName styles them. Inside a scroll container, pass its ref as container.
"use client";
import * as React from "react";
import { ScrollRevealText } from "@/components/dusk/text/scroll-reveal-text";
export default function ScrollRevealTextQuoteDemo() {
const scroller = React.useRef<HTMLDivElement>(null);
return (
<div
ref={scroller}
tabIndex={0}
aria-label="Scrollable example"
className="relative h-80 w-full max-w-xl overflow-y-auto rounded-xl border border-white/10 bg-black/30 px-8 outline-none focus-visible:ring-2 focus-visible:ring-ring/50 [scrollbar-width:thin]"
>
<div className="h-48" />
<figure>
<ScrollRevealText
as="blockquote"
container={scroller}
baseOpacity={0.08}
offset={["start 0.9", "end 0.6"]}
className="font-serif text-3xl leading-tight italic"
>
“We shipped the redesign on a Friday night. By Monday, nobody remembered the old one.”
</ScrollRevealText>
<figcaption className="mt-4 text-sm text-white/50">— Someone who deploys on Fridays</figcaption>
</figure>
<div className="h-56" />
</div>
);
}API Reference
Also accepts any HTML attribute for the rendered element.
| Prop | Type | Default | Description |
|---|---|---|---|
| children | string | — | The text to reveal. Plain text only; it is split into words. |
| as | "p" | "div" | "blockquote" | "span" | "h1" … "h6" | "p" | Element to render. |
| baseOpacity | number | 0.18 | Opacity of words not reached yet (0–1). |
| offset | UseScrollOptions["offset"] | ["start 0.85", "end 0.5"] | When the reveal starts and ends: starts when the top is 85% down the viewport, ends when the bottom reaches the middle. |
| container | RefObject<HTMLElement | null> | — | The scrolling element, when the text is inside a scroll container instead of the page. |
| wordClassName | string | — | Classes for each word. |
Accessibility
- The words are the real text, split into inline spans with the original spacing, so there is no duplicated or hidden copy and selection, translation and search work normally.
- The server, no-JS visitors and the first client render show the paragraph fully revealed; the reveal is armed just before the first paint.
- Only opacity changes, and never below baseOpacity, so text is never fully invisible.
- Scroll progress drives Motion values directly, with no React state per frame.
- With prefers-reduced-motion every word is fully visible and scrolling changes nothing.