@brightlocal/ui-components 1.2.4 → 1.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,732 @@
1
+ "use client";
2
+
3
+ import * as React from "react";
4
+ import {
5
+ useCallback,
6
+ useEffect,
7
+ useId,
8
+ useMemo,
9
+ useRef,
10
+ useState,
11
+ } from "react";
12
+ import { animate } from "framer-motion";
13
+
14
+ import { cn } from "@brightlocal/ui-components/lib/utils";
15
+ import { usePrefersReducedMotion } from "../hooks/use-prefers-reduced-motion";
16
+
17
+ // ---------------------------------------------------------------------------
18
+ // Shape paths (500×500 viewBox)
19
+ // ---------------------------------------------------------------------------
20
+
21
+ const CIRCLE_PATH =
22
+ "M250 500C388.071 500 500 388.071 500 250C500 111.929 388.071 0 250 0C111.929 0 0 111.929 0 250C0 388.071 111.929 500 250 500Z";
23
+
24
+ const STAR_PATH =
25
+ "M492.425 259.355C332.265 293.145 293.145 332.265 259.355 492.425C257.225 502.525 242.775 502.525 240.645 492.425C206.855 332.265 167.735 293.145 7.575 259.355C-2.525 257.225 -2.525 242.775 7.575 240.645C167.735 206.855 206.855 167.735 240.645 7.575C242.775 -2.525 257.225 -2.525 259.355 7.575C293.145 167.735 332.265 206.855 492.425 240.645C502.525 242.775 502.525 257.225 492.425 259.355Z";
26
+
27
+ const STAR_BURST =
28
+ "M258.366 5.58795L296.379 97.3854C298.509 102.526 304.729 104.546 309.47 101.636L394.177 49.7215C401.057 45.5112 409.598 51.7117 407.708 59.5523L384.506 156.16C383.206 161.571 387.046 166.861 392.596 167.301L491.645 175.092C499.685 175.722 502.945 185.763 496.815 191.003L421.259 255.528C417.029 259.138 417.029 265.679 421.259 269.299L496.815 333.824C502.945 339.065 499.685 349.106 491.645 349.736L392.596 357.526C387.046 357.966 383.206 363.257 384.506 368.667L407.708 465.275C409.588 473.116 401.047 479.326 394.177 475.106L309.47 423.192C304.729 420.282 298.509 422.302 296.379 427.442L258.366 519.24C255.275 526.69 244.725 526.69 241.634 519.24L203.621 427.442C201.491 422.302 195.271 420.282 190.53 423.192L105.823 475.106C98.9428 479.316 90.4021 473.116 92.2922 465.275L115.494 368.667C116.794 363.257 112.954 357.966 107.403 357.526L8.35543 349.736C0.314784 349.106 -2.94548 339.065 3.18502 333.824L78.7411 269.299C82.9715 265.689 82.9715 259.148 78.7411 255.528L3.18502 191.003C-2.94548 185.763 0.314784 175.722 8.35543 175.092L107.403 167.301C112.954 166.861 116.794 161.571 115.494 156.16L92.2922 59.5523C90.4121 51.7117 98.9528 45.5012 105.823 49.7215L190.53 101.636C195.271 104.546 201.491 102.526 203.621 97.3854L241.634 5.58795C244.725 -1.86265 255.275 -1.86265 258.366 5.58795Z";
29
+
30
+ /** Clamp value to [0, 1] — avoids repeated Math.min/max in hot loops */
31
+ const clamp01 = (v: number) => (v < 0 ? 0 : v > 1 ? 1 : v);
32
+
33
+ /** Default 6-shape morph sequence */
34
+ const DEFAULT_SHAPES: string[] = [
35
+ CIRCLE_PATH,
36
+ STAR_PATH,
37
+ STAR_BURST,
38
+ CIRCLE_PATH,
39
+ STAR_BURST,
40
+ STAR_PATH,
41
+ ];
42
+
43
+ /** Number of pre-sampled frames for flubber morphs */
44
+ const MORPH_SAMPLES = 30;
45
+
46
+ /** Fewer samples on low-end devices to reduce pre-computation + memory */
47
+ const MORPH_SAMPLES_LOW = 15;
48
+
49
+ /**
50
+ * Detect low-performance devices via hardware concurrency.
51
+ * Falls back to false on SSR or when the API is unavailable.
52
+ */
53
+ const isLowPerfDevice =
54
+ typeof navigator !== "undefined" &&
55
+ typeof navigator.hardwareConcurrency === "number" &&
56
+ navigator.hardwareConcurrency <= 4;
57
+
58
+ // ---------------------------------------------------------------------------
59
+ // rAF-batched DOM write scheduler
60
+ // ---------------------------------------------------------------------------
61
+
62
+ interface FrameState {
63
+ ballX: number;
64
+ clipRight: number;
65
+ rotate: number;
66
+ pathD: string;
67
+ }
68
+
69
+ function createFrameScheduler(
70
+ ball: HTMLDivElement,
71
+ group: SVGGElement,
72
+ path: SVGPathElement
73
+ ) {
74
+ let pending = false;
75
+ let activeItemEl: HTMLSpanElement | null = null;
76
+ const state: FrameState = { ballX: 0, clipRight: 100, rotate: 0, pathD: "" };
77
+
78
+ function flush() {
79
+ pending = false;
80
+ ball.style.transform = `translateX(${state.ballX}px)`;
81
+ if (activeItemEl) {
82
+ activeItemEl.style.clipPath = `inset(0 ${state.clipRight}% 0 0)`;
83
+ }
84
+ group.style.transform = `rotate(${state.rotate}deg)`;
85
+ path.setAttribute("d", state.pathD);
86
+ }
87
+
88
+ function schedule(
89
+ ballX: number,
90
+ clipRight: number,
91
+ rotate: number,
92
+ pathD: string
93
+ ) {
94
+ state.ballX = ballX;
95
+ state.clipRight = clipRight;
96
+ state.rotate = rotate;
97
+ state.pathD = pathD;
98
+ if (!pending) {
99
+ pending = true;
100
+ requestAnimationFrame(flush);
101
+ }
102
+ }
103
+
104
+ /** Update which item element receives clip-path writes */
105
+ schedule.setItem = (el: HTMLSpanElement) => {
106
+ activeItemEl = el;
107
+ };
108
+
109
+ return schedule;
110
+ }
111
+
112
+ // ---------------------------------------------------------------------------
113
+ // rAF-based delay (replaces setTimeout for frame-aligned timing)
114
+ // ---------------------------------------------------------------------------
115
+
116
+ function rafDelay(ms: number, signal: { cancelled: boolean }): Promise<void> {
117
+ return new Promise((resolve) => {
118
+ const start = performance.now();
119
+ function check() {
120
+ if (signal.cancelled || performance.now() - start >= ms) {
121
+ resolve();
122
+ } else {
123
+ requestAnimationFrame(check);
124
+ }
125
+ }
126
+ requestAnimationFrame(check);
127
+ });
128
+ }
129
+
130
+ // ---------------------------------------------------------------------------
131
+ // Props
132
+ // ---------------------------------------------------------------------------
133
+
134
+ /**
135
+ * Props for the TextRevealMotion component.
136
+ * Extends native div props for full HTML attribute support.
137
+ *
138
+ * Each child represents one item in the reveal cycle. The rolling ball
139
+ * animates across, revealing each child via clip-path, then rolls back
140
+ * before moving to the next.
141
+ *
142
+ * @example
143
+ * ```tsx
144
+ * <TextRevealMotion dataHook="loader">
145
+ * <TypographyP dataHook="w1">Analyzing</TypographyP>
146
+ * <TypographyP dataHook="w2">Processing</TypographyP>
147
+ * </TextRevealMotion>
148
+ * ```
149
+ */
150
+ export interface TextRevealMotionProps extends Omit<
151
+ React.ComponentPropsWithoutRef<"div">,
152
+ "children"
153
+ > {
154
+ /**
155
+ * Required data-hook attribute for automated testing.
156
+ * @example "text-reveal-loader"
157
+ */
158
+ dataHook: string;
159
+ /**
160
+ * How long (ms) each item stays fully visible.
161
+ * The ball keeps morphing and rotating during the hold.
162
+ * @default 1300
163
+ */
164
+ holdTime?: number;
165
+ /**
166
+ * Show shimmer sweep after each reveal.
167
+ * @default true
168
+ */
169
+ shimmer?: boolean;
170
+ /**
171
+ * Gradient start color. Accepts any CSS color value.
172
+ * @default "var(--loading-gradient-from)"
173
+ */
174
+ gradientFrom?: string;
175
+ /**
176
+ * Gradient end color. Accepts any CSS color value.
177
+ * @default "var(--loading-gradient-to)"
178
+ */
179
+ gradientTo?: string;
180
+ /**
181
+ * Tracking element identifier for analytics.
182
+ */
183
+ trackingEl?: string;
184
+ /**
185
+ * Tracking label for analytics context.
186
+ */
187
+ trackingLabel?: string;
188
+ /**
189
+ * Each child is one item in the reveal cycle.
190
+ * Pass any Typography component as a child.
191
+ * @example
192
+ * ```tsx
193
+ * <TypographyP dataHook="w1">Analyzing</TypographyP>
194
+ * <TypographyP dataHook="w2">Processing</TypographyP>
195
+ * ```
196
+ */
197
+ children: React.ReactNode;
198
+ }
199
+
200
+ // ---------------------------------------------------------------------------
201
+ // Component
202
+ // ---------------------------------------------------------------------------
203
+
204
+ /**
205
+ * Animated text reveal component with a rolling shape-morphing ball.
206
+ *
207
+ * The ball rolls across each child, revealing it via clip-path, holds,
208
+ * then rolls back and cycles to the next child. Supports any Typography
209
+ * component as children.
210
+ */
211
+ const TextRevealMotion = React.forwardRef<
212
+ HTMLDivElement,
213
+ TextRevealMotionProps
214
+ >(
215
+ (
216
+ {
217
+ className,
218
+ dataHook,
219
+ holdTime = 1300,
220
+ shimmer = true,
221
+ gradientFrom = "var(--loading-gradient-from)",
222
+ gradientTo = "var(--loading-gradient-to)",
223
+ trackingEl,
224
+ trackingLabel,
225
+ children,
226
+ ...props
227
+ },
228
+ ref
229
+ ) => {
230
+ // Internal animation defaults matching the Loading story tuning
231
+ const shapes = DEFAULT_SHAPES;
232
+ const rollSpeed = 0.8;
233
+ const stiffness = 180;
234
+ const damping = 24;
235
+ const mass = 1;
236
+
237
+ const reducedMotion = usePrefersReducedMotion();
238
+ const rawId = useId();
239
+ const gradientId = useMemo(
240
+ () => `text-reveal-gradient-${rawId.replace(/:/g, "")}`,
241
+ [rawId]
242
+ );
243
+
244
+ // Refs for imperative animation
245
+ const ballRef = useRef<HTMLDivElement>(null);
246
+ const shimmerRef = useRef<HTMLDivElement>(null);
247
+ const pathRef = useRef<SVGPathElement>(null);
248
+ const groupRef = useRef<SVGGElement>(null);
249
+ const gradientElRef = useRef<SVGLinearGradientElement>(null);
250
+ const containerRef = useRef<HTMLDivElement>(null);
251
+ const textWrapRef = useRef<HTMLSpanElement>(null);
252
+ const itemRefs = useRef<(HTMLSpanElement | null)[]>([]);
253
+
254
+ const items = React.Children.toArray(children);
255
+ const [measurements, setMeasurements] = useState<{
256
+ widths: number[];
257
+ ballSize: number;
258
+ }>({ widths: [], ballSize: 0 });
259
+ const { widths, ballSize } = measurements;
260
+
261
+ // -----------------------------------------------------------------------
262
+ // Collect refs
263
+ // -----------------------------------------------------------------------
264
+ const itemRefCallback = useCallback(
265
+ (index: number) => (el: HTMLSpanElement | null) => {
266
+ itemRefs.current[index] = el;
267
+ },
268
+ []
269
+ );
270
+
271
+ // Measure children widths and font-size after refs are attached.
272
+ // Waits for document.fonts.ready so measurements use the real web
273
+ // font metrics rather than fallback system font widths.
274
+ useEffect(() => {
275
+ if (itemRefs.current.length === 0 || !itemRefs.current[0]) return;
276
+ let cancelled = false;
277
+
278
+ function measure() {
279
+ if (cancelled) return;
280
+ const w: number[] = [];
281
+ let fontSize = 32;
282
+ for (let i = 0; i < items.length; i++) {
283
+ const node = itemRefs.current[i];
284
+ if (!node) continue;
285
+ w[i] = node.scrollWidth;
286
+ if (i === 0) {
287
+ const textEl = node.firstElementChild ?? node;
288
+ const fs = parseFloat(getComputedStyle(textEl).fontSize) || 16;
289
+ fontSize = Math.round(fs * 1.5);
290
+ }
291
+ }
292
+ setMeasurements({ widths: w, ballSize: fontSize });
293
+ }
294
+
295
+ document.fonts.ready.then(() => {
296
+ if (cancelled) return;
297
+ requestAnimationFrame(measure);
298
+ });
299
+
300
+ return () => {
301
+ cancelled = true;
302
+ };
303
+ }, [items.length]);
304
+
305
+ // -----------------------------------------------------------------------
306
+ // Gradient wander
307
+ // -----------------------------------------------------------------------
308
+ useEffect(() => {
309
+ if (reducedMotion) return;
310
+ const grad = gradientElRef.current;
311
+ if (!grad) return;
312
+ const gradEl: SVGLinearGradientElement = grad;
313
+ let raf: number;
314
+ let start: number | null = null;
315
+ const sA = 0.12 + Math.random() * 0.06;
316
+ const sB = 0.08 + Math.random() * 0.04;
317
+ const oA = Math.random() * Math.PI * 2;
318
+ const oB = Math.random() * Math.PI * 2;
319
+ let prevAngle = -Infinity;
320
+ function tick(now: number) {
321
+ if (!start) start = now;
322
+ const t = (now - start) / 1000;
323
+ const angle =
324
+ Math.sin(t * sA + oA) * Math.PI + Math.sin(t * sB + oB) * 0.5;
325
+ if (Math.abs(angle - prevAngle) > 0.01) {
326
+ prevAngle = angle;
327
+ const cos = Math.cos(angle);
328
+ const sin = Math.sin(angle);
329
+ gradEl.setAttribute("x1", `${50 + 45 * cos}%`);
330
+ gradEl.setAttribute("y1", `${50 + 45 * sin}%`);
331
+ gradEl.setAttribute("x2", `${50 - 45 * cos}%`);
332
+ gradEl.setAttribute("y2", `${50 - 45 * sin}%`);
333
+ }
334
+ raf = requestAnimationFrame(tick);
335
+ }
336
+ raf = requestAnimationFrame(tick);
337
+ return () => cancelAnimationFrame(raf);
338
+ }, [reducedMotion]);
339
+
340
+ // -----------------------------------------------------------------------
341
+ // Shimmer helper
342
+ // -----------------------------------------------------------------------
343
+ function runShimmerEffect(el: HTMLDivElement) {
344
+ el.style.opacity = "1";
345
+ animate(0, 1, {
346
+ duration: 1.2,
347
+ ease: [0.25, 1, 0.5, 1],
348
+ onUpdate: (p: number) => {
349
+ el.style.transform = `translateX(${-100 + p * 200}%)`;
350
+ },
351
+ onComplete: () => {
352
+ el.style.opacity = "0";
353
+ },
354
+ });
355
+ }
356
+
357
+ // -----------------------------------------------------------------------
358
+ // Main animation loop
359
+ // -----------------------------------------------------------------------
360
+ useEffect(() => {
361
+ if (
362
+ !pathRef.current ||
363
+ !groupRef.current ||
364
+ !ballRef.current ||
365
+ !shimmerRef.current ||
366
+ !containerRef.current ||
367
+ !textWrapRef.current ||
368
+ widths.length === 0 ||
369
+ ballSize === 0
370
+ )
371
+ return;
372
+
373
+ const path: SVGPathElement = pathRef.current;
374
+ const group: SVGGElement = groupRef.current;
375
+ const ball: HTMLDivElement = ballRef.current;
376
+ const shimmerEl: HTMLDivElement = shimmerRef.current;
377
+ const container: HTMLDivElement = containerRef.current;
378
+ const textWrap: HTMLSpanElement = textWrapRef.current;
379
+
380
+ function resizeToItem(wordW: number) {
381
+ const w = `${wordW}px`;
382
+ container.style.width = w;
383
+ textWrap.style.width = w;
384
+ }
385
+
386
+ if (reducedMotion) {
387
+ path.setAttribute("d", shapes[0]!);
388
+ group.style.opacity = "1";
389
+ const firstItem = itemRefs.current[0];
390
+ if (firstItem) {
391
+ firstItem.style.opacity = "1";
392
+ firstItem.style.clipPath = "none";
393
+ }
394
+ resizeToItem(widths[0] ?? 0);
395
+ const gap0 = ballSize * 0.25;
396
+ const endX0 = (widths[0] ?? 0) + gap0 + ballSize / 2;
397
+ ball.style.transform = `translateX(${endX0}px)`;
398
+ return;
399
+ }
400
+
401
+ const signal = { cancelled: false };
402
+ const gap = ballSize * 0.25;
403
+
404
+ async function loop() {
405
+ // Lazy-load flubber so it doesn't block first paint
406
+ const { interpolate } = await import("flubber");
407
+ if (signal.cancelled) return;
408
+
409
+ // Pre-sample all morph transitions into lookup tables.
410
+ // Eliminates per-frame flubber string generation (~50% CPU savings).
411
+ // On low-end devices, halve the sample count to reduce memory + init cost.
412
+ const sampleCount = isLowPerfDevice ? MORPH_SAMPLES_LOW : MORPH_SAMPLES;
413
+ const sampledFrames: string[][] = [];
414
+ for (let i = 0; i < shapes.length; i++) {
415
+ const interp = interpolate(
416
+ shapes[i]!,
417
+ shapes[(i + 1) % shapes.length]!
418
+ );
419
+ const frames: string[] = [];
420
+ for (let j = 0; j <= sampleCount; j++) {
421
+ frames.push(interp(j / sampleCount));
422
+ }
423
+ sampledFrames.push(frames);
424
+ }
425
+
426
+ // Create scheduler once — reuse across all phases and items
427
+ const schedule = createFrameScheduler(ball, group, path);
428
+
429
+ group.style.opacity = "1";
430
+ let wordIndex = 0;
431
+ let shapeStep = 0;
432
+ let totalRotation = 0;
433
+
434
+ while (!signal.cancelled) {
435
+ const idx = wordIndex % items.length;
436
+ const wordW = widths[idx] ?? 0;
437
+
438
+ resizeToItem(wordW);
439
+
440
+ const startX = -(gap + ballSize / 2);
441
+ const endX = wordW + gap + ballSize / 2;
442
+ const travel = endX - startX;
443
+
444
+ const itemEl = itemRefs.current[idx];
445
+ if (!itemEl) break;
446
+
447
+ // Point scheduler at the active item for clip-path writes
448
+ schedule.setItem(itemEl);
449
+
450
+ // Show this item (clipped), hide all others via opacity (no layout cost)
451
+ for (let i = 0; i < items.length; i++) {
452
+ const el = itemRefs.current[i];
453
+ if (!el) continue;
454
+ el.style.opacity = i === idx ? "1" : "0";
455
+ el.style.clipPath = "inset(0 100% 0 0)";
456
+ }
457
+
458
+ // Phase 1: Roll right → reveal
459
+ const morphFrames1 = sampledFrames[shapeStep % shapes.length]!;
460
+ const rollDeg = 360;
461
+ const startRot1 = totalRotation;
462
+
463
+ await new Promise<void>((resolve) => {
464
+ if (signal.cancelled) {
465
+ resolve();
466
+ return;
467
+ }
468
+ const controls = animate(startX, endX, {
469
+ type: "spring",
470
+ stiffness: stiffness * rollSpeed,
471
+ damping,
472
+ mass,
473
+ onUpdate: (x: number) => {
474
+ if (signal.cancelled) {
475
+ controls.stop();
476
+ resolve();
477
+ return;
478
+ }
479
+ const t = clamp01((x - startX) / travel);
480
+ const clipRight =
481
+ wordW > 0 ? 100 - clamp01(x / wordW) * 100 : 100;
482
+ const frameIdx = Math.round(t * sampleCount);
483
+ schedule(
484
+ x,
485
+ clipRight,
486
+ startRot1 + t * rollDeg,
487
+ morphFrames1[frameIdx]!
488
+ );
489
+ },
490
+ onComplete: () => {
491
+ totalRotation = startRot1 + rollDeg;
492
+ shapeStep = (shapeStep + 1) % shapes.length;
493
+ resolve();
494
+ },
495
+ });
496
+ });
497
+
498
+ if (signal.cancelled) break;
499
+
500
+ // Shimmer
501
+ if (shimmer) {
502
+ runShimmerEffect(shimmerEl);
503
+ }
504
+
505
+ // Hold — ball morphs during the pause (batched through scheduler)
506
+ if (holdTime > 0) {
507
+ const holdStart = performance.now();
508
+ // Capture ball position at end of reveal for hold phase
509
+ const holdBallX = endX;
510
+ while (
511
+ !signal.cancelled &&
512
+ performance.now() - holdStart < holdTime
513
+ ) {
514
+ await rafDelay(300, signal);
515
+ if (signal.cancelled || performance.now() - holdStart >= holdTime)
516
+ break;
517
+
518
+ const nextStep = (shapeStep + 1) % shapes.length;
519
+ const holdFrames = sampledFrames[shapeStep % shapes.length]!;
520
+ const startRotH = totalRotation;
521
+ const targetRotH = totalRotation + 360;
522
+
523
+ await animate(0, 1, {
524
+ type: "spring",
525
+ stiffness: 30,
526
+ damping: 9,
527
+ mass: 0.5,
528
+ onUpdate: (v: number) => {
529
+ const frameIdx = Math.round(clamp01(v) * sampleCount);
530
+ schedule(
531
+ holdBallX,
532
+ 0,
533
+ startRotH + v * (targetRotH - startRotH),
534
+ holdFrames[frameIdx]!
535
+ );
536
+ },
537
+ });
538
+
539
+ totalRotation = targetRotH;
540
+ shapeStep = nextStep;
541
+ }
542
+ }
543
+ if (signal.cancelled) break;
544
+
545
+ // Phase 2: Roll back left → hide
546
+ const morphFrames2 = sampledFrames[shapeStep % shapes.length]!;
547
+ const startRot2 = totalRotation;
548
+
549
+ await new Promise<void>((resolve) => {
550
+ if (signal.cancelled) {
551
+ resolve();
552
+ return;
553
+ }
554
+ const controls = animate(endX, startX, {
555
+ type: "spring",
556
+ stiffness: stiffness * rollSpeed,
557
+ damping,
558
+ mass,
559
+ onUpdate: (x: number) => {
560
+ if (signal.cancelled) {
561
+ controls.stop();
562
+ resolve();
563
+ return;
564
+ }
565
+ const t = clamp01(1 - (x - startX) / travel);
566
+ const clipRight =
567
+ wordW > 0 ? 100 - clamp01(x / wordW) * 100 : 100;
568
+ const frameIdx = Math.round(t * sampleCount);
569
+ schedule(
570
+ x,
571
+ clipRight,
572
+ startRot2 - t * rollDeg,
573
+ morphFrames2[frameIdx]!
574
+ );
575
+ },
576
+ onComplete: () => {
577
+ totalRotation = startRot2 - rollDeg;
578
+ shapeStep = (shapeStep + 1) % shapes.length;
579
+ resolve();
580
+ },
581
+ });
582
+ });
583
+
584
+ if (signal.cancelled) break;
585
+
586
+ // Brief pause between items (frame-aligned)
587
+ await rafDelay(300, signal);
588
+ wordIndex++;
589
+ }
590
+ }
591
+
592
+ loop();
593
+ return () => {
594
+ signal.cancelled = true;
595
+ };
596
+ }, [
597
+ widths,
598
+ ballSize,
599
+ items.length,
600
+ reducedMotion,
601
+ rollSpeed,
602
+ holdTime,
603
+ shimmer,
604
+ stiffness,
605
+ damping,
606
+ mass,
607
+ shapes,
608
+ ]);
609
+
610
+ const maxWidth = useMemo(
611
+ () => (widths.length > 0 ? Math.max(...widths) : 0),
612
+ [widths]
613
+ );
614
+ const ready = widths.length > 0 && ballSize > 0;
615
+
616
+ return (
617
+ <div
618
+ ref={ref}
619
+ className={cn("relative inline-flex items-center", className)}
620
+ data-hook={dataHook}
621
+ data-slot="text-reveal"
622
+ data-tracking-el={trackingEl}
623
+ data-tracking-label={trackingLabel}
624
+ style={{
625
+ opacity: ready ? 1 : 0,
626
+ transition: "opacity 0.4s ease-out",
627
+ }}
628
+ {...props}
629
+ >
630
+ <div
631
+ ref={containerRef}
632
+ className="relative flex items-center"
633
+ style={{
634
+ width: maxWidth || undefined,
635
+ transition: "width 0.3s ease-out",
636
+ }}
637
+ >
638
+ <span
639
+ ref={textWrapRef}
640
+ className="relative inline-block overflow-hidden"
641
+ style={{
642
+ width: maxWidth || undefined,
643
+ transition: "width 0.3s ease-out",
644
+ }}
645
+ >
646
+ {items.map((child, i) => (
647
+ <span
648
+ key={i}
649
+ ref={itemRefCallback(i)}
650
+ style={{
651
+ display: "block",
652
+ whiteSpace: "nowrap",
653
+ opacity: 0,
654
+ clipPath: "inset(0 100% 0 0)",
655
+ willChange: "clip-path",
656
+ ...(i > 0 ? { position: "absolute", top: 0, left: 0 } : {}),
657
+ }}
658
+ >
659
+ {child}
660
+ </span>
661
+ ))}
662
+
663
+ {/* Shimmer overlay — uses var(--background) from the design token preset */}
664
+ <div
665
+ ref={shimmerRef}
666
+ className="pointer-events-none absolute inset-0"
667
+ style={{
668
+ opacity: 0,
669
+ transform: "translateX(-100%)",
670
+ willChange: "transform",
671
+ background:
672
+ "linear-gradient(90deg, transparent 0%, color-mix(in srgb, var(--background) 40%, transparent) 40%, color-mix(in srgb, var(--background) 70%, transparent) 50%, color-mix(in srgb, var(--background) 40%, transparent) 60%, transparent 100%)",
673
+ }}
674
+ />
675
+ </span>
676
+
677
+ {/* Rolling ball */}
678
+ <div
679
+ ref={ballRef}
680
+ className="absolute"
681
+ style={{
682
+ left: -ballSize / 2,
683
+ top: "50%",
684
+ marginTop: -ballSize / 2,
685
+ width: ballSize,
686
+ height: ballSize,
687
+ transform: "translateX(0px)",
688
+ willChange: "transform",
689
+ }}
690
+ >
691
+ <svg
692
+ aria-label="Loading"
693
+ data-slot="text-reveal-ball"
694
+ fill="none"
695
+ height={ballSize}
696
+ role="img"
697
+ style={{ overflow: "visible" }}
698
+ viewBox="-30 -30 560 560"
699
+ width={ballSize}
700
+ xmlns="http://www.w3.org/2000/svg"
701
+ >
702
+ <defs>
703
+ <linearGradient ref={gradientElRef} id={gradientId}>
704
+ <stop offset="-20%" stopColor={gradientFrom} />
705
+ <stop offset="120%" stopColor={gradientTo} />
706
+ </linearGradient>
707
+ </defs>
708
+ <g
709
+ ref={groupRef}
710
+ style={{
711
+ opacity: 0,
712
+ transformOrigin: "250px 250px",
713
+ willChange: "transform",
714
+ }}
715
+ >
716
+ <path
717
+ ref={pathRef}
718
+ d={shapes[0]}
719
+ fill={`url(#${gradientId})`}
720
+ />
721
+ </g>
722
+ </svg>
723
+ </div>
724
+ </div>
725
+ </div>
726
+ );
727
+ }
728
+ );
729
+
730
+ TextRevealMotion.displayName = "TextRevealMotion";
731
+
732
+ export { TextRevealMotion };