@brightlocal/ui-components 2.21.0 → 2.23.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 (70) hide show
  1. package/README.md +2 -1
  2. package/component-meta.json +40 -5
  3. package/dist/alert.cjs +1 -1
  4. package/dist/alert.d.ts.map +1 -1
  5. package/dist/alert.js +31 -31
  6. package/dist/blocks/data-table.cjs +1 -1
  7. package/dist/blocks/data-table.d.ts +35 -3
  8. package/dist/blocks/data-table.d.ts.map +1 -1
  9. package/dist/blocks/data-table.js +178 -152
  10. package/dist/context-menu.cjs +1 -1
  11. package/dist/context-menu.d.ts.map +1 -1
  12. package/dist/context-menu.js +111 -109
  13. package/dist/dropdown-menu.cjs +1 -1
  14. package/dist/dropdown-menu.d.ts.map +1 -1
  15. package/dist/dropdown-menu.js +109 -121
  16. package/dist/hover-card.cjs +1 -1
  17. package/dist/hover-card.d.ts.map +1 -1
  18. package/dist/hover-card.js +45 -45
  19. package/dist/index.cjs +1 -1
  20. package/dist/index.d.ts +1 -0
  21. package/dist/index.d.ts.map +1 -1
  22. package/dist/index.js +311 -308
  23. package/dist/lib/utils.cjs +1 -1
  24. package/dist/lib/utils.d.ts +1 -0
  25. package/dist/lib/utils.d.ts.map +1 -1
  26. package/dist/lib/utils.js +7 -5
  27. package/dist/loading.cjs +1 -0
  28. package/dist/loading.d.ts +46 -0
  29. package/dist/loading.d.ts.map +1 -0
  30. package/dist/loading.js +142 -0
  31. package/dist/map.cjs +1 -1
  32. package/dist/map.d.ts.map +1 -1
  33. package/dist/map.js +1 -1
  34. package/dist/menubar.cjs +1 -1
  35. package/dist/menubar.d.ts.map +1 -1
  36. package/dist/menubar.js +97 -99
  37. package/dist/navigation-menu.cjs +1 -1
  38. package/dist/navigation-menu.d.ts +5 -7
  39. package/dist/navigation-menu.d.ts.map +1 -1
  40. package/dist/navigation-menu.js +82 -90
  41. package/dist/popover.cjs +1 -1
  42. package/dist/popover.d.ts.map +1 -1
  43. package/dist/popover.js +27 -26
  44. package/dist/select.cjs +2 -2
  45. package/dist/select.d.ts.map +1 -1
  46. package/dist/select.js +29 -35
  47. package/dist/table.cjs +1 -1
  48. package/dist/table.d.ts +22 -2
  49. package/dist/table.d.ts.map +1 -1
  50. package/dist/table.js +57 -43
  51. package/dist/tooltip.cjs +1 -1
  52. package/dist/tooltip.d.ts +21 -2
  53. package/dist/tooltip.d.ts.map +1 -1
  54. package/dist/tooltip.js +37 -34
  55. package/package.json +1 -1
  56. package/src/blocks/data-table.tsx +58 -4
  57. package/src/components/alert.tsx +9 -13
  58. package/src/components/context-menu.tsx +8 -3
  59. package/src/components/dropdown-menu.tsx +6 -15
  60. package/src/components/hover-card.tsx +6 -3
  61. package/src/components/loading.tsx +322 -0
  62. package/src/components/map.tsx +3 -1
  63. package/src/components/menubar.tsx +6 -5
  64. package/src/components/navigation-menu.tsx +80 -101
  65. package/src/components/popover.tsx +6 -2
  66. package/src/components/select.tsx +5 -8
  67. package/src/components/table.tsx +44 -7
  68. package/src/components/tooltip.tsx +36 -8
  69. package/src/index.ts +1 -0
  70. package/src/lib/utils.ts +3 -0
@@ -2,7 +2,10 @@
2
2
 
3
3
  import * as React from "react";
4
4
  import * as HoverCardPrimitive from "@radix-ui/react-hover-card";
5
- import { cn } from "@brightlocal/ui-components/lib/utils";
5
+ import {
6
+ cn,
7
+ FLOATING_CONTENT_ANIMATION,
8
+ } from "@brightlocal/ui-components/lib/utils";
6
9
 
7
10
  /**
8
11
  * HoverCard props
@@ -142,8 +145,8 @@ const HoverCardContent = React.forwardRef<
142
145
  ref={ref}
143
146
  align={align}
144
147
  className={cn(
145
- "border-border bg-popover text-popover-foreground z-50 w-80 rounded-lg border p-4 shadow-md outline-hidden",
146
- "data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0 data-[state=closed]:zoom-out-95 data-[state=open]:zoom-in-95 data-[side=bottom]:slide-in-from-top-2 data-[side=left]:slide-in-from-right-2 data-[side=right]:slide-in-from-left-2 data-[side=top]:slide-in-from-bottom-2",
148
+ "border-border bg-popover text-popover-foreground z-50 w-max max-w-sm rounded-lg border p-4 shadow-md outline-hidden",
149
+ FLOATING_CONTENT_ANIMATION,
147
150
  className
148
151
  )}
149
152
  data-hook={dataHook}
@@ -0,0 +1,322 @@
1
+ "use client";
2
+
3
+ import * as React from "react";
4
+ import { useEffect, useId, useMemo, useRef } from "react";
5
+ import { animate } from "framer-motion";
6
+
7
+ import { cn } from "@brightlocal/ui-components/lib/utils";
8
+ import { usePrefersReducedMotion } from "../hooks/use-prefers-reduced-motion";
9
+
10
+ // ---------------------------------------------------------------------------
11
+ // Shape paths (500×500 viewBox)
12
+ // ---------------------------------------------------------------------------
13
+
14
+ const CIRCLE_PATH =
15
+ "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";
16
+
17
+ const STAR_PATH =
18
+ "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";
19
+
20
+ const STAR_BURST =
21
+ "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";
22
+
23
+ /** Clamp value to [0, 1] — avoids repeated Math.min/max in hot loops */
24
+ const clamp01 = (v: number) => (v < 0 ? 0 : v > 1 ? 1 : v);
25
+
26
+ /**
27
+ * Default 6-shape morph sequence: circle → star → burst → circle → burst → star.
28
+ * The repeated circle acts as a natural "blink" beat between orderings.
29
+ */
30
+ const LOADING_SHAPES: string[] = [
31
+ CIRCLE_PATH,
32
+ STAR_PATH,
33
+ STAR_BURST,
34
+ CIRCLE_PATH,
35
+ STAR_BURST,
36
+ STAR_PATH,
37
+ ];
38
+
39
+ // ---------------------------------------------------------------------------
40
+ // Baked-in animation tuning
41
+ //
42
+ // These values are fixed, not consumer-configurable — the balance was tuned in
43
+ // the Lab playground (DS-770). Consumers get a single, opinionated loader.
44
+ // ---------------------------------------------------------------------------
45
+
46
+ /** Pause (ms) each shape holds before morphing to the next */
47
+ const HOLD_MS = 150;
48
+ /** Spring stiffness for the morph + rotation */
49
+ const STIFFNESS = 30;
50
+ /** Spring damping — higher = less oscillation */
51
+ const DAMPING = 9;
52
+ /** Spring mass — higher = more inertia */
53
+ const MASS = 0.5;
54
+ /** Degrees of rotation per morph cycle (clockwise) */
55
+ const ROTATION_DEG = 360;
56
+ /** Rendered size of the loader in pixels (width and height) */
57
+ const SIZE = 56;
58
+ /** Speed of the gradient angle drift */
59
+ const GRADIENT_WANDER_SPEED = 0.15;
60
+ /** How far the gradient endpoints swing from centre */
61
+ 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;
76
+
77
+ // ---------------------------------------------------------------------------
78
+ // Props
79
+ // ---------------------------------------------------------------------------
80
+
81
+ /**
82
+ * Props for the Loading component.
83
+ * Extends native `svg` props for full attribute + layout-className support.
84
+ */
85
+ export interface LoadingProps extends Omit<
86
+ React.ComponentPropsWithoutRef<"svg">,
87
+ "children"
88
+ > {
89
+ /**
90
+ * Required data-hook attribute for automated testing.
91
+ * @example "page-loading"
92
+ */
93
+ dataHook: string;
94
+ /**
95
+ * Accessible label for the loading animation, announced by screen readers.
96
+ * @default "Loading"
97
+ */
98
+ loadingLabel?: string;
99
+ /**
100
+ * Tracking element identifier for analytics.
101
+ */
102
+ trackingEl?: string;
103
+ /**
104
+ * Tracking label for analytics context.
105
+ */
106
+ trackingLabel?: string;
107
+ }
108
+
109
+ // ---------------------------------------------------------------------------
110
+ // Component
111
+ // ---------------------------------------------------------------------------
112
+
113
+ /**
114
+ * Brand loading animation — a gradient shape that morphs between a circle, a
115
+ * star, and a starburst while gently rotating, with a slowly wandering gradient.
116
+ *
117
+ * The animation is fully self-contained and opinionated: size, timing, spring
118
+ * physics and colours are all baked in. The only accessibility hook is an
119
+ * optional `loadingLabel`. Colours come from the `--loading-gradient-from` /
120
+ * `--loading-gradient-to` design tokens.
121
+ *
122
+ * Respects `prefers-reduced-motion`: renders a static shape when reduced motion
123
+ * is requested.
124
+ *
125
+ * @example
126
+ * ```tsx
127
+ * <Loading dataHook="page-loading" />
128
+ * <Loading dataHook="page-loading" loadingLabel={t("common.loading")} />
129
+ * ```
130
+ */
131
+ const Loading = React.forwardRef<SVGSVGElement, LoadingProps>(
132
+ (
133
+ { className, dataHook, loadingLabel, trackingEl, trackingLabel, ...props },
134
+ ref
135
+ ) => {
136
+ const reducedMotion = usePrefersReducedMotion();
137
+ const rawId = useId();
138
+ const gradientId = useMemo(
139
+ () => `loading-gradient-${rawId.replace(/:/g, "")}`,
140
+ [rawId]
141
+ );
142
+
143
+ const pathRef = useRef<SVGPathElement>(null);
144
+ const groupRef = useRef<SVGGElement>(null);
145
+ const gradientElRef = useRef<SVGLinearGradientElement>(null);
146
+
147
+ // -----------------------------------------------------------------------
148
+ // Slow wandering gradient direction
149
+ // -----------------------------------------------------------------------
150
+ useEffect(() => {
151
+ if (reducedMotion) return;
152
+ const grad = gradientElRef.current;
153
+ if (!grad) return;
154
+ const gradEl: SVGLinearGradientElement = grad;
155
+ let raf: number;
156
+ let start: number | null = null;
157
+ const sA =
158
+ GRADIENT_WANDER_SPEED * 0.8 +
159
+ Math.random() * GRADIENT_WANDER_SPEED * 0.4;
160
+ const sB =
161
+ GRADIENT_WANDER_SPEED * 0.53 +
162
+ Math.random() * GRADIENT_WANDER_SPEED * 0.27;
163
+ const oA = Math.random() * Math.PI * 2;
164
+ const oB = Math.random() * Math.PI * 2;
165
+ let prevAngle = -Infinity;
166
+ function tick(now: number) {
167
+ if (start === null) start = now;
168
+ const t = (now - start) / 1000;
169
+ const angle =
170
+ Math.sin(t * sA + oA) * Math.PI + Math.sin(t * sB + oB) * 0.5;
171
+ if (Math.abs(angle - prevAngle) > 0.01) {
172
+ prevAngle = angle;
173
+ const cos = Math.cos(angle);
174
+ const sin = Math.sin(angle);
175
+ gradEl.setAttribute("x1", `${50 + GRADIENT_WANDER_RADIUS * cos}%`);
176
+ gradEl.setAttribute("y1", `${50 + GRADIENT_WANDER_RADIUS * sin}%`);
177
+ gradEl.setAttribute("x2", `${50 - GRADIENT_WANDER_RADIUS * cos}%`);
178
+ gradEl.setAttribute("y2", `${50 - GRADIENT_WANDER_RADIUS * sin}%`);
179
+ }
180
+ raf = requestAnimationFrame(tick);
181
+ }
182
+ raf = requestAnimationFrame(tick);
183
+ return () => cancelAnimationFrame(raf);
184
+ }, [reducedMotion]);
185
+
186
+ // -----------------------------------------------------------------------
187
+ // Morph + rotation loop with holds between shapes
188
+ // -----------------------------------------------------------------------
189
+ useEffect(() => {
190
+ const path = pathRef.current;
191
+ const group = groupRef.current;
192
+ if (!path || !group) return;
193
+
194
+ if (reducedMotion) {
195
+ path.setAttribute("d", LOADING_SHAPES[0]!);
196
+ group.style.opacity = "1";
197
+ group.style.transform = "rotate(0deg)";
198
+ return;
199
+ }
200
+
201
+ const signal = { cancelled: false };
202
+
203
+ async function loop() {
204
+ // Lazy-load flubber so it doesn't block first paint
205
+ const { interpolate } = await import("flubber");
206
+ if (signal.cancelled) return;
207
+
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[][] = [];
212
+ 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]!
216
+ );
217
+ const frames: string[] = [];
218
+ for (let j = 0; j <= sampleCount; j++) {
219
+ frames.push(interp(j / sampleCount));
220
+ }
221
+ sampledFrames.push(frames);
222
+ }
223
+
224
+ group!.style.opacity = "1";
225
+ let step = 0;
226
+ let rotation = 0;
227
+
228
+ while (!signal.cancelled) {
229
+ const morphFrames = sampledFrames[step % LOADING_SHAPES.length]!;
230
+
231
+ // Hold on the current shape
232
+ await new Promise<void>((r) => setTimeout(r, HOLD_MS));
233
+ if (signal.cancelled) break;
234
+
235
+ const startRotation = rotation;
236
+ const targetRotation = rotation + ROTATION_DEG;
237
+
238
+ // Wrap animate so an in-flight spring is stopped on cleanup — without
239
+ // this, onUpdate keeps writing to detached DOM nodes if the component
240
+ // unmounts mid-morph (mirrors text-reveal-motion.tsx).
241
+ await new Promise<void>((resolve) => {
242
+ if (signal.cancelled) {
243
+ resolve();
244
+ return;
245
+ }
246
+ const controls = animate(0, 1, {
247
+ type: "spring",
248
+ stiffness: STIFFNESS,
249
+ damping: DAMPING,
250
+ mass: MASS,
251
+ onUpdate: (v: number) => {
252
+ if (signal.cancelled) {
253
+ controls.stop();
254
+ resolve();
255
+ return;
256
+ }
257
+ 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)`;
261
+ },
262
+ onComplete: () => resolve(),
263
+ });
264
+ });
265
+
266
+ rotation = targetRotation;
267
+ step = (step + 1) % LOADING_SHAPES.length;
268
+ }
269
+ }
270
+
271
+ loop();
272
+ return () => {
273
+ signal.cancelled = true;
274
+ };
275
+ }, [reducedMotion]);
276
+
277
+ return (
278
+ <svg
279
+ ref={ref}
280
+ aria-label={loadingLabel ?? "Loading"}
281
+ className={cn("shrink-0", className)}
282
+ data-hook={dataHook}
283
+ data-slot="loading"
284
+ data-tracking-el={trackingEl}
285
+ data-tracking-label={trackingLabel}
286
+ fill="none"
287
+ height={SIZE}
288
+ role="img"
289
+ style={{ overflow: "visible" }}
290
+ viewBox="-30 -30 560 560"
291
+ width={SIZE}
292
+ xmlns="http://www.w3.org/2000/svg"
293
+ {...props}
294
+ >
295
+ <defs>
296
+ <linearGradient ref={gradientElRef} id={gradientId}>
297
+ <stop offset="-20%" stopColor="var(--loading-gradient-from)" />
298
+ <stop offset="120%" stopColor="var(--loading-gradient-to)" />
299
+ </linearGradient>
300
+ </defs>
301
+ <g
302
+ ref={groupRef}
303
+ style={{
304
+ opacity: 0,
305
+ transformOrigin: "250px 250px",
306
+ willChange: "transform",
307
+ }}
308
+ >
309
+ <path
310
+ ref={pathRef}
311
+ d={LOADING_SHAPES[0]}
312
+ fill={`url(#${gradientId})`}
313
+ />
314
+ </g>
315
+ </svg>
316
+ );
317
+ }
318
+ );
319
+
320
+ Loading.displayName = "Loading";
321
+
322
+ export { Loading };
@@ -141,7 +141,9 @@ const MAP_STYLES: Record<
141
141
  // Shared dark mode store — one MutationObserver for all consumers
142
142
  const _darkModeListeners = new Set<() => void>();
143
143
  let _isDark =
144
- typeof document !== "undefined" && document.body.classList.contains("dark");
144
+ typeof document !== "undefined" &&
145
+ !!document.body &&
146
+ document.body.classList.contains("dark");
145
147
  let _darkModeObserverStarted = false;
146
148
 
147
149
  function _initDarkModeObserver() {
@@ -3,7 +3,10 @@ import * as React from "react";
3
3
  import * as MenubarPrimitive from "@radix-ui/react-menubar";
4
4
  import { Check, ChevronRight, Circle } from "@brightlocal/icons";
5
5
 
6
- import { cn } from "@brightlocal/ui-components/lib/utils";
6
+ import {
7
+ cn,
8
+ FLOATING_CONTENT_ANIMATION,
9
+ } from "@brightlocal/ui-components/lib/utils";
7
10
 
8
11
  /**
9
12
  * Menubar component - A horizontal menu bar with multiple dropdown menus
@@ -112,8 +115,7 @@ function MenubarContent({
112
115
  alignOffset={alignOffset}
113
116
  className={cn(
114
117
  "border-border bg-popover text-popover-foreground z-50 min-w-[12rem] origin-(--radix-menubar-content-transform-origin) rounded-md border p-1 shadow-md",
115
- "data-[state=open]:animate-in data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0 data-[state=closed]:zoom-out-95 data-[state=open]:zoom-in-95",
116
- "data-[side=bottom]:slide-in-from-top-2 data-[side=left]:slide-in-from-right-2 data-[side=right]:slide-in-from-left-2 data-[side=top]:slide-in-from-bottom-2",
118
+ FLOATING_CONTENT_ANIMATION,
117
119
  className
118
120
  )}
119
121
  data-slot="menubar-content"
@@ -407,8 +409,7 @@ function MenubarSubContent({
407
409
  <MenubarPrimitive.SubContent
408
410
  className={cn(
409
411
  "border-border bg-popover text-popover-foreground z-50 min-w-[8rem] origin-(--radix-menubar-content-transform-origin) rounded-md border p-1 shadow-lg",
410
- "data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0 data-[state=closed]:zoom-out-95 data-[state=open]:zoom-in-95",
411
- "data-[side=bottom]:slide-in-from-top-2 data-[side=left]:slide-in-from-right-2 data-[side=right]:slide-in-from-left-2 data-[side=top]:slide-in-from-bottom-2",
412
+ FLOATING_CONTENT_ANIMATION,
412
413
  className
413
414
  )}
414
415
  data-slot="menubar-sub-content"
@@ -92,11 +92,12 @@ type NavigationMenuTriggerProps = React.ComponentPropsWithoutRef<
92
92
  >;
93
93
 
94
94
  /**
95
- * Window (ms) after the menu opens during which a mouse click is treated as
96
- * instinctive and is NOT allowed to close the menu. Mirrors Base UI's
97
- * "patient click" threshold (PATIENT_CLICK_THRESHOLD).
95
+ * Window (ms) after the pointer enters an open trigger during which a mouse
96
+ * click is treated as instinctive and is NOT allowed to close the menu. Based
97
+ * on Base UI's "patient click" threshold (PATIENT_CLICK_THRESHOLD = 500), tuned
98
+ * slightly shorter so the trigger becomes a deliberate toggle a touch sooner.
98
99
  */
99
- const PATIENT_CLICK_THRESHOLD_MS = 500;
100
+ const PATIENT_CLICK_THRESHOLD_MS = 400;
100
101
 
101
102
  /**
102
103
  * Trigger button that opens a dropdown menu
@@ -107,109 +108,87 @@ const PATIENT_CLICK_THRESHOLD_MS = 500;
107
108
  * closes it immediately (see radix-ui/primitives#1630).
108
109
  *
109
110
  * We port Base UI's "patient click" behaviour: an instinctive mouse click made
110
- * within {@link PATIENT_CLICK_THRESHOLD_MS} of the menu opening keeps it open
111
- * (calling `preventDefault()` makes Radix's internal `composeEventHandlers`
112
- * skip its toggle). The window is measured from the `data-state` open
113
- * transition — not the pointer enter — so it stays correct no matter how large
114
- * `delayDuration` is. A deliberate click after that window still toggles the
115
- * menu shut, and touch taps and keyboard activation are left untouched so they
116
- * can toggle it too.
111
+ * within {@link PATIENT_CLICK_THRESHOLD_MS} of the pointer entering an open
112
+ * trigger keeps the menu open (calling `preventDefault()` makes Radix's internal
113
+ * `composeEventHandlers` skip its toggle). A deliberate click after that window
114
+ * still toggles the menu shut, and touch taps and keyboard activation are left
115
+ * untouched so they can toggle it too.
117
116
  */
118
117
  const NavigationMenuTrigger = React.forwardRef<
119
118
  React.ElementRef<typeof NavigationMenuPrimitive.Trigger>,
120
119
  NavigationMenuTriggerProps
121
- >(({ className, children, onClick, onPointerDown, ...props }, ref) => {
122
- // Pointer type of the interaction, so the keep-open behaviour only applies to
123
- // mouse (the only input that hover-opens before a click).
124
- const lastPointerTypeRef = React.useRef<string>("");
125
- // Timestamp (event clock) of the last open transition, used to measure whether
126
- // a click is "instinctive" (right after the menu opened) or "patient"
127
- // (deliberate). Measuring from the open — not the pointer enter — keeps the
128
- // window correct regardless of `delayDuration`. Starts at -Infinity so a click
129
- // on a trigger that was already open at mount (no observed transition) is never
130
- // misclassified as instinctive.
131
- const openedAtRef = React.useRef(-Infinity);
132
- const observerRef = React.useRef<MutationObserver | null>(null);
120
+ >(
121
+ (
122
+ { className, children, onClick, onPointerDown, onPointerEnter, ...props },
123
+ ref
124
+ ) => {
125
+ // Pointer type of the interaction, so the keep-open behaviour only applies
126
+ // to mouse (the only input that hover-opens before a click).
127
+ const lastPointerTypeRef = React.useRef<string>("");
128
+ // Timestamp of the last pointer enter, used to measure whether a click is
129
+ // "instinctive" (right after hover) or "patient" (deliberate). Captured from
130
+ // the SAME `event.timeStamp` clock the click uses so the subtraction is
131
+ // valid — never compare against `performance.now()`, which is a different
132
+ // origin and makes every click read as "patient" (i.e. always closes).
133
+ const pointerEnterAtRef = React.useRef(0);
133
134
 
134
- // Merge the forwarded ref with a `data-state` observer that stamps the open
135
- // time. A callback ref (stable via useCallback) attaches once on mount and
136
- // tears the observer down on unmount.
137
- const composedRef = React.useCallback(
138
- (node: React.ElementRef<typeof NavigationMenuPrimitive.Trigger> | null) => {
139
- if (typeof ref === "function") {
140
- ref(node);
141
- } else if (ref) {
142
- ref.current = node;
143
- }
144
-
145
- observerRef.current?.disconnect();
146
- if (node) {
147
- const observer = new MutationObserver(() => {
148
- if (node.getAttribute("data-state") === "open") {
149
- openedAtRef.current = performance.now();
135
+ return (
136
+ <NavigationMenuPrimitive.Trigger
137
+ ref={ref}
138
+ className={cn(
139
+ [
140
+ "group inline-flex h-9 w-max items-center justify-center gap-1",
141
+ "rounded-md px-4 py-2",
142
+ "text-sm font-medium",
143
+ "text-foreground",
144
+ "transition-colors",
145
+ "hover:bg-accent hover:text-accent-foreground",
146
+ "focus:bg-accent focus:text-accent-foreground",
147
+ "focus-visible:bg-accent focus-visible:ring-ring focus-visible:shadow-[0_0_0_4px_rgba(163,163,163,0.1)] focus-visible:ring-1 focus-visible:outline-none",
148
+ "disabled:pointer-events-none disabled:opacity-50",
149
+ "data-[state=open]:bg-accent/50",
150
+ ],
151
+ className
152
+ )}
153
+ data-slot="navigation-menu-trigger"
154
+ onClick={(event) => {
155
+ // `data-state` is "open" only when hover already opened this trigger.
156
+ // `event.detail > 0` excludes keyboard-synthesised clicks (detail === 0).
157
+ const isOpen =
158
+ event.currentTarget.getAttribute("data-state") === "open";
159
+ const isInstinctiveClick =
160
+ event.timeStamp - pointerEnterAtRef.current <
161
+ PATIENT_CLICK_THRESHOLD_MS;
162
+ if (
163
+ lastPointerTypeRef.current === "mouse" &&
164
+ event.detail > 0 &&
165
+ isOpen &&
166
+ isInstinctiveClick
167
+ ) {
168
+ event.preventDefault();
150
169
  }
151
- });
152
- observer.observe(node, {
153
- attributes: true,
154
- attributeFilter: ["data-state"],
155
- });
156
- observerRef.current = observer;
157
- }
158
- },
159
- [ref]
160
- );
161
-
162
- return (
163
- <NavigationMenuPrimitive.Trigger
164
- ref={composedRef}
165
- className={cn(
166
- [
167
- "group inline-flex h-9 w-max items-center justify-center gap-1",
168
- "rounded-md px-4 py-2",
169
- "text-sm font-medium",
170
- "text-foreground",
171
- "transition-colors",
172
- "hover:bg-accent hover:text-accent-foreground",
173
- "focus:bg-accent focus:text-accent-foreground",
174
- "focus-visible:bg-accent focus-visible:ring-ring focus-visible:shadow-[0_0_0_4px_rgba(163,163,163,0.1)] focus-visible:ring-1 focus-visible:outline-none",
175
- "disabled:pointer-events-none disabled:opacity-50",
176
- "data-[state=open]:bg-accent/50",
177
- ],
178
- className
179
- )}
180
- data-slot="navigation-menu-trigger"
181
- onClick={(event) => {
182
- // `data-state` is "open" only when hover already opened this trigger.
183
- // `event.detail > 0` excludes keyboard-synthesised clicks (detail === 0).
184
- const isOpen =
185
- event.currentTarget.getAttribute("data-state") === "open";
186
- const isInstinctiveClick =
187
- event.timeStamp - openedAtRef.current < PATIENT_CLICK_THRESHOLD_MS;
188
- if (
189
- lastPointerTypeRef.current === "mouse" &&
190
- event.detail > 0 &&
191
- isOpen &&
192
- isInstinctiveClick
193
- ) {
194
- event.preventDefault();
195
- }
196
- onClick?.(event);
197
- }}
198
- onPointerDown={(event) => {
199
- lastPointerTypeRef.current = event.pointerType;
200
- onPointerDown?.(event);
201
- }}
202
- {...props}
203
- >
204
- {children}
205
- <ChevronDown
206
- aria-hidden="true"
207
- className="relative top-[1px] ml-1 transition duration-300 group-data-[state=open]:rotate-180"
208
- size={12}
209
- />
210
- </NavigationMenuPrimitive.Trigger>
211
- );
212
- });
170
+ onClick?.(event);
171
+ }}
172
+ onPointerDown={(event) => {
173
+ lastPointerTypeRef.current = event.pointerType;
174
+ onPointerDown?.(event);
175
+ }}
176
+ onPointerEnter={(event) => {
177
+ pointerEnterAtRef.current = event.timeStamp;
178
+ onPointerEnter?.(event);
179
+ }}
180
+ {...props}
181
+ >
182
+ {children}
183
+ <ChevronDown
184
+ aria-hidden="true"
185
+ className="relative top-[1px] ml-1 transition duration-300 group-data-[state=open]:rotate-180"
186
+ size={12}
187
+ />
188
+ </NavigationMenuPrimitive.Trigger>
189
+ );
190
+ }
191
+ );
213
192
 
214
193
  NavigationMenuTrigger.displayName = "NavigationMenuTrigger";
215
194
 
@@ -3,7 +3,10 @@
3
3
  import * as React from "react";
4
4
  import * as PopoverPrimitive from "@radix-ui/react-popover";
5
5
 
6
- import { cn } from "@brightlocal/ui-components/lib/utils";
6
+ import {
7
+ cn,
8
+ FLOATING_CONTENT_ANIMATION,
9
+ } from "@brightlocal/ui-components/lib/utils";
7
10
  import { useOverlayPointerLock } from "@brightlocal/ui-components/hooks/use-overlay-pointer-lock";
8
11
 
9
12
  export interface PopoverProps extends React.ComponentProps<
@@ -93,7 +96,8 @@ const PopoverContent = React.forwardRef<
93
96
  ref={ref}
94
97
  align={align}
95
98
  className={cn(
96
- "bg-popover text-popover-foreground data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0 data-[state=closed]:zoom-out-95 data-[state=open]:zoom-in-95 data-[side=bottom]:slide-in-from-top-2 data-[side=left]:slide-in-from-right-2 data-[side=right]:slide-in-from-left-2 data-[side=top]:slide-in-from-bottom-2 z-50 w-72 origin-(--radix-popover-content-transform-origin) rounded-md border p-4 shadow-md outline-hidden",
99
+ "bg-popover text-popover-foreground z-50 w-72 origin-(--radix-popover-content-transform-origin) rounded-md border p-4 shadow-md outline-hidden",
100
+ FLOATING_CONTENT_ANIMATION,
97
101
  className
98
102
  )}
99
103
  data-hook={dataHook}
@@ -5,7 +5,10 @@ import * as SelectPrimitive from "@radix-ui/react-select";
5
5
  import { Check, ChevronDown, ChevronUp } from "@brightlocal/icons";
6
6
  import { cva } from "class-variance-authority";
7
7
 
8
- import { cn } from "@brightlocal/ui-components/lib/utils";
8
+ import {
9
+ cn,
10
+ FLOATING_CONTENT_ANIMATION,
11
+ } from "@brightlocal/ui-components/lib/utils";
9
12
  import { useDevWarning } from "../lib/use-dev-warning";
10
13
  import { useOverlayPointerLock } from "@brightlocal/ui-components/hooks/use-overlay-pointer-lock";
11
14
 
@@ -237,13 +240,7 @@ function SelectContent({
237
240
  "origin-[var(--radix-select-content-transform-origin)]",
238
241
 
239
242
  // Animations
240
- "data-[state=open]:animate-in data-[state=closed]:animate-out",
241
- "data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0",
242
- "data-[state=closed]:zoom-out-95 data-[state=open]:zoom-in-95",
243
- "data-[side=bottom]:slide-in-from-top-2",
244
- "data-[side=left]:slide-in-from-right-2",
245
- "data-[side=right]:slide-in-from-left-2",
246
- "data-[side=top]:slide-in-from-bottom-2"
243
+ FLOATING_CONTENT_ANIMATION
247
244
  );
248
245
 
249
246
  const popperStyles =