@brightlocal/ui-components 1.2.3 → 1.3.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.
- package/README.md +1 -1
- package/component-meta.json +492 -113
- package/dist/blocks/input-list.cjs +2 -2
- package/dist/blocks/input-list.d.ts +2 -9
- package/dist/blocks/input-list.d.ts.map +1 -1
- package/dist/blocks/input-list.js +134 -173
- package/dist/hooks/use-prefers-reduced-motion.cjs +1 -0
- package/dist/hooks/use-prefers-reduced-motion.d.ts +7 -0
- package/dist/hooks/use-prefers-reduced-motion.d.ts.map +1 -0
- package/dist/hooks/use-prefers-reduced-motion.js +18 -0
- package/dist/index.cjs +1 -1
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +133 -129
- package/dist/text-reveal-motion.cjs +1 -0
- package/dist/text-reveal-motion.d.ts +73 -0
- package/dist/text-reveal-motion.d.ts.map +1 -0
- package/dist/text-reveal-motion.js +315 -0
- package/package.json +5 -3
- package/src/blocks/input-list.tsx +97 -152
- package/src/components/text-reveal-motion.tsx +568 -0
- package/src/hooks/use-prefers-reduced-motion.ts +28 -0
- package/src/index.ts +2 -0
|
@@ -0,0 +1,568 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import * as React from "react";
|
|
4
|
+
import { useCallback, useEffect, useId, useRef, useState } from "react";
|
|
5
|
+
import { animate } from "framer-motion";
|
|
6
|
+
import { interpolate } from "flubber";
|
|
7
|
+
|
|
8
|
+
import { cn } from "@brightlocal/ui-components/lib/utils";
|
|
9
|
+
import { usePrefersReducedMotion } from "../hooks/use-prefers-reduced-motion";
|
|
10
|
+
|
|
11
|
+
// ---------------------------------------------------------------------------
|
|
12
|
+
// Shape paths (500×500 viewBox)
|
|
13
|
+
// ---------------------------------------------------------------------------
|
|
14
|
+
|
|
15
|
+
const CIRCLE_PATH =
|
|
16
|
+
"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";
|
|
17
|
+
|
|
18
|
+
const STAR_PATH =
|
|
19
|
+
"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";
|
|
20
|
+
|
|
21
|
+
const STAR_BURST =
|
|
22
|
+
"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";
|
|
23
|
+
|
|
24
|
+
/** Clamp value to [0, 1] — avoids repeated Math.min/max in hot loops */
|
|
25
|
+
const clamp01 = (v: number) => (v < 0 ? 0 : v > 1 ? 1 : v);
|
|
26
|
+
|
|
27
|
+
/** Default 6-shape morph sequence */
|
|
28
|
+
const DEFAULT_SHAPES: string[] = [
|
|
29
|
+
CIRCLE_PATH,
|
|
30
|
+
STAR_PATH,
|
|
31
|
+
STAR_BURST,
|
|
32
|
+
CIRCLE_PATH,
|
|
33
|
+
STAR_BURST,
|
|
34
|
+
STAR_PATH,
|
|
35
|
+
];
|
|
36
|
+
|
|
37
|
+
// ---------------------------------------------------------------------------
|
|
38
|
+
// Props
|
|
39
|
+
// ---------------------------------------------------------------------------
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Props for the TextRevealMotion component.
|
|
43
|
+
* Extends native div props for full HTML attribute support.
|
|
44
|
+
*
|
|
45
|
+
* Each child represents one item in the reveal cycle. The rolling ball
|
|
46
|
+
* animates across, revealing each child via clip-path, then rolls back
|
|
47
|
+
* before moving to the next.
|
|
48
|
+
*
|
|
49
|
+
* @example
|
|
50
|
+
* ```tsx
|
|
51
|
+
* <TextRevealMotion dataHook="loader">
|
|
52
|
+
* <TypographyP dataHook="w1">Analyzing</TypographyP>
|
|
53
|
+
* <TypographyP dataHook="w2">Processing</TypographyP>
|
|
54
|
+
* </TextRevealMotion>
|
|
55
|
+
* ```
|
|
56
|
+
*/
|
|
57
|
+
export interface TextRevealMotionProps extends Omit<
|
|
58
|
+
React.ComponentPropsWithoutRef<"div">,
|
|
59
|
+
"children"
|
|
60
|
+
> {
|
|
61
|
+
/**
|
|
62
|
+
* Required data-hook attribute for automated testing.
|
|
63
|
+
* @example "text-reveal-loader"
|
|
64
|
+
*/
|
|
65
|
+
dataHook: string;
|
|
66
|
+
/**
|
|
67
|
+
* How long (ms) each item stays fully visible.
|
|
68
|
+
* The ball keeps morphing and rotating during the hold.
|
|
69
|
+
* @default 1300
|
|
70
|
+
*/
|
|
71
|
+
holdTime?: number;
|
|
72
|
+
/**
|
|
73
|
+
* Show shimmer sweep after each reveal.
|
|
74
|
+
* @default true
|
|
75
|
+
*/
|
|
76
|
+
shimmer?: boolean;
|
|
77
|
+
/**
|
|
78
|
+
* Gradient start color. Accepts any CSS color value.
|
|
79
|
+
* @default "var(--loading-gradient-from)"
|
|
80
|
+
*/
|
|
81
|
+
gradientFrom?: string;
|
|
82
|
+
/**
|
|
83
|
+
* Gradient end color. Accepts any CSS color value.
|
|
84
|
+
* @default "var(--loading-gradient-to)"
|
|
85
|
+
*/
|
|
86
|
+
gradientTo?: string;
|
|
87
|
+
/**
|
|
88
|
+
* Tracking element identifier for analytics.
|
|
89
|
+
*/
|
|
90
|
+
trackingEl?: string;
|
|
91
|
+
/**
|
|
92
|
+
* Tracking label for analytics context.
|
|
93
|
+
*/
|
|
94
|
+
trackingLabel?: string;
|
|
95
|
+
/**
|
|
96
|
+
* Each child is one item in the reveal cycle.
|
|
97
|
+
* Pass any Typography component as a child.
|
|
98
|
+
* @example
|
|
99
|
+
* ```tsx
|
|
100
|
+
* <TypographyP dataHook="w1">Analyzing</TypographyP>
|
|
101
|
+
* <TypographyP dataHook="w2">Processing</TypographyP>
|
|
102
|
+
* ```
|
|
103
|
+
*/
|
|
104
|
+
children: React.ReactNode;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
// ---------------------------------------------------------------------------
|
|
108
|
+
// Component
|
|
109
|
+
// ---------------------------------------------------------------------------
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Animated text reveal component with a rolling shape-morphing ball.
|
|
113
|
+
*
|
|
114
|
+
* The ball rolls across each child, revealing it via clip-path, holds,
|
|
115
|
+
* then rolls back and cycles to the next child. Supports any Typography
|
|
116
|
+
* component as children.
|
|
117
|
+
*/
|
|
118
|
+
const TextRevealMotion = React.forwardRef<
|
|
119
|
+
HTMLDivElement,
|
|
120
|
+
TextRevealMotionProps
|
|
121
|
+
>(
|
|
122
|
+
(
|
|
123
|
+
{
|
|
124
|
+
className,
|
|
125
|
+
dataHook,
|
|
126
|
+
holdTime = 1300,
|
|
127
|
+
shimmer = true,
|
|
128
|
+
gradientFrom = "var(--loading-gradient-from)",
|
|
129
|
+
gradientTo = "var(--loading-gradient-to)",
|
|
130
|
+
trackingEl,
|
|
131
|
+
trackingLabel,
|
|
132
|
+
children,
|
|
133
|
+
...props
|
|
134
|
+
},
|
|
135
|
+
ref
|
|
136
|
+
) => {
|
|
137
|
+
// Internal animation defaults matching the Loading story tuning
|
|
138
|
+
const shapes = DEFAULT_SHAPES;
|
|
139
|
+
const rollSpeed = 0.8;
|
|
140
|
+
const stiffness = 180;
|
|
141
|
+
const damping = 24;
|
|
142
|
+
const mass = 1;
|
|
143
|
+
|
|
144
|
+
const reducedMotion = usePrefersReducedMotion();
|
|
145
|
+
const rawId = useId();
|
|
146
|
+
const gradientId = `text-reveal-gradient-${rawId.replace(/:/g, "")}`;
|
|
147
|
+
|
|
148
|
+
// Refs for imperative animation
|
|
149
|
+
const ballRef = useRef<HTMLDivElement>(null);
|
|
150
|
+
const shimmerRef = useRef<HTMLDivElement>(null);
|
|
151
|
+
const pathRef = useRef<SVGPathElement>(null);
|
|
152
|
+
const groupRef = useRef<SVGGElement>(null);
|
|
153
|
+
const gradientElRef = useRef<SVGLinearGradientElement>(null);
|
|
154
|
+
const itemRefsMap = useRef<Map<number, HTMLSpanElement>>(new Map());
|
|
155
|
+
|
|
156
|
+
const items = React.Children.toArray(children);
|
|
157
|
+
const [measurements, setMeasurements] = useState<{
|
|
158
|
+
widths: number[];
|
|
159
|
+
ballSize: number;
|
|
160
|
+
}>({ widths: [], ballSize: 0 });
|
|
161
|
+
const { widths, ballSize } = measurements;
|
|
162
|
+
|
|
163
|
+
// -----------------------------------------------------------------------
|
|
164
|
+
// Collect refs and measure on last mount
|
|
165
|
+
// -----------------------------------------------------------------------
|
|
166
|
+
const itemRefCallback = useCallback(
|
|
167
|
+
(index: number) => (el: HTMLSpanElement | null) => {
|
|
168
|
+
if (el) {
|
|
169
|
+
itemRefsMap.current.set(index, el);
|
|
170
|
+
} else {
|
|
171
|
+
itemRefsMap.current.delete(index);
|
|
172
|
+
}
|
|
173
|
+
},
|
|
174
|
+
[]
|
|
175
|
+
);
|
|
176
|
+
|
|
177
|
+
// Measure children widths and font-size after refs are attached.
|
|
178
|
+
// setState is deferred via rAF so it runs outside the synchronous
|
|
179
|
+
// effect body, satisfying the react-hooks/set-state-in-effect rule.
|
|
180
|
+
useEffect(() => {
|
|
181
|
+
if (itemRefsMap.current.size === 0) return;
|
|
182
|
+
const raf = requestAnimationFrame(() => {
|
|
183
|
+
const w: number[] = [];
|
|
184
|
+
let fontSize = 32;
|
|
185
|
+
itemRefsMap.current.forEach((node, i) => {
|
|
186
|
+
w[i] = node.scrollWidth;
|
|
187
|
+
if (i === 0) {
|
|
188
|
+
// Read font-size from the actual Typography element, not the wrapper span.
|
|
189
|
+
// Ball size = 1.5× font-size for proportional scaling.
|
|
190
|
+
const textEl = node.firstElementChild ?? node;
|
|
191
|
+
const fs = parseFloat(getComputedStyle(textEl).fontSize) || 16;
|
|
192
|
+
fontSize = Math.round(fs * 1.5);
|
|
193
|
+
}
|
|
194
|
+
});
|
|
195
|
+
setMeasurements({ widths: w, ballSize: fontSize });
|
|
196
|
+
});
|
|
197
|
+
return () => cancelAnimationFrame(raf);
|
|
198
|
+
}, [items.length]);
|
|
199
|
+
|
|
200
|
+
// -----------------------------------------------------------------------
|
|
201
|
+
// Gradient wander
|
|
202
|
+
// -----------------------------------------------------------------------
|
|
203
|
+
useEffect(() => {
|
|
204
|
+
if (reducedMotion) return;
|
|
205
|
+
const grad = gradientElRef.current;
|
|
206
|
+
if (!grad) return;
|
|
207
|
+
// Capture as non-null const for use inside rAF closure
|
|
208
|
+
const gradEl: SVGLinearGradientElement = grad;
|
|
209
|
+
let raf: number;
|
|
210
|
+
let start: number | null = null;
|
|
211
|
+
const sA = 0.12 + Math.random() * 0.06;
|
|
212
|
+
const sB = 0.08 + Math.random() * 0.04;
|
|
213
|
+
const oA = Math.random() * Math.PI * 2;
|
|
214
|
+
const oB = Math.random() * Math.PI * 2;
|
|
215
|
+
// Track previous values to skip redundant DOM writes
|
|
216
|
+
let prevAngle = -Infinity;
|
|
217
|
+
function tick(now: number) {
|
|
218
|
+
if (!start) start = now;
|
|
219
|
+
const t = (now - start) / 1000;
|
|
220
|
+
const angle =
|
|
221
|
+
Math.sin(t * sA + oA) * Math.PI + Math.sin(t * sB + oB) * 0.5;
|
|
222
|
+
// Skip write when angle change is imperceptible (< 0.01 rad ≈ 0.6°)
|
|
223
|
+
if (Math.abs(angle - prevAngle) > 0.01) {
|
|
224
|
+
prevAngle = angle;
|
|
225
|
+
const cos = Math.cos(angle);
|
|
226
|
+
const sin = Math.sin(angle);
|
|
227
|
+
gradEl.setAttribute("x1", `${50 + 45 * cos}%`);
|
|
228
|
+
gradEl.setAttribute("y1", `${50 + 45 * sin}%`);
|
|
229
|
+
gradEl.setAttribute("x2", `${50 - 45 * cos}%`);
|
|
230
|
+
gradEl.setAttribute("y2", `${50 - 45 * sin}%`);
|
|
231
|
+
}
|
|
232
|
+
raf = requestAnimationFrame(tick);
|
|
233
|
+
}
|
|
234
|
+
raf = requestAnimationFrame(tick);
|
|
235
|
+
return () => cancelAnimationFrame(raf);
|
|
236
|
+
}, [reducedMotion]);
|
|
237
|
+
|
|
238
|
+
// -----------------------------------------------------------------------
|
|
239
|
+
// Shimmer helper
|
|
240
|
+
// -----------------------------------------------------------------------
|
|
241
|
+
function runShimmerEffect(el: HTMLDivElement) {
|
|
242
|
+
el.style.opacity = "1";
|
|
243
|
+
animate(0, 1, {
|
|
244
|
+
duration: 1.2,
|
|
245
|
+
ease: [0.25, 1, 0.5, 1],
|
|
246
|
+
onUpdate: (p: number) => {
|
|
247
|
+
el.style.transform = `translateX(${-100 + p * 200}%)`;
|
|
248
|
+
},
|
|
249
|
+
onComplete: () => {
|
|
250
|
+
el.style.opacity = "0";
|
|
251
|
+
},
|
|
252
|
+
});
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
// -----------------------------------------------------------------------
|
|
256
|
+
// Main animation loop
|
|
257
|
+
// -----------------------------------------------------------------------
|
|
258
|
+
useEffect(() => {
|
|
259
|
+
if (
|
|
260
|
+
!pathRef.current ||
|
|
261
|
+
!groupRef.current ||
|
|
262
|
+
!ballRef.current ||
|
|
263
|
+
!shimmerRef.current ||
|
|
264
|
+
widths.length === 0 ||
|
|
265
|
+
ballSize === 0
|
|
266
|
+
)
|
|
267
|
+
return;
|
|
268
|
+
|
|
269
|
+
// Capture as non-null locals for use inside async closures
|
|
270
|
+
const path: SVGPathElement = pathRef.current;
|
|
271
|
+
const group: SVGGElement = groupRef.current;
|
|
272
|
+
const ball: HTMLDivElement = ballRef.current;
|
|
273
|
+
const shimmerEl: HTMLDivElement = shimmerRef.current;
|
|
274
|
+
|
|
275
|
+
if (reducedMotion) {
|
|
276
|
+
path.setAttribute("d", shapes[0]!);
|
|
277
|
+
group.style.opacity = "1";
|
|
278
|
+
const firstItem = itemRefsMap.current.get(0);
|
|
279
|
+
if (firstItem) {
|
|
280
|
+
firstItem.style.visibility = "visible";
|
|
281
|
+
firstItem.style.clipPath = "none";
|
|
282
|
+
}
|
|
283
|
+
const gap0 = ballSize * 0.5;
|
|
284
|
+
const endX0 = (widths[0] ?? 0) + gap0 + ballSize / 2;
|
|
285
|
+
ball.style.transform = `translateX(${endX0}px)`;
|
|
286
|
+
return;
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
let cancelled = false;
|
|
290
|
+
// Equal visual gap between ball edge and text edge on both sides
|
|
291
|
+
const gap = ballSize * 0.5;
|
|
292
|
+
|
|
293
|
+
// Precompute all flubber interpolators — avoids expensive SVG path
|
|
294
|
+
// parsing inside the animation loop.
|
|
295
|
+
const interpCache: ((t: number) => string)[] = [];
|
|
296
|
+
for (let i = 0; i < shapes.length; i++) {
|
|
297
|
+
interpCache.push(
|
|
298
|
+
interpolate(shapes[i]!, shapes[(i + 1) % shapes.length]!)
|
|
299
|
+
);
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
async function loop() {
|
|
303
|
+
group.style.opacity = "1";
|
|
304
|
+
let wordIndex = 0;
|
|
305
|
+
let shapeStep = 0;
|
|
306
|
+
let totalRotation = 0;
|
|
307
|
+
|
|
308
|
+
while (!cancelled) {
|
|
309
|
+
const idx = wordIndex % items.length;
|
|
310
|
+
const wordW = widths[idx] ?? 0;
|
|
311
|
+
|
|
312
|
+
// Ball center positions for equal visual gap on both sides
|
|
313
|
+
const startX = -(gap + ballSize / 2);
|
|
314
|
+
const endX = wordW + gap + ballSize / 2;
|
|
315
|
+
const travel = endX - startX;
|
|
316
|
+
|
|
317
|
+
const container = itemRefsMap.current.get(idx);
|
|
318
|
+
if (!container) break;
|
|
319
|
+
|
|
320
|
+
// Show this item (clipped), hide all others
|
|
321
|
+
itemRefsMap.current.forEach((el, i) => {
|
|
322
|
+
el.style.visibility = i === idx ? "visible" : "hidden";
|
|
323
|
+
el.style.clipPath = "inset(0 100% 0 0)";
|
|
324
|
+
});
|
|
325
|
+
|
|
326
|
+
// Phase 1: Roll right → reveal
|
|
327
|
+
const interp1 = interpCache[shapeStep % shapes.length]!;
|
|
328
|
+
const rollDeg = 360;
|
|
329
|
+
const startRot1 = totalRotation;
|
|
330
|
+
|
|
331
|
+
await new Promise<void>((resolve) => {
|
|
332
|
+
if (cancelled) {
|
|
333
|
+
resolve();
|
|
334
|
+
return;
|
|
335
|
+
}
|
|
336
|
+
const controls = animate(startX, endX, {
|
|
337
|
+
type: "spring",
|
|
338
|
+
stiffness: stiffness * rollSpeed,
|
|
339
|
+
damping,
|
|
340
|
+
mass,
|
|
341
|
+
onUpdate: (x: number) => {
|
|
342
|
+
if (cancelled) {
|
|
343
|
+
controls.stop();
|
|
344
|
+
resolve();
|
|
345
|
+
return;
|
|
346
|
+
}
|
|
347
|
+
ball.style.transform = `translateX(${x}px)`;
|
|
348
|
+
const clipRight =
|
|
349
|
+
wordW > 0 ? 100 - clamp01(x / wordW) * 100 : 100;
|
|
350
|
+
container.style.clipPath = `inset(0 ${clipRight}% 0 0)`;
|
|
351
|
+
const t = (x - startX) / travel;
|
|
352
|
+
group.style.transform = `rotate(${startRot1 + t * rollDeg}deg)`;
|
|
353
|
+
path.setAttribute("d", interp1(clamp01(t)));
|
|
354
|
+
},
|
|
355
|
+
onComplete: () => {
|
|
356
|
+
totalRotation = startRot1 + rollDeg;
|
|
357
|
+
shapeStep = (shapeStep + 1) % shapes.length;
|
|
358
|
+
resolve();
|
|
359
|
+
},
|
|
360
|
+
});
|
|
361
|
+
});
|
|
362
|
+
|
|
363
|
+
if (cancelled) break;
|
|
364
|
+
|
|
365
|
+
// Shimmer
|
|
366
|
+
if (shimmer) {
|
|
367
|
+
runShimmerEffect(shimmerEl);
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
// Hold — identical loop to Default: pause → spring (await full settle)
|
|
371
|
+
if (holdTime > 0) {
|
|
372
|
+
const holdStart = performance.now();
|
|
373
|
+
while (!cancelled && performance.now() - holdStart < holdTime) {
|
|
374
|
+
await new Promise<void>((r) => setTimeout(r, 300));
|
|
375
|
+
if (cancelled || performance.now() - holdStart >= holdTime) break;
|
|
376
|
+
|
|
377
|
+
const nextStep = (shapeStep + 1) % shapes.length;
|
|
378
|
+
const interpH = interpCache[shapeStep % shapes.length]!;
|
|
379
|
+
const startRotH = totalRotation;
|
|
380
|
+
const targetRotH = totalRotation + 360;
|
|
381
|
+
|
|
382
|
+
// Await full spring settle — no early stop, same as Default
|
|
383
|
+
await animate(0, 1, {
|
|
384
|
+
type: "spring",
|
|
385
|
+
stiffness: 30,
|
|
386
|
+
damping: 9,
|
|
387
|
+
mass: 0.5,
|
|
388
|
+
onUpdate: (v: number) => {
|
|
389
|
+
path.setAttribute("d", interpH(clamp01(v)));
|
|
390
|
+
group.style.transform = `rotate(${startRotH + v * (targetRotH - startRotH)}deg)`;
|
|
391
|
+
},
|
|
392
|
+
});
|
|
393
|
+
|
|
394
|
+
totalRotation = targetRotH;
|
|
395
|
+
shapeStep = nextStep;
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
if (cancelled) break;
|
|
399
|
+
|
|
400
|
+
// Phase 2: Roll back left → hide
|
|
401
|
+
const interp2 = interpCache[shapeStep % shapes.length]!;
|
|
402
|
+
const startRot2 = totalRotation;
|
|
403
|
+
|
|
404
|
+
await new Promise<void>((resolve) => {
|
|
405
|
+
if (cancelled) {
|
|
406
|
+
resolve();
|
|
407
|
+
return;
|
|
408
|
+
}
|
|
409
|
+
const controls = animate(endX, startX, {
|
|
410
|
+
type: "spring",
|
|
411
|
+
stiffness: stiffness * rollSpeed,
|
|
412
|
+
damping,
|
|
413
|
+
mass,
|
|
414
|
+
onUpdate: (x: number) => {
|
|
415
|
+
if (cancelled) {
|
|
416
|
+
controls.stop();
|
|
417
|
+
resolve();
|
|
418
|
+
return;
|
|
419
|
+
}
|
|
420
|
+
ball.style.transform = `translateX(${x}px)`;
|
|
421
|
+
const clipRight =
|
|
422
|
+
wordW > 0 ? 100 - clamp01(x / wordW) * 100 : 100;
|
|
423
|
+
container.style.clipPath = `inset(0 ${clipRight}% 0 0)`;
|
|
424
|
+
const t = 1 - (x - startX) / travel;
|
|
425
|
+
group.style.transform = `rotate(${startRot2 - t * rollDeg}deg)`;
|
|
426
|
+
path.setAttribute("d", interp2(clamp01(t)));
|
|
427
|
+
},
|
|
428
|
+
onComplete: () => {
|
|
429
|
+
totalRotation = startRot2 - rollDeg;
|
|
430
|
+
shapeStep = (shapeStep + 1) % shapes.length;
|
|
431
|
+
resolve();
|
|
432
|
+
},
|
|
433
|
+
});
|
|
434
|
+
});
|
|
435
|
+
|
|
436
|
+
if (cancelled) break;
|
|
437
|
+
|
|
438
|
+
// Brief pause between items
|
|
439
|
+
await new Promise<void>((r) => setTimeout(r, 300));
|
|
440
|
+
wordIndex++;
|
|
441
|
+
}
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
loop();
|
|
445
|
+
return () => {
|
|
446
|
+
cancelled = true;
|
|
447
|
+
};
|
|
448
|
+
}, [
|
|
449
|
+
widths,
|
|
450
|
+
ballSize,
|
|
451
|
+
items.length,
|
|
452
|
+
reducedMotion,
|
|
453
|
+
rollSpeed,
|
|
454
|
+
holdTime,
|
|
455
|
+
shimmer,
|
|
456
|
+
stiffness,
|
|
457
|
+
damping,
|
|
458
|
+
mass,
|
|
459
|
+
shapes,
|
|
460
|
+
]);
|
|
461
|
+
|
|
462
|
+
const maxWidth = widths.length > 0 ? Math.max(...widths) : 0;
|
|
463
|
+
|
|
464
|
+
return (
|
|
465
|
+
<div
|
|
466
|
+
ref={ref}
|
|
467
|
+
className={cn("relative inline-flex items-center", className)}
|
|
468
|
+
data-hook={dataHook}
|
|
469
|
+
data-slot="text-reveal"
|
|
470
|
+
data-tracking-el={trackingEl}
|
|
471
|
+
data-tracking-label={trackingLabel}
|
|
472
|
+
{...props}
|
|
473
|
+
>
|
|
474
|
+
<div
|
|
475
|
+
className="relative flex items-center"
|
|
476
|
+
style={{ width: maxWidth || undefined }}
|
|
477
|
+
>
|
|
478
|
+
<span
|
|
479
|
+
className="relative inline-block overflow-hidden"
|
|
480
|
+
style={{ width: maxWidth || undefined }}
|
|
481
|
+
>
|
|
482
|
+
{items.map((child, i) => (
|
|
483
|
+
<span
|
|
484
|
+
key={i}
|
|
485
|
+
ref={itemRefCallback(i)}
|
|
486
|
+
style={{
|
|
487
|
+
display: "block",
|
|
488
|
+
whiteSpace: "nowrap",
|
|
489
|
+
visibility: "hidden",
|
|
490
|
+
clipPath: "inset(0 100% 0 0)",
|
|
491
|
+
willChange: "clip-path, visibility",
|
|
492
|
+
...(i > 0 ? { position: "absolute", top: 0, left: 0 } : {}),
|
|
493
|
+
}}
|
|
494
|
+
>
|
|
495
|
+
{child}
|
|
496
|
+
</span>
|
|
497
|
+
))}
|
|
498
|
+
|
|
499
|
+
{/* Shimmer overlay — uses var(--background) from the design token preset */}
|
|
500
|
+
<div
|
|
501
|
+
ref={shimmerRef}
|
|
502
|
+
className="pointer-events-none absolute inset-0"
|
|
503
|
+
style={{
|
|
504
|
+
opacity: 0,
|
|
505
|
+
transform: "translateX(-100%)",
|
|
506
|
+
willChange: "transform, opacity",
|
|
507
|
+
background:
|
|
508
|
+
"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%)",
|
|
509
|
+
}}
|
|
510
|
+
/>
|
|
511
|
+
</span>
|
|
512
|
+
|
|
513
|
+
{/* Rolling ball */}
|
|
514
|
+
<div
|
|
515
|
+
ref={ballRef}
|
|
516
|
+
className="absolute"
|
|
517
|
+
style={{
|
|
518
|
+
left: -ballSize / 2,
|
|
519
|
+
top: "50%",
|
|
520
|
+
marginTop: -ballSize / 2,
|
|
521
|
+
width: ballSize,
|
|
522
|
+
height: ballSize,
|
|
523
|
+
transform: "translateX(0px)",
|
|
524
|
+
willChange: "transform",
|
|
525
|
+
}}
|
|
526
|
+
>
|
|
527
|
+
<svg
|
|
528
|
+
aria-label="Loading"
|
|
529
|
+
data-slot="text-reveal-ball"
|
|
530
|
+
fill="none"
|
|
531
|
+
height={ballSize}
|
|
532
|
+
role="img"
|
|
533
|
+
style={{ overflow: "visible" }}
|
|
534
|
+
viewBox="-30 -30 560 560"
|
|
535
|
+
width={ballSize}
|
|
536
|
+
xmlns="http://www.w3.org/2000/svg"
|
|
537
|
+
>
|
|
538
|
+
<defs>
|
|
539
|
+
<linearGradient ref={gradientElRef} id={gradientId}>
|
|
540
|
+
<stop offset="-20%" stopColor={gradientFrom} />
|
|
541
|
+
<stop offset="120%" stopColor={gradientTo} />
|
|
542
|
+
</linearGradient>
|
|
543
|
+
</defs>
|
|
544
|
+
<g
|
|
545
|
+
ref={groupRef}
|
|
546
|
+
style={{
|
|
547
|
+
opacity: 0,
|
|
548
|
+
transformOrigin: "250px 250px",
|
|
549
|
+
willChange: "transform",
|
|
550
|
+
}}
|
|
551
|
+
>
|
|
552
|
+
<path
|
|
553
|
+
ref={pathRef}
|
|
554
|
+
d={shapes[0]}
|
|
555
|
+
fill={`url(#${gradientId})`}
|
|
556
|
+
/>
|
|
557
|
+
</g>
|
|
558
|
+
</svg>
|
|
559
|
+
</div>
|
|
560
|
+
</div>
|
|
561
|
+
</div>
|
|
562
|
+
);
|
|
563
|
+
}
|
|
564
|
+
);
|
|
565
|
+
|
|
566
|
+
TextRevealMotion.displayName = "TextRevealMotion";
|
|
567
|
+
|
|
568
|
+
export { TextRevealMotion };
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import { useSyncExternalStore } from "react";
|
|
4
|
+
|
|
5
|
+
const QUERY = "(prefers-reduced-motion: reduce)";
|
|
6
|
+
|
|
7
|
+
function subscribe(callback: () => void) {
|
|
8
|
+
const mql = window.matchMedia(QUERY);
|
|
9
|
+
mql.addEventListener("change", callback);
|
|
10
|
+
return () => mql.removeEventListener("change", callback);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
function getSnapshot() {
|
|
14
|
+
return window.matchMedia(QUERY).matches;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
function getServerSnapshot() {
|
|
18
|
+
return false;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Returns `true` when the user prefers reduced motion.
|
|
23
|
+
* Listens to OS/browser preference changes in real time.
|
|
24
|
+
* SSR-safe: defaults to `false` on the server.
|
|
25
|
+
*/
|
|
26
|
+
export function usePrefersReducedMotion() {
|
|
27
|
+
return useSyncExternalStore(subscribe, getSnapshot, getServerSnapshot);
|
|
28
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -55,6 +55,7 @@ export * from "./components/split-layout";
|
|
|
55
55
|
export * from "./components/switch";
|
|
56
56
|
export * from "./components/table";
|
|
57
57
|
export * from "./components/tabs";
|
|
58
|
+
export * from "./components/text-reveal-motion";
|
|
58
59
|
export * from "./components/textarea";
|
|
59
60
|
export * from "./components/toggle";
|
|
60
61
|
export * from "./components/toggle-group";
|
|
@@ -68,6 +69,7 @@ export * from "./blocks/sidebar";
|
|
|
68
69
|
|
|
69
70
|
// Hooks
|
|
70
71
|
export * from "./hooks/use-mobile";
|
|
72
|
+
export * from "./hooks/use-prefers-reduced-motion";
|
|
71
73
|
|
|
72
74
|
// Utils
|
|
73
75
|
export * from "./lib/utils";
|