Build a React 19 + TypeScript component called ScrollTextReveal: a centred paragraph that colours in word by word, from dim through a blue streak to white, as you scroll inside the component.
Constraints
- One .tsx file, "use client", imports from react only, default export, no required props. Optional props: text (default below; "\n" marks a line break) and streakWidth (default 0.15). All CSS in a template string rendered as <style href="play-scroll-text-reveal" precedence="play">, every class prefixed "str-".
- The root fills its parent (width/height 100%, background #0a0a0f, isolation isolate) and is itself the scroll container: overflow-y auto, overflow-x hidden, overscroll-behavior auto so the page continues once it ends, scrollbar hidden. Never read the window's scroll.
- Font: var(--font-google-sans-flex, "Google Sans Flex"), system-ui, sans-serif.
Layout
- Inside the root, a runway 300% of the root's height. Inside that, a sticky panel (top 0, height a third of the runway, so exactly the root's height): a flex column with padding 32px 32px 24px (24px sides when narrow). The paragraph is centred in the space above a scroll cue.
- Paragraph: weight 500, line-height 1.6, centred, colour #F0F0F5. Default text, one line each:
"The light reaching you tonight"
"left some of these stars"
"before anyone you know was born."
"A few of them are already gone."
"You're seeing them anyway."
- Make the root an inline-size container. At 520px and wider keep the line breaks and use font-size: min(2.75rem, calc((100cqi - 64px) / 17)), so the longest line (about 16em) never wraps. Below 520px hide the breaks, render a space in their place, and use clamp(1.125rem, 5.6cqi, 1.5rem) with text-wrap: pretty.
- Split the text on spaces only and wrap each word in a span, so a word either side of a line break colours as one.
Colour
- Progress p = scrollTop / (scrollHeight - clientHeight), clamped 0 to 1.
- Word i of n: local = (p - i/n × (1 - streak)) / streak, clamped 0 to 1.
- Five evenly spaced stops: dim rgb(55,55,72), blue-400 rgb(96,165,250), indigo-400 rgb(129,140,248), indigo-500 rgb(99,102,241), final rgb(240,240,245). Scale local by 4, pick the segment, lerp RGB and round. Words start dim in CSS so nothing flashes on mount.
- Don't drive colours through React state. Keep a smoothed progress in a variable; a requestAnimationFrame loop does smooth += (target - smooth) × 0.06, snaps when the gap is under 0.001, writes span.style.color only when the value changed, and stops when the gap is under 0.0005. Scroll and resize events wake it.
Scroll cue
- Under the paragraph: "Scroll" (12px, weight 500, #8A8AA3) over a 14px chevron that bobs 3px every 1.6s. Its opacity is 1 - p / 0.12, so it's gone almost as soon as scrolling starts.
Accessibility and behaviour
- The root has tabIndex 0, role="region" and an aria-label, so arrow keys, Page Up/Down and Space scroll it. On :focus-visible, draw a 2px indigo ring inset 12px with a 28px radius instead of the outline. Touch scrolling works natively.
- Stop the loop and pause the CSS animations off screen (IntersectionObserver) and in hidden tabs; remove every listener on unmount.
- prefers-reduced-motion: no smoothing, streak or bob. Each word is white once p > i/n and dim before that.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373"use client";
import { Fragment, useEffect, useRef } from "react";
const DEFAULT_TEXT = [
"The light reaching you tonight",
"left some of these stars",
"before anyone you know was born.",
"A few of them are already gone.",
"You're seeing them anyway.",
].join("\n");
type RGB = readonly [number, number, number];
const DIM: RGB = [55, 55, 72];
const STREAK: RGB[] = [
[96, 165, 250],
[129, 140, 248],
[99, 102, 241],
];
const FINAL: RGB = [240, 240, 245];
const STOPS: RGB[] = [DIM, ...STREAK, FINAL];
const RUNWAY = 3;
const SMOOTHING = 0.06;
const CUE_FADE = 0.12;
function rgbString(c: RGB): string {
return `rgb(${c[0]},${c[1]},${c[2]})`;
}
function lerpColor(a: RGB, b: RGB, t: number): RGB {
return [
Math.round(a[0] + (b[0] - a[0]) * t),
Math.round(a[1] + (b[1] - a[1]) * t),
Math.round(a[2] + (b[2] - a[2]) * t),
];
}
const DIM_COLOR = rgbString(DIM);
const FINAL_COLOR = rgbString(FINAL);
function wordColor(progress: number): string {
if (progress <= 0) return DIM_COLOR;
if (progress >= 1) return FINAL_COLOR;
const segments = STOPS.length - 1;
const scaled = progress * segments;
const idx = Math.min(Math.floor(scaled), segments - 1);
return rgbString(lerpColor(STOPS[idx], STOPS[idx + 1], scaled - idx));
}
const css = `
.str-root {
container: str / inline-size;
position: relative;
width: 100%;
height: 100%;
overflow-x: hidden;
overflow-y: auto;
overscroll-behavior: auto;
isolation: isolate;
background: #0a0a0f;
color: ${FINAL_COLOR};
font-family: var(--font-google-sans-flex, "Google Sans Flex"), system-ui, sans-serif;
font-size: 16px;
line-height: 1.5;
text-align: center;
scrollbar-width: none;
outline: none;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
}
.str-root::-webkit-scrollbar { display: none; }
.str-root *, .str-root *::before, .str-root *::after { box-sizing: border-box; }
.str-runway { position: relative; height: ${RUNWAY * 100}%; }
.str-panel {
position: sticky;
top: 0;
height: ${100 / RUNWAY}%;
display: flex;
flex-direction: column;
padding: 32px 24px 24px;
overflow: hidden;
}
/* The paragraph */
.str-body {
position: relative;
z-index: 1;
flex: 1 1 auto;
min-height: 0;
display: flex;
align-items: center;
justify-content: center;
}
.str-text {
margin: 0;
max-width: 100%;
font-size: clamp(1.125rem, 5.6cqi, 1.5rem);
font-weight: 500;
line-height: 1.6;
text-wrap: pretty;
color: ${FINAL_COLOR};
}
.str-word { color: ${DIM_COLOR}; }
.str-sp { display: none; }
/* Wide frames keep the authored line breaks, with the type sized so the longest line (about 16em in
Google Sans Flex) never wraps. Narrow frames drop the breaks and let the text flow. */
@container str (min-width: 520px) {
.str-panel { padding-left: 32px; padding-right: 32px; }
.str-text { font-size: min(2.75rem, calc((100cqi - 64px) / 17)); }
}
@container str (max-width: 519.98px) {
.str-br { display: none; }
.str-sp { display: inline; }
}
/* Scroll cue */
.str-cue {
position: relative;
z-index: 1;
flex: none;
align-self: center;
display: flex;
flex-direction: column;
align-items: center;
gap: 2px;
font-size: 12px;
font-weight: 500;
line-height: 16px;
letter-spacing: 0.02em;
color: #8a8aa3;
pointer-events: none;
user-select: none;
-webkit-user-select: none;
}
.str-cue-chev { width: 14px; height: 14px; }
/* Keyboard focus: a ring inset from the frame's edge */
.str-root:focus-visible .str-panel::after {
content: "";
position: absolute;
inset: 12px;
z-index: 2;
border: 2px solid rgba(129, 140, 248, 0.65);
border-radius: 28px;
pointer-events: none;
}
@media (prefers-reduced-motion: no-preference) {
.str-cue-chev { animation: str-cue-bob 1.6s ease-in-out infinite; }
}
/* Off screen or in a hidden tab: freeze every animation */
.str-root[data-paused], .str-root[data-paused] * { animation-play-state: paused; }
@keyframes str-cue-bob { 0%, 100% { transform: translateY(0); } 50% { transform: translateY(3px); } }
`;
type ScrollTextRevealProps = {
text?: string;
streakWidth?: number;
};
export default function ScrollTextReveal({
text = DEFAULT_TEXT,
streakWidth = 0.15,
}: ScrollTextRevealProps) {
const rootRef = useRef<HTMLDivElement>(null);
const cueRef = useRef<HTMLDivElement>(null);
const wordsRef = useRef<(HTMLSpanElement | null)[]>([]);
const words = text.split(" ");
const count = words.length;
useEffect(() => {
const root = rootRef.current;
if (!root) return;
const cue = cueRef.current;
const spans = wordsRef.current;
spans.length = count;
const painted: string[] = [];
const reducedMotion = window.matchMedia("(prefers-reduced-motion: reduce)");
let raf = 0;
let smooth = 0;
let onScreen = true;
let tabVisible = document.visibilityState === "visible";
const readProgress = () => {
const max = root.scrollHeight - root.clientHeight;
return max > 0 ? Math.min(1, Math.max(0, root.scrollTop / max)) : 0;
};
const paint = (progress: number, snap: boolean) => {
if (cue) cue.style.opacity = String(Math.max(0, 1 - progress / CUE_FADE));
for (let i = 0; i < count; i++) {
const span = spans[i];
if (!span) continue;
const color = snap
? progress > i / count
? FINAL_COLOR
: DIM_COLOR
: wordColor((progress - (i / count) * (1 - streakWidth)) / streakWidth);
if (painted[i] !== color) {
painted[i] = color;
span.style.color = color;
}
}
};
const tick = () => {
raf = 0;
const target = readProgress();
if (reducedMotion.matches) {
smooth = target;
paint(target, true);
return;
}
const diff = target - smooth;
smooth += diff * SMOOTHING;
if (Math.abs(diff) < 0.001) smooth = target;
paint(smooth, false);
if (Math.abs(diff) >= 0.0005 && onScreen && tabVisible) raf = requestAnimationFrame(tick);
};
const wake = () => {
if (!raf && onScreen && tabVisible) raf = requestAnimationFrame(tick);
};
const updateActivity = () => {
if (onScreen && tabVisible) {
delete root.dataset.paused;
wake();
} else {
root.dataset.paused = "";
cancelAnimationFrame(raf);
raf = 0;
}
};
const io = new IntersectionObserver(([entry]) => {
onScreen = entry.isIntersecting;
updateActivity();
});
const onVisibility = () => {
tabVisible = document.visibilityState === "visible";
updateActivity();
};
const ro = new ResizeObserver(wake);
io.observe(root);
ro.observe(root);
root.addEventListener("scroll", wake, { passive: true });
document.addEventListener("visibilitychange", onVisibility);
reducedMotion.addEventListener("change", wake);
wake();
return () => {
cancelAnimationFrame(raf);
io.disconnect();
ro.disconnect();
root.removeEventListener("scroll", wake);
document.removeEventListener("visibilitychange", onVisibility);
reducedMotion.removeEventListener("change", wake);
delete root.dataset.paused;
};
}, [count, streakWidth, text]);
return (
<div
ref={rootRef}
className="str-root"
tabIndex={0}
role="region"
aria-label="Scrolling text reveal. Scroll to read."
>
<style href="play-scroll-text-reveal" precedence="play">
{css}
</style>
<div className="str-runway">
<div className="str-panel">
<div className="str-body">
<p className="str-text">
{words.map((word, i) => (
<span
key={i}
className="str-word"
ref={(el) => {
wordsRef.current[i] = el;
}}
>
{word.split("\n").map((part, j) => (
<Fragment key={j}>
{}
{j > 0 && (
<>
<br className="str-br" />
<span className="str-sp"> </span>
</>
)}
{part}
</Fragment>
))}
{i < count - 1 ? " " : ""}
</span>
))}
</p>
</div>
<div ref={cueRef} className="str-cue" aria-hidden="true">
<span>Scroll</span>
<svg className="str-cue-chev" viewBox="0 0 16 16" fill="none" focusable="false">
<path
d="M4 6l4 4 4-4"
stroke="currentColor"
strokeWidth="1.5"
strokeLinecap="round"
strokeLinejoin="round"
/>
</svg>
</div>
</div>
</div>
</div>
);
}