@dloizides/ui-layout 1.9.0 → 1.10.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/dist/index.js CHANGED
@@ -100,10 +100,7 @@ var SEGMENT_PAD_V = 7;
100
100
  var SEGMENT_FONT = 14;
101
101
  var SEGMENT_WEIGHT = "600";
102
102
  var BORDER_WIDTH = 1;
103
- var SHADOW_COLOR = "#141e32";
104
- var SHADOW_OPACITY = 0.1;
105
- var SHADOW_RADIUS = 3;
106
- var SHADOW_OFFSET = { width: 0, height: 1 };
103
+ var SEGMENT_BOX_SHADOW = "0px 1px 3px rgba(20, 30, 50, 0.1)";
107
104
  var SHADOW_ELEVATION = 2;
108
105
  var TRANSPARENT = "transparent";
109
106
  var styles4 = reactNative.StyleSheet.create({
@@ -122,10 +119,7 @@ var styles4 = reactNative.StyleSheet.create({
122
119
  borderRadius: SEGMENT_RADIUS
123
120
  },
124
121
  segmentSelected: {
125
- shadowColor: SHADOW_COLOR,
126
- shadowOpacity: SHADOW_OPACITY,
127
- shadowRadius: SHADOW_RADIUS,
128
- shadowOffset: SHADOW_OFFSET,
122
+ boxShadow: SEGMENT_BOX_SHADOW,
129
123
  elevation: SHADOW_ELEVATION
130
124
  },
131
125
  label: {
@@ -343,6 +337,14 @@ function buildPortalPopoverStyle(rect, minWidth = 0) {
343
337
  function buildAnchorStackStyle(isMenuOpen) {
344
338
  return isMenuOpen ? { zIndex: ANCHOR_OPEN_Z_INDEX, elevation: MENU_ELEVATION } : null;
345
339
  }
340
+ function isAnchorOutOfView(rect, viewportHeight, viewportWidth) {
341
+ const hasLayout = rect.width > 0 && rect.bottom !== rect.top;
342
+ const hasViewport = viewportHeight > 0 && viewportWidth > 0;
343
+ if (!hasLayout || !hasViewport) return false;
344
+ const isAboveOrBelow = rect.bottom <= 0 || rect.top >= viewportHeight;
345
+ const isLeftOrRight = rect.left + rect.width <= 0 || rect.left >= viewportWidth;
346
+ return isAboveOrBelow || isLeftOrRight;
347
+ }
346
348
  var OPTION_BORDER_RADIUS = 4;
347
349
  var BODY_FONT_SIZE = 14;
348
350
  var SELECTED_FONT_WEIGHT = "600";
@@ -387,6 +389,57 @@ var OptionRow = ({
387
389
  }
388
390
  );
389
391
  };
392
+ var IS_WEB = reactNative.Platform.OS === "web";
393
+ function toDomNode(containerRef) {
394
+ const node = containerRef.current;
395
+ if (node === null || typeof node.getBoundingClientRect !== "function") return null;
396
+ return node;
397
+ }
398
+ function readAnchorRect(containerRef) {
399
+ if (!IS_WEB) return null;
400
+ const node = toDomNode(containerRef);
401
+ if (node === null) return null;
402
+ const r = node.getBoundingClientRect();
403
+ return { top: r.top, left: r.left, width: r.width, bottom: r.bottom };
404
+ }
405
+ function observeLayout(node, onChange) {
406
+ if (typeof ResizeObserver !== "function") return null;
407
+ const observer = new ResizeObserver(onChange);
408
+ if (node !== null) observer.observe(node);
409
+ observer.observe(document.body);
410
+ return observer;
411
+ }
412
+ function useAnchorTracking(containerRef, onOutOfView) {
413
+ const [rect, setRect] = React8.useState(() => readAnchorRect(containerRef));
414
+ React8.useLayoutEffect(() => {
415
+ if (!IS_WEB) return void 0;
416
+ let frame = null;
417
+ let isDisposed = false;
418
+ const apply = () => {
419
+ frame = null;
420
+ if (isDisposed) return;
421
+ const next = readAnchorRect(containerRef);
422
+ setRect(next);
423
+ if (next !== null && isAnchorOutOfView(next, window.innerHeight, window.innerWidth)) onOutOfView();
424
+ };
425
+ const schedule = () => {
426
+ if (frame !== null) return;
427
+ frame = window.requestAnimationFrame(apply);
428
+ };
429
+ apply();
430
+ document.addEventListener("scroll", schedule, true);
431
+ window.addEventListener("resize", schedule);
432
+ const observer = observeLayout(toDomNode(containerRef), schedule);
433
+ return () => {
434
+ isDisposed = true;
435
+ if (frame !== null) window.cancelAnimationFrame(frame);
436
+ document.removeEventListener("scroll", schedule, true);
437
+ window.removeEventListener("resize", schedule);
438
+ if (observer !== null) observer.disconnect();
439
+ };
440
+ }, [containerRef, onOutOfView]);
441
+ return rect;
442
+ }
390
443
  function refContains(ref, target) {
391
444
  if (ref === void 0) return false;
392
445
  const node = ref.current;
@@ -397,32 +450,36 @@ var clampIndex = (index, itemCount) => {
397
450
  const lastIndex = itemCount - 1;
398
451
  return index > lastIndex ? lastIndex : index;
399
452
  };
453
+ function claimKey(event) {
454
+ event.preventDefault();
455
+ event.stopPropagation();
456
+ }
400
457
  function buildKeyHandler(params) {
401
458
  const { itemCount, onHighlightChange, onSelectHighlighted, onClose } = params;
402
459
  return (event) => {
403
460
  switch (event.key) {
404
461
  case "ArrowDown":
405
- event.preventDefault();
462
+ claimKey(event);
406
463
  onHighlightChange((prev) => clampIndex(prev + 1, itemCount));
407
464
  return;
408
465
  case "ArrowUp":
409
- event.preventDefault();
466
+ claimKey(event);
410
467
  onHighlightChange((prev) => clampIndex(prev - 1, itemCount));
411
468
  return;
412
469
  case "Home":
413
- event.preventDefault();
470
+ claimKey(event);
414
471
  onHighlightChange(() => 0);
415
472
  return;
416
473
  case "End":
417
- event.preventDefault();
474
+ claimKey(event);
418
475
  onHighlightChange(() => clampIndex(itemCount - 1, itemCount));
419
476
  return;
420
477
  case "Enter":
421
- event.preventDefault();
478
+ claimKey(event);
422
479
  onSelectHighlighted();
423
480
  return;
424
481
  case "Escape":
425
- event.preventDefault();
482
+ claimKey(event);
426
483
  onClose();
427
484
  return;
428
485
  }
@@ -439,15 +496,15 @@ function useMenuKeyboard(params) {
439
496
  const insideMenu = refContains(menuRef, target);
440
497
  if (!insideAnchor && !insideMenu) onClose();
441
498
  };
442
- document.addEventListener("keydown", onKeyDown);
499
+ document.addEventListener("keydown", onKeyDown, true);
443
500
  document.addEventListener("mousedown", onMouseDown);
444
501
  return () => {
445
- document.removeEventListener("keydown", onKeyDown);
502
+ document.removeEventListener("keydown", onKeyDown, true);
446
503
  document.removeEventListener("mousedown", onMouseDown);
447
504
  };
448
505
  }, [containerRef, menuRef, enabled, onClose, params]);
449
506
  }
450
- var IS_WEB = reactNative.Platform.OS === "web";
507
+ var IS_WEB2 = reactNative.Platform.OS === "web";
451
508
  var BORDER_RADIUS2 = 8;
452
509
  var BORDER_WIDTH3 = 1;
453
510
  var MENU_PADDING2 = 4;
@@ -468,30 +525,6 @@ var styles7 = reactNative.StyleSheet.create({
468
525
  elevation: MENU_ELEVATION
469
526
  }
470
527
  });
471
- function readAnchorRect(containerRef) {
472
- if (!IS_WEB) return null;
473
- const node = containerRef.current;
474
- if (node === null || typeof node.getBoundingClientRect !== "function") return null;
475
- const r = node.getBoundingClientRect();
476
- return { top: r.top, left: r.left, width: r.width, bottom: r.bottom };
477
- }
478
- function useAnchorRect(containerRef) {
479
- const [rect, setRect] = React8.useState(() => readAnchorRect(containerRef));
480
- React8.useLayoutEffect(() => {
481
- if (!IS_WEB) return void 0;
482
- const measure = () => {
483
- setRect(readAnchorRect(containerRef));
484
- };
485
- measure();
486
- window.addEventListener("scroll", measure, true);
487
- window.addEventListener("resize", measure);
488
- return () => {
489
- window.removeEventListener("scroll", measure, true);
490
- window.removeEventListener("resize", measure);
491
- };
492
- }, [containerRef]);
493
- return rect;
494
- }
495
528
  var InlineMenu = ({
496
529
  testID,
497
530
  accessibilityLabel,
@@ -506,7 +539,7 @@ var InlineMenu = ({
506
539
  const { theme } = uiFeedback.useUi();
507
540
  const { colors } = theme;
508
541
  const menuRef = React8.useRef(null);
509
- const rect = useAnchorRect(containerRef);
542
+ const rect = useAnchorTracking(containerRef, onClose);
510
543
  const selectedIndex = React8.useMemo(
511
544
  () => Math.max(0, options.findIndex((opt) => opt.value === value)),
512
545
  [options, value]
@@ -536,7 +569,7 @@ var InlineMenu = ({
536
569
  });
537
570
  const popoverStyle = React8.useMemo(() => {
538
571
  const themed = { borderColor: colors.border, backgroundColor: colors.surface };
539
- if (IS_WEB)
572
+ if (IS_WEB2)
540
573
  return [
541
574
  buildPortalPopoverStyle(rect ?? { left: 0, width: 0, bottom: 0 }, menuMinWidth),
542
575
  themed
@@ -564,7 +597,7 @@ var InlineMenu = ({
564
597
  )) })
565
598
  }
566
599
  );
567
- if (IS_WEB) return reactDom.createPortal(menu, document.body);
600
+ if (IS_WEB2) return reactDom.createPortal(menu, document.body);
568
601
  return menu;
569
602
  };
570
603
  var MENU_BREAKPOINT = 768;
@@ -578,6 +611,7 @@ function useResolvedDropdownVariant(explicit) {
578
611
  const { width } = reactNative.useWindowDimensions();
579
612
  return resolveDropdownVariant(explicit, width, reactNative.Platform.OS);
580
613
  }
614
+ var IS_WEB3 = reactNative.Platform.OS === "web";
581
615
  var BORDER_RADIUS3 = 8;
582
616
  var BORDER_WIDTH4 = 1;
583
617
  var BODY_FONT_SIZE2 = 14;
@@ -586,8 +620,21 @@ var MODAL_MIN_WIDTH = 200;
586
620
  var MODAL_MAX_HEIGHT = 300;
587
621
  var CONTAINER_PADDING_H = 12;
588
622
  var CONTAINER_PADDING_V = 10;
623
+ var SR_ONLY_SIZE = 1;
624
+ var SR_ONLY_OFFSET = -1;
625
+ function buildHintProps(hint, hintId) {
626
+ return IS_WEB3 ? { "aria-describedby": hintId } : { accessibilityHint: hint };
627
+ }
589
628
  var styles8 = reactNative.StyleSheet.create({
590
629
  anchor: { position: "relative" },
630
+ srOnly: {
631
+ position: "absolute",
632
+ width: SR_ONLY_SIZE,
633
+ height: SR_ONLY_SIZE,
634
+ margin: SR_ONLY_OFFSET,
635
+ padding: 0,
636
+ overflow: "hidden"
637
+ },
591
638
  container: {
592
639
  borderWidth: BORDER_WIDTH4,
593
640
  borderRadius: BORDER_RADIUS3,
@@ -625,6 +672,7 @@ var ModalDropdown = ({
625
672
  const resolvedVariant = useResolvedDropdownVariant(variant);
626
673
  const isMenu = resolvedVariant === "menu" /* Menu */;
627
674
  const [isOpen, setIsOpen] = React8.useState(false);
675
+ const hintId = `${testID}-hint`;
628
676
  const anchorRef = React8.useRef(null);
629
677
  const dialogRef = React8.useRef(null);
630
678
  useFocusTrap(dialogRef, isOpen && !isMenu);
@@ -679,7 +727,7 @@ var ModalDropdown = ({
679
727
  reactNative.TouchableOpacity,
680
728
  {
681
729
  accessible: true,
682
- accessibilityHint,
730
+ ...buildHintProps(accessibilityHint, hintId),
683
731
  accessibilityLabel,
684
732
  accessibilityRole: "button",
685
733
  accessibilityState: { expanded: isOpen },
@@ -690,6 +738,7 @@ var ModalDropdown = ({
690
738
  children: renderTrigger !== void 0 ? renderTrigger({ label: selectedLabel, isOpen }) : /* @__PURE__ */ jsxRuntime.jsx(reactNative.Text, { style: [styles8.selectedText, { color: colors.text }], children: selectedLabel })
691
739
  }
692
740
  ),
741
+ IS_WEB3 ? /* @__PURE__ */ jsxRuntime.jsx(reactNative.Text, { nativeID: hintId, style: styles8.srOnly, testID: `${testID}-hint`, children: accessibilityHint }) : null,
693
742
  isMenu && isOpen ? /* @__PURE__ */ jsxRuntime.jsx(
694
743
  InlineMenu,
695
744
  {