@brightlocal/ui-components 1.2.1 → 1.2.3

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.
@@ -330,8 +330,8 @@ export interface ComboboxContentProps {
330
330
  children: React.ReactNode;
331
331
  /** Alignment of the popover relative to trigger */
332
332
  align?: "start" | "center" | "end";
333
- /** Which side of the trigger to render the popover. @default "bottom" */
334
- side?: "top" | "right" | "bottom" | "left";
333
+ /** Which side of the trigger to render the popover. Only vertical positioning is supported — the dropdown spans the full trigger width. @default "bottom" */
334
+ side?: "top" | "bottom";
335
335
  /** Distance in px between the trigger and the popover. @default 4 */
336
336
  sideOffset?: number;
337
337
  /**
@@ -358,7 +358,103 @@ function ComboboxContent({
358
358
  avoidCollisions,
359
359
  hideList = false,
360
360
  }: ComboboxContentProps) {
361
- const { shouldFilter, itemToStringValue } = useComboboxContext();
361
+ const { open, shouldFilter, itemToStringValue } = useComboboxContext();
362
+
363
+ // TODO: This is a workaround for floating-ui re-flipping on content resize.
364
+ // The root cause is that Radix Popper's autoUpdate watches elementResize and
365
+ // re-runs the full middleware pipeline (including flip) every time the list
366
+ // shrinks during filtering. Ideally this would be solved at a lower level:
367
+ //
368
+ // 1. Radix exposes `updatePositionStrategy` but not fine-grained autoUpdate
369
+ // options (e.g. disabling `elementResize` or `layoutShift` listeners).
370
+ // If Radix adds this, we can disable elementResize and remove this hack.
371
+ // Track: https://github.com/radix-ui/primitives/issues
372
+ //
373
+ // 2. Build a base-ui / headless Combobox with floating-ui directly, where
374
+ // we control the autoUpdate config and middleware pipeline. This would
375
+ // let us disable flip after initial placement or use a custom middleware
376
+ // that only flips on scroll/viewport changes, not content resize.
377
+ //
378
+ // 3. Use Radix's `updatePositionStrategy="optimized"` — but this only
379
+ // throttles updates, it doesn't prevent flip on resize specifically.
380
+ //
381
+ // Current workaround: lock the resolved `data-side` after initial open via
382
+ // MutationObserver + double-rAF, then force that side on subsequent renders.
383
+ // Works but adds complexity. Remove when a proper upstream fix is available.
384
+ //
385
+ // Lock the resolved side after initial open so that content resizes
386
+ // (e.g. filtering the list) don't cause the dropdown to flip.
387
+ // Scroll repositioning and shift still work because avoidCollisions
388
+ // stays enabled — only the preferred `side` is locked.
389
+ const [lockedSide, setLockedSide] = React.useState<"top" | "bottom" | null>(
390
+ null
391
+ );
392
+ const cleanupRef = React.useRef<(() => void) | null>(null);
393
+
394
+ // When the popover content mounts, detect the resolved side via two
395
+ // complementary strategies and lock it:
396
+ // 1. MutationObserver — catches the flip (data-side changes from the
397
+ // initial preferred side to the resolved side after computePosition).
398
+ // 2. Double rAF fallback — if no flip occurs, data-side never changes,
399
+ // so we read it after two animation frames when positioning is done.
400
+ // Note: safe under React StrictMode — the triple ref-callback invocation
401
+ // (node → null → node) is handled by the cleanup/done guard pattern.
402
+ const handleContentRef = React.useCallback((node: HTMLDivElement | null) => {
403
+ // Cleanup previous observer/timers
404
+ cleanupRef.current?.();
405
+ cleanupRef.current = null;
406
+
407
+ if (!node) return;
408
+
409
+ let done = false;
410
+ const lock = (attr: string | null) => {
411
+ if (done) return;
412
+ if (attr === "top" || attr === "bottom") {
413
+ done = true;
414
+ observer.disconnect();
415
+ setLockedSide(attr);
416
+ }
417
+ };
418
+
419
+ // Strategy 1: observe data-side attribute mutations (catches flip)
420
+ const observer = new MutationObserver(() => {
421
+ lock(node.getAttribute("data-side"));
422
+ });
423
+ observer.observe(node, {
424
+ attributes: true,
425
+ attributeFilter: ["data-side"],
426
+ });
427
+
428
+ // Strategy 2: double rAF fallback (catches no-flip case where
429
+ // data-side is set during initial render and never changes)
430
+ let rafOuter = 0;
431
+ let rafInner = 0;
432
+ rafOuter = requestAnimationFrame(() => {
433
+ rafInner = requestAnimationFrame(() => {
434
+ lock(node.getAttribute("data-side"));
435
+ });
436
+ });
437
+
438
+ cleanupRef.current = () => {
439
+ done = true;
440
+ observer.disconnect();
441
+ cancelAnimationFrame(rafOuter);
442
+ cancelAnimationFrame(rafInner);
443
+ };
444
+ }, []);
445
+
446
+ // Cleanup observers on close, but delay the lockedSide reset until
447
+ // the exit animation finishes (150ms) so the side prop stays locked
448
+ // and the dropdown doesn't visibly flip during the fade-out/zoom-out.
449
+ React.useEffect(() => {
450
+ if (!open) {
451
+ cleanupRef.current?.();
452
+ cleanupRef.current = null;
453
+ const timer = setTimeout(() => setLockedSide(null), 150);
454
+ return () => clearTimeout(timer);
455
+ }
456
+ return undefined;
457
+ }, [open]);
362
458
 
363
459
  const filter = React.useMemo(() => {
364
460
  if (!itemToStringValue) return undefined;
@@ -368,12 +464,13 @@ function ComboboxContent({
368
464
 
369
465
  return (
370
466
  <PopoverContent
467
+ ref={handleContentRef}
371
468
  align={align}
372
469
  aria-label="Options"
373
470
  avoidCollisions={avoidCollisions}
374
471
  className={cn("w-[var(--radix-popover-trigger-width)] p-0", className)}
375
472
  data-slot="combobox-popover"
376
- side={side}
473
+ side={lockedSide ?? side}
377
474
  sideOffset={sideOffset}
378
475
  >
379
476
  <Command