@brightlocal/ui-components 2.23.0 → 2.24.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.
Files changed (90) hide show
  1. package/AI_USAGE.md +1 -1
  2. package/README.md +3 -2
  3. package/component-meta.json +116 -25
  4. package/deprecations.json +22 -0
  5. package/dist/alert.cjs +1 -1
  6. package/dist/alert.d.ts.map +1 -1
  7. package/dist/alert.js +6 -9
  8. package/dist/blocks/sidebar.cjs +1 -1
  9. package/dist/blocks/sidebar.d.ts.map +1 -1
  10. package/dist/blocks/sidebar.js +151 -156
  11. package/dist/brand-illustration-motif.cjs +1 -0
  12. package/dist/brand-illustration-motif.d.ts +55 -0
  13. package/dist/brand-illustration-motif.d.ts.map +1 -0
  14. package/dist/brand-illustration-motif.js +67 -0
  15. package/dist/chart.cjs +7 -7
  16. package/dist/chart.d.ts +287 -2
  17. package/dist/chart.d.ts.map +1 -1
  18. package/dist/chart.js +408 -152
  19. package/dist/combobox.cjs +3 -3
  20. package/dist/combobox.d.ts +14 -1
  21. package/dist/combobox.d.ts.map +1 -1
  22. package/dist/combobox.js +387 -376
  23. package/dist/data-table.cjs +1 -0
  24. package/dist/{blocks/data-table.d.ts → data-table.d.ts} +159 -4
  25. package/dist/data-table.d.ts.map +1 -0
  26. package/dist/data-table.js +721 -0
  27. package/dist/field.cjs +1 -1
  28. package/dist/field.d.ts +16 -1
  29. package/dist/field.d.ts.map +1 -1
  30. package/dist/field.js +23 -7
  31. package/dist/global-layout.cjs +1 -1
  32. package/dist/global-layout.d.ts +32 -2
  33. package/dist/global-layout.d.ts.map +1 -1
  34. package/dist/global-layout.js +179 -109
  35. package/dist/index.cjs +1 -1
  36. package/dist/index.d.ts +2 -1
  37. package/dist/index.d.ts.map +1 -1
  38. package/dist/index.js +526 -511
  39. package/dist/label.cjs +1 -1
  40. package/dist/label.d.ts +9 -0
  41. package/dist/label.d.ts.map +1 -1
  42. package/dist/label.js +26 -22
  43. package/dist/lib/spring-settle.cjs +1 -0
  44. package/dist/lib/spring-settle.d.ts +20 -0
  45. package/dist/lib/spring-settle.d.ts.map +1 -0
  46. package/dist/lib/spring-settle.js +4 -0
  47. package/dist/link.cjs +1 -1
  48. package/dist/link.d.ts +3 -2
  49. package/dist/link.d.ts.map +1 -1
  50. package/dist/link.js +22 -11
  51. package/dist/loading.cjs +1 -1
  52. package/dist/loading.d.ts +2 -1
  53. package/dist/loading.d.ts.map +1 -1
  54. package/dist/loading.js +106 -85
  55. package/dist/select.cjs +2 -2
  56. package/dist/select.d.ts +16 -1
  57. package/dist/select.d.ts.map +1 -1
  58. package/dist/select.js +112 -79
  59. package/dist/table.cjs +1 -1
  60. package/dist/table.d.ts +29 -0
  61. package/dist/table.d.ts.map +1 -1
  62. package/dist/table.js +2 -2
  63. package/dist/text-reveal-motion.cjs +1 -1
  64. package/dist/text-reveal-motion.d.ts.map +1 -1
  65. package/dist/text-reveal-motion.js +236 -219
  66. package/dist/typography.cjs +1 -1
  67. package/dist/typography.d.ts +20 -4
  68. package/dist/typography.d.ts.map +1 -1
  69. package/dist/typography.js +263 -228
  70. package/package.json +7 -7
  71. package/src/blocks/sidebar.tsx +11 -23
  72. package/src/components/alert.tsx +1 -4
  73. package/src/components/brand-illustration-motif.tsx +143 -0
  74. package/src/components/chart.tsx +632 -1
  75. package/src/components/combobox.tsx +47 -0
  76. package/src/{blocks → components}/data-table.tsx +270 -16
  77. package/src/components/field.tsx +31 -0
  78. package/src/components/global-layout.tsx +144 -5
  79. package/src/components/label.tsx +13 -1
  80. package/src/components/link.tsx +13 -1
  81. package/src/components/loading.tsx +71 -38
  82. package/src/components/select.tsx +82 -2
  83. package/src/components/table.tsx +31 -2
  84. package/src/components/text-reveal-motion.tsx +90 -64
  85. package/src/components/typography.tsx +74 -18
  86. package/src/index.ts +2 -1
  87. package/src/lib/spring-settle.ts +19 -0
  88. package/dist/blocks/data-table.cjs +0 -1
  89. package/dist/blocks/data-table.d.ts.map +0 -1
  90. package/dist/blocks/data-table.js +0 -654
@@ -12,6 +12,9 @@ const labelVariants = cva(
12
12
  optional: {
13
13
  true: "after:ml-1 after:content-['(optional)']",
14
14
  },
15
+ srOnly: {
16
+ true: "sr-only",
17
+ },
15
18
  },
16
19
  defaultVariants: {},
17
20
  }
@@ -38,6 +41,14 @@ interface LabelProps extends React.ComponentPropsWithoutRef<"label"> {
38
41
  * @default false
39
42
  */
40
43
  optional?: boolean;
44
+ /**
45
+ * Visually hides the label while keeping it in the accessibility tree,
46
+ * preserving the label/input association via `htmlFor`.
47
+ * The parent element must establish a positioning context (`relative`) —
48
+ * Field handles this automatically for FieldLabel.
49
+ * @default false
50
+ */
51
+ srOnly?: boolean;
41
52
  }
42
53
 
43
54
  const Label = React.forwardRef<HTMLLabelElement, LabelProps>(
@@ -45,6 +56,7 @@ const Label = React.forwardRef<HTMLLabelElement, LabelProps>(
45
56
  {
46
57
  className,
47
58
  optional,
59
+ srOnly,
48
60
  dataHook,
49
61
  trackingEl,
50
62
  trackingLabel,
@@ -58,7 +70,7 @@ const Label = React.forwardRef<HTMLLabelElement, LabelProps>(
58
70
  ref={ref}
59
71
  className={cn(
60
72
  "flex items-center select-none group-data-[disabled=true]:pointer-events-none group-data-[disabled=true]:opacity-50 peer-disabled:cursor-not-allowed peer-disabled:opacity-50",
61
- labelVariants({ optional }),
73
+ labelVariants({ optional, srOnly }),
62
74
  className
63
75
  )}
64
76
  data-hook={dataHook}
@@ -82,6 +82,17 @@ const linkVariants = cva(
82
82
  "text-foreground",
83
83
  "hover:bg-accent",
84
84
  ],
85
+ // Icon-only, circular. Mirrors `ghost` in a fixed 36px square
86
+ // footprint for icon-only links (e.g. back navigation).
87
+ // Requires an `aria-label` since there is no text content.
88
+ icon: [
89
+ ...buttonStyledBase,
90
+ "w-9",
91
+ "px-0",
92
+ "bg-transparent",
93
+ "text-foreground",
94
+ "hover:bg-accent",
95
+ ],
85
96
  },
86
97
  },
87
98
  defaultVariants: {
@@ -103,7 +114,8 @@ export interface LinkProps extends React.AnchorHTMLAttributes<HTMLAnchorElement>
103
114
  /**
104
115
  * Visual style variant. `inline` renders a standard text link; `filled`,
105
116
  * `outline` and `ghost` render button-styled treatments for inline actions
106
- * and high-priority navigation.
117
+ * and high-priority navigation; `icon` renders a circular icon-only link
118
+ * (provide an `aria-label` since there is no text content).
107
119
  * @default "inline"
108
120
  */
109
121
  variant?: LinkVariant;
@@ -6,6 +6,7 @@ import { animate } from "framer-motion";
6
6
 
7
7
  import { cn } from "@brightlocal/ui-components/lib/utils";
8
8
  import { usePrefersReducedMotion } from "../hooks/use-prefers-reduced-motion";
9
+ import { SETTLE_REST_DELTA } from "../lib/spring-settle";
9
10
 
10
11
  // ---------------------------------------------------------------------------
11
12
  // Shape paths (500×500 viewBox)
@@ -47,7 +48,11 @@ const LOADING_SHAPES: string[] = [
47
48
  const HOLD_MS = 150;
48
49
  /** Spring stiffness for the morph + rotation */
49
50
  const STIFFNESS = 30;
50
- /** Spring damping — higher = less oscillation */
51
+ /**
52
+ * Spring damping — higher = less oscillation.
53
+ * Must stay ≥ 2×√(STIFFNESS×MASS) (≈7.75) so the spring never overshoots —
54
+ * the settle trick below depends on it (see SETTLE_REST_DELTA).
55
+ */
51
56
  const DAMPING = 9;
52
57
  /** Spring mass — higher = more inertia */
53
58
  const MASS = 0.5;
@@ -59,20 +64,12 @@ const SIZE = 56;
59
64
  const GRADIENT_WANDER_SPEED = 0.15;
60
65
  /** How far the gradient endpoints swing from centre */
61
66
  const GRADIENT_WANDER_RADIUS = 45;
62
-
63
- /** Number of pre-sampled frames per flubber morph */
64
- const MORPH_SAMPLES = 30;
65
- /** Fewer samples on low-end devices to reduce pre-computation + memory */
66
- const MORPH_SAMPLES_LOW = 15;
67
-
68
- /**
69
- * Detect low-performance devices via hardware concurrency.
70
- * Falls back to false on SSR or when the API is unavailable.
71
- */
72
- const isLowPerfDevice =
73
- typeof navigator !== "undefined" &&
74
- typeof navigator.hardwareConcurrency === "number" &&
75
- navigator.hardwareConcurrency <= 4;
67
+ /** Base gradient direction — `from` colour glowing from the top-right
68
+ * corner into the `to` colour toward bottom-left, matching the brand
69
+ * Gradient Green swatch. */
70
+ const GRADIENT_BASE_ANGLE = -Math.PI / 4;
71
+ /** How far the gradient angle may drift from the base direction (radians) */
72
+ const GRADIENT_WANDER_SWING = 0.5;
76
73
 
77
74
  // ---------------------------------------------------------------------------
78
75
  // Props
@@ -117,7 +114,8 @@ export interface LoadingProps extends Omit<
117
114
  * The animation is fully self-contained and opinionated: size, timing, spring
118
115
  * physics and colours are all baked in. The only accessibility hook is an
119
116
  * optional `loadingLabel`. Colours come from the `--loading-gradient-from` /
120
- * `--loading-gradient-to` design tokens.
117
+ * `--loading-gradient-to` design tokens — the brand "Gradient Green"
118
+ * (#FFF085 → #2AE855) in light mode, chart-token variants in dark mode.
121
119
  *
122
120
  * Respects `prefers-reduced-motion`: renders a static shape when reduced motion
123
121
  * is requested.
@@ -167,7 +165,9 @@ const Loading = React.forwardRef<SVGSVGElement, LoadingProps>(
167
165
  if (start === null) start = now;
168
166
  const t = (now - start) / 1000;
169
167
  const angle =
170
- Math.sin(t * sA + oA) * Math.PI + Math.sin(t * sB + oB) * 0.5;
168
+ GRADIENT_BASE_ANGLE +
169
+ Math.sin(t * sA + oA) * GRADIENT_WANDER_SWING +
170
+ Math.sin(t * sB + oB) * 0.25;
171
171
  if (Math.abs(angle - prevAngle) > 0.01) {
172
172
  prevAngle = angle;
173
173
  const cos = Math.cos(angle);
@@ -191,10 +191,21 @@ const Loading = React.forwardRef<SVGSVGElement, LoadingProps>(
191
191
  const group = groupRef.current;
192
192
  if (!path || !group) return;
193
193
 
194
+ // The gradient counter-rotates against the shape so its orientation
195
+ // stays fixed on screen (yellow anchored top-right) while the shape
196
+ // spins beneath it.
197
+ const setRotation = (deg: number) => {
198
+ group.style.transform = `rotate(${deg}deg)`;
199
+ gradientElRef.current?.setAttribute(
200
+ "gradientTransform",
201
+ `rotate(${-deg} 0.5 0.5)`
202
+ );
203
+ };
204
+
194
205
  if (reducedMotion) {
195
206
  path.setAttribute("d", LOADING_SHAPES[0]!);
196
207
  group.style.opacity = "1";
197
- group.style.transform = "rotate(0deg)";
208
+ setRotation(0);
198
209
  return;
199
210
  }
200
211
 
@@ -205,20 +216,19 @@ const Loading = React.forwardRef<SVGSVGElement, LoadingProps>(
205
216
  const { interpolate } = await import("flubber");
206
217
  if (signal.cancelled) return;
207
218
 
208
- // Pre-sample every morph transition into a lookup table so we don't
209
- // regenerate flubber interpolators on every frame.
210
- const sampleCount = isLowPerfDevice ? MORPH_SAMPLES_LOW : MORPH_SAMPLES;
211
- const sampledFrames: string[][] = [];
219
+ // Build one interpolator per transition up-front — constructing them is
220
+ // the expensive part (point matching), calling one per frame is cheap
221
+ // (tens of µs). Do NOT pre-sample into a frame table: the spring's slow
222
+ // settle tail makes discrete frames visible as a pop into the final
223
+ // shape at ~99%.
224
+ const morphs: Array<(t: number) => string> = [];
212
225
  for (let i = 0; i < LOADING_SHAPES.length; i++) {
213
- const interp = interpolate(
214
- LOADING_SHAPES[i]!,
215
- LOADING_SHAPES[(i + 1) % LOADING_SHAPES.length]!
226
+ morphs.push(
227
+ interpolate(
228
+ LOADING_SHAPES[i]!,
229
+ LOADING_SHAPES[(i + 1) % LOADING_SHAPES.length]!
230
+ )
216
231
  );
217
- const frames: string[] = [];
218
- for (let j = 0; j <= sampleCount; j++) {
219
- frames.push(interp(j / sampleCount));
220
- }
221
- sampledFrames.push(frames);
222
232
  }
223
233
 
224
234
  group!.style.opacity = "1";
@@ -226,7 +236,7 @@ const Loading = React.forwardRef<SVGSVGElement, LoadingProps>(
226
236
  let rotation = 0;
227
237
 
228
238
  while (!signal.cancelled) {
229
- const morphFrames = sampledFrames[step % LOADING_SHAPES.length]!;
239
+ const morph = morphs[step]!;
230
240
 
231
241
  // Hold on the current shape
232
242
  await new Promise<void>((r) => setTimeout(r, HOLD_MS));
@@ -243,11 +253,12 @@ const Loading = React.forwardRef<SVGSVGElement, LoadingProps>(
243
253
  resolve();
244
254
  return;
245
255
  }
246
- const controls = animate(0, 1, {
256
+ const controls = animate(0, 1 + SETTLE_REST_DELTA, {
247
257
  type: "spring",
248
258
  stiffness: STIFFNESS,
249
259
  damping: DAMPING,
250
260
  mass: MASS,
261
+ restDelta: SETTLE_REST_DELTA,
251
262
  onUpdate: (v: number) => {
252
263
  if (signal.cancelled) {
253
264
  controls.stop();
@@ -255,11 +266,21 @@ const Loading = React.forwardRef<SVGSVGElement, LoadingProps>(
255
266
  return;
256
267
  }
257
268
  const t = clamp01(v);
258
- const frameIdx = Math.round(t * sampleCount);
259
- path!.setAttribute("d", morphFrames[frameIdx]!);
260
- group!.style.transform = `rotate(${startRotation + t * (targetRotation - startRotation)}deg)`;
269
+ path!.setAttribute("d", morph(t));
270
+ setRotation(
271
+ startRotation + t * (targetRotation - startRotation)
272
+ );
273
+ },
274
+ onComplete: () => {
275
+ // Pin the exact end state — don't rely on motion-dom's
276
+ // internal guarantee that the final onUpdate fires with the
277
+ // snapped target value.
278
+ if (!signal.cancelled) {
279
+ path!.setAttribute("d", morph(1));
280
+ setRotation(targetRotation);
281
+ }
282
+ resolve();
261
283
  },
262
- onComplete: () => resolve(),
263
284
  });
264
285
  });
265
286
 
@@ -293,8 +314,20 @@ const Loading = React.forwardRef<SVGSVGElement, LoadingProps>(
293
314
  {...props}
294
315
  >
295
316
  <defs>
296
- <linearGradient ref={gradientElRef} id={gradientId}>
297
- <stop offset="-20%" stopColor="var(--loading-gradient-from)" />
317
+ {/* Static direction matches GRADIENT_BASE_ANGLE for the first
318
+ frame and the reduced-motion render. */}
319
+ <linearGradient
320
+ ref={gradientElRef}
321
+ id={gradientId}
322
+ x1="81.8%"
323
+ x2="18.2%"
324
+ y1="18.2%"
325
+ y2="81.8%"
326
+ >
327
+ {/* The from-stop sits far outside the shape so the brightest
328
+ visible point is already blended toward the to-colour —
329
+ pure yellow never shows. */}
330
+ <stop offset="-60%" stopColor="var(--loading-gradient-from)" />
298
331
  <stop offset="120%" stopColor="var(--loading-gradient-to)" />
299
332
  </linearGradient>
300
333
  </defs>
@@ -21,6 +21,21 @@ interface SelectProps extends React.ComponentProps<
21
21
  > {
22
22
  /** Children to render inside the select */
23
23
  children?: React.ReactNode;
24
+ /**
25
+ * Close-commit callback — fired once each time the dropdown actually
26
+ * closes, and guaranteed to fire **after** any pending `onValueChange` has
27
+ * been delivered. Designed for blur-based form validation — in React Hook
28
+ * Form's onBlur mode, pass `field.onBlur` here so validation runs against
29
+ * the committed value when the dropdown closes, keeping `onOpenChange`
30
+ * free for product logic.
31
+ *
32
+ * Fires only on resolved closes: a controlled consumer that vetoes a close
33
+ * request gets no callback, while a programmatic controlled close does
34
+ * fire it. Unmounting while open counts as a close. Note that Radix Select
35
+ * also closes the dropdown when the window loses focus or resizes — those
36
+ * closes fire this callback too.
37
+ */
38
+ onClose?: () => void;
24
39
  }
25
40
 
26
41
  interface SelectContentProps extends React.ComponentProps<
@@ -58,9 +73,74 @@ interface SelectContentProps extends React.ComponentProps<
58
73
  * </Field>
59
74
  * ```
60
75
  */
61
- function Select({ children, ...props }: SelectProps) {
76
+ function Select({
77
+ children,
78
+ onClose,
79
+ onOpenChange,
80
+ open: openProp,
81
+ defaultOpen,
82
+ ...props
83
+ }: SelectProps) {
84
+ // Mirror the open state Radix resolves. Radix calls onOpenChange on every
85
+ // close *request*, even when a controlled consumer vetoes it by keeping
86
+ // `open` true — so onClose must be driven by the resolved state, never the
87
+ // request. Mirroring also catches programmatic controlled closes, which
88
+ // never go through onOpenChange at all.
89
+ const [uncontrolledOpen, setUncontrolledOpen] = React.useState(
90
+ defaultOpen ?? false
91
+ );
92
+ const isOpenControlled = openProp !== undefined;
93
+ const open = isOpenControlled ? openProp : uncontrolledOpen;
94
+
95
+ const handleOpenChange = React.useCallback(
96
+ (nextOpen: boolean) => {
97
+ if (!isOpenControlled) {
98
+ setUncontrolledOpen(nextOpen);
99
+ }
100
+ onOpenChange?.(nextOpen);
101
+ },
102
+ [isOpenControlled, onOpenChange]
103
+ );
104
+
105
+ // Close-commit callback (kept in sync with the identical block in
106
+ // combobox.tsx): fire onClose on the open → closed transition of the
107
+ // resolved state. Running in the effect phase — after the Radix Root
108
+ // child's own effects — guarantees an uncontrolled value's onValueChange
109
+ // (which Radix delivers from a passive effect) lands before onClose.
110
+ const onCloseRef = React.useRef(onClose);
111
+ React.useEffect(() => {
112
+ onCloseRef.current = onClose;
113
+ }, [onClose]);
114
+
115
+ const prevOpenRef = React.useRef(open);
116
+ React.useEffect(() => {
117
+ if (prevOpenRef.current && !open) {
118
+ onCloseRef.current?.();
119
+ }
120
+ prevOpenRef.current = open;
121
+ }, [open]);
122
+
123
+ // An unmount can BE the close (e.g. the dropdown is open inside a Dialog
124
+ // the same click dismisses) — the transition effect above never runs for an
125
+ // unmounting component, so fire from cleanup instead. Cleanups run
126
+ // child-first, so a react-hook-form Controller wrapping this component is
127
+ // still registered when its onBlur is invoked here.
128
+ React.useEffect(() => {
129
+ return () => {
130
+ if (prevOpenRef.current) {
131
+ onCloseRef.current?.();
132
+ }
133
+ };
134
+ }, []);
135
+
62
136
  return (
63
- <SelectPrimitive.Root data-slot="select" {...props}>
137
+ <SelectPrimitive.Root
138
+ data-slot="select"
139
+ defaultOpen={defaultOpen}
140
+ open={openProp}
141
+ onOpenChange={handleOpenChange}
142
+ {...props}
143
+ >
64
144
  {children}
65
145
  </SelectPrimitive.Root>
66
146
  );
@@ -32,7 +32,7 @@ const tableCellVariants = cva(
32
32
  );
33
33
 
34
34
  const tableHeadVariants = cva(
35
- "h-10 p-2 align-middle text-muted-foreground text-sm font-medium leading-normal hover:bg-muted break-words outline-none focus-visible:shadow-default transition-[background-color,box-shadow] duration-150 ease-in-out",
35
+ "h-12 p-2 align-middle text-muted-foreground text-sm font-medium leading-normal break-words outline-none focus-visible:shadow-default transition-[background-color,box-shadow] duration-150 ease-in-out",
36
36
  {
37
37
  variants: {
38
38
  align: {
@@ -157,7 +157,7 @@ const TableHeader = React.forwardRef<HTMLTableSectionElement, TableHeaderProps>(
157
157
  ({ className, dataHook, ...props }, ref) => (
158
158
  <thead
159
159
  ref={ref}
160
- className={cn(className)}
160
+ className={cn("bg-muted", className)}
161
161
  data-hook={dataHook}
162
162
  data-slot="table-header"
163
163
  {...props}
@@ -355,6 +355,10 @@ TableCell.displayName = "TableCell";
355
355
  * TableCaption
356
356
  * -----------------------------------------------------------------------------------------------*/
357
357
 
358
+ /**
359
+ * Props for the TableCaption component.
360
+ * @deprecated Name the table with a heading and `aria-labelledby` instead. TableCaption will be removed in the next major version.
361
+ */
358
362
  interface TableCaptionProps extends React.ComponentProps<"caption"> {
359
363
  /**
360
364
  * Data hook for automated testing
@@ -362,6 +366,31 @@ interface TableCaptionProps extends React.ComponentProps<"caption"> {
362
366
  dataHook?: string;
363
367
  }
364
368
 
369
+ /**
370
+ * @deprecated Name the table with a heading and `aria-labelledby` instead.
371
+ *
372
+ * A `<caption>` is placed solely by the CSS `caption-side` property, never by
373
+ * where it sits among the table's children, so it cannot participate in
374
+ * composition. It also supplies the table's accessible name while contributing
375
+ * nothing to the document outline, and it scrolls horizontally with the table
376
+ * when `minWidth` is set.
377
+ *
378
+ * ```tsx
379
+ * // Before
380
+ * <Table dataHook="transactions">
381
+ * <TableCaption>Recent transactions</TableCaption>
382
+ * ...
383
+ * </Table>
384
+ *
385
+ * // After
386
+ * <TypographyH3 dataHook="transactions-heading" id="transactions-heading">
387
+ * Recent transactions
388
+ * </TypographyH3>
389
+ * <Table aria-labelledby="transactions-heading" dataHook="transactions">
390
+ * ...
391
+ * </Table>
392
+ * ```
393
+ */
365
394
  const TableCaption = React.forwardRef<
366
395
  HTMLTableCaptionElement,
367
396
  TableCaptionProps
@@ -13,6 +13,7 @@ import { animate } from "framer-motion";
13
13
 
14
14
  import { cn } from "@brightlocal/ui-components/lib/utils";
15
15
  import { usePrefersReducedMotion } from "../hooks/use-prefers-reduced-motion";
16
+ import { SETTLE_REST_DELTA } from "../lib/spring-settle";
16
17
 
17
18
  // ---------------------------------------------------------------------------
18
19
  // Shape paths (500×500 viewBox)
@@ -40,21 +41,6 @@ const DEFAULT_SHAPES: string[] = [
40
41
  STAR_PATH,
41
42
  ];
42
43
 
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
44
  // ---------------------------------------------------------------------------
59
45
  // rAF-batched DOM write scheduler
60
46
  // ---------------------------------------------------------------------------
@@ -69,7 +55,8 @@ interface FrameState {
69
55
  function createFrameScheduler(
70
56
  ball: HTMLDivElement,
71
57
  group: SVGGElement,
72
- path: SVGPathElement
58
+ path: SVGPathElement,
59
+ gradient: SVGLinearGradientElement | null
73
60
  ) {
74
61
  let pending = false;
75
62
  let activeItemEl: HTMLSpanElement | null = null;
@@ -82,6 +69,12 @@ function createFrameScheduler(
82
69
  activeItemEl.style.clipPath = `inset(0 ${state.clipRight}% 0 0)`;
83
70
  }
84
71
  group.style.transform = `rotate(${state.rotate}deg)`;
72
+ // Counter-rotate the gradient so its orientation stays fixed on screen
73
+ // (yellow anchored top-right) while the ball spins beneath it.
74
+ gradient?.setAttribute(
75
+ "gradientTransform",
76
+ `rotate(${-state.rotate} 0.5 0.5)`
77
+ );
85
78
  path.setAttribute("d", state.pathD);
86
79
  }
87
80
 
@@ -326,8 +319,13 @@ const TextRevealMotion = React.forwardRef<
326
319
  function tick(now: number) {
327
320
  if (!start) start = now;
328
321
  const t = (now - start) / 1000;
322
+ // Anchored to the brand gradient direction — `from` colour glowing
323
+ // from top-right into the `to` colour toward bottom-left — drifting
324
+ // gently around it.
329
325
  const angle =
330
- Math.sin(t * sA + oA) * Math.PI + Math.sin(t * sB + oB) * 0.5;
326
+ -Math.PI / 4 +
327
+ Math.sin(t * sA + oA) * 0.5 +
328
+ Math.sin(t * sB + oB) * 0.25;
331
329
  if (Math.abs(angle - prevAngle) > 0.01) {
332
330
  prevAngle = angle;
333
331
  const cos = Math.cos(angle);
@@ -412,25 +410,25 @@ const TextRevealMotion = React.forwardRef<
412
410
  const { interpolate } = await import("flubber");
413
411
  if (signal.cancelled) return;
414
412
 
415
- // Pre-sample all morph transitions into lookup tables.
416
- // Eliminates per-frame flubber string generation (~50% CPU savings).
417
- // On low-end devices, halve the sample count to reduce memory + init cost.
418
- const sampleCount = isLowPerfDevice ? MORPH_SAMPLES_LOW : MORPH_SAMPLES;
419
- const sampledFrames: string[][] = [];
413
+ // Build one interpolator per transition up-front — constructing them is
414
+ // the expensive part (point matching), calling one per frame is cheap
415
+ // (tens of µs). Do NOT pre-sample into a frame table: the hold spring's
416
+ // slow settle tail makes discrete frames visible as a pop into the
417
+ // final shape at ~99% (same fix as loading.tsx).
418
+ const morphs: Array<(t: number) => string> = [];
420
419
  for (let i = 0; i < shapes.length; i++) {
421
- const interp = interpolate(
422
- shapes[i]!,
423
- shapes[(i + 1) % shapes.length]!
420
+ morphs.push(
421
+ interpolate(shapes[i]!, shapes[(i + 1) % shapes.length]!)
424
422
  );
425
- const frames: string[] = [];
426
- for (let j = 0; j <= sampleCount; j++) {
427
- frames.push(interp(j / sampleCount));
428
- }
429
- sampledFrames.push(frames);
430
423
  }
431
424
 
432
425
  // Create scheduler once — reuse across all phases and items
433
- const schedule = createFrameScheduler(ball, group, path);
426
+ const schedule = createFrameScheduler(
427
+ ball,
428
+ group,
429
+ path,
430
+ gradientElRef.current
431
+ );
434
432
 
435
433
  group.style.opacity = "1";
436
434
  let wordIndex = 0;
@@ -462,7 +460,7 @@ const TextRevealMotion = React.forwardRef<
462
460
  }
463
461
 
464
462
  // Phase 1: Roll right → reveal
465
- const morphFrames1 = sampledFrames[shapeStep % shapes.length]!;
463
+ const morph1 = morphs[shapeStep]!;
466
464
  const rollDeg = 360;
467
465
  const startRot1 = totalRotation;
468
466
 
@@ -485,17 +483,16 @@ const TextRevealMotion = React.forwardRef<
485
483
  const t = clamp01((x - startX) / travel);
486
484
  const clipRight =
487
485
  wordW > 0 ? 100 - clamp01(x / wordW) * 100 : 100;
488
- const frameIdx = Math.round(t * sampleCount);
489
- schedule(
490
- x,
491
- clipRight,
492
- startRot1 + t * rollDeg,
493
- morphFrames1[frameIdx]!
494
- );
486
+ schedule(x, clipRight, startRot1 + t * rollDeg, morph1(t));
495
487
  },
496
488
  onComplete: () => {
497
489
  totalRotation = startRot1 + rollDeg;
498
490
  shapeStep = (shapeStep + 1) % shapes.length;
491
+ // Pin the exact end state — don't rely on the final onUpdate
492
+ // having fired with the snapped target value.
493
+ if (!signal.cancelled) {
494
+ schedule(endX, 0, totalRotation, morph1(1));
495
+ }
499
496
  resolve();
500
497
  },
501
498
  });
@@ -522,26 +519,46 @@ const TextRevealMotion = React.forwardRef<
522
519
  break;
523
520
 
524
521
  const nextStep = (shapeStep + 1) % shapes.length;
525
- const holdFrames = sampledFrames[shapeStep % shapes.length]!;
522
+ const holdMorph = morphs[shapeStep]!;
526
523
  const startRotH = totalRotation;
527
524
  const targetRotH = totalRotation + 360;
528
525
 
529
- await animate(0, 1, {
530
- type: "spring",
531
- stiffness: 30,
532
- damping: 9,
533
- mass: 0.5,
534
- onUpdate: (v: number) => {
535
- const frameIdx = Math.round(clamp01(v) * sampleCount);
536
- schedule(
537
- holdBallX,
538
- 0,
539
- startRotH + v * (targetRotH - startRotH),
540
- holdFrames[frameIdx]!
541
- );
542
- },
526
+ // Aim past 1 and clamp the rendered progress so the spring's
527
+ // terminal snap lands off-screen (see SETTLE_REST_DELTA).
528
+ await new Promise<void>((resolve) => {
529
+ if (signal.cancelled) {
530
+ resolve();
531
+ return;
532
+ }
533
+ const controls = animate(0, 1 + SETTLE_REST_DELTA, {
534
+ type: "spring",
535
+ stiffness: 30,
536
+ damping: 9,
537
+ mass: 0.5,
538
+ restDelta: SETTLE_REST_DELTA,
539
+ onUpdate: (v: number) => {
540
+ if (signal.cancelled) {
541
+ controls.stop();
542
+ resolve();
543
+ return;
544
+ }
545
+ const t = clamp01(v);
546
+ schedule(
547
+ holdBallX,
548
+ 0,
549
+ startRotH + t * (targetRotH - startRotH),
550
+ holdMorph(t)
551
+ );
552
+ },
553
+ onComplete: resolve,
554
+ });
543
555
  });
544
556
 
557
+ // Pin the exact end state before the next hold iteration
558
+ if (!signal.cancelled) {
559
+ schedule(holdBallX, 0, targetRotH, holdMorph(1));
560
+ }
561
+
545
562
  totalRotation = targetRotH;
546
563
  shapeStep = nextStep;
547
564
  }
@@ -549,7 +566,7 @@ const TextRevealMotion = React.forwardRef<
549
566
  if (signal.cancelled) break;
550
567
 
551
568
  // Phase 2: Roll back left → hide
552
- const morphFrames2 = sampledFrames[shapeStep % shapes.length]!;
569
+ const morph2 = morphs[shapeStep]!;
553
570
  const startRot2 = totalRotation;
554
571
 
555
572
  await new Promise<void>((resolve) => {
@@ -571,17 +588,15 @@ const TextRevealMotion = React.forwardRef<
571
588
  const t = clamp01(1 - (x - startX) / travel);
572
589
  const clipRight =
573
590
  wordW > 0 ? 100 - clamp01(x / wordW) * 100 : 100;
574
- const frameIdx = Math.round(t * sampleCount);
575
- schedule(
576
- x,
577
- clipRight,
578
- startRot2 - t * rollDeg,
579
- morphFrames2[frameIdx]!
580
- );
591
+ schedule(x, clipRight, startRot2 - t * rollDeg, morph2(t));
581
592
  },
582
593
  onComplete: () => {
583
594
  totalRotation = startRot2 - rollDeg;
584
595
  shapeStep = (shapeStep + 1) % shapes.length;
596
+ // Pin the exact end state (ball parked left, item hidden)
597
+ if (!signal.cancelled) {
598
+ schedule(startX, 100, totalRotation, morph2(1));
599
+ }
585
600
  resolve();
586
601
  },
587
602
  });
@@ -706,8 +721,19 @@ const TextRevealMotion = React.forwardRef<
706
721
  xmlns="http://www.w3.org/2000/svg"
707
722
  >
708
723
  <defs>
709
- <linearGradient ref={gradientElRef} id={gradientId}>
710
- <stop offset="-20%" stopColor={gradientFrom} />
724
+ {/* Static direction matches the base angle for the first
725
+ frame and the reduced-motion render. */}
726
+ <linearGradient
727
+ ref={gradientElRef}
728
+ id={gradientId}
729
+ x1="81.8%"
730
+ x2="18.2%"
731
+ y1="18.2%"
732
+ y2="81.8%"
733
+ >
734
+ {/* From-stop far outside the shape — the brightest visible
735
+ point is already blended toward the to-colour. */}
736
+ <stop offset="-60%" stopColor={gradientFrom} />
711
737
  <stop offset="120%" stopColor={gradientTo} />
712
738
  </linearGradient>
713
739
  </defs>