@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.mjs CHANGED
@@ -94,10 +94,7 @@ var SEGMENT_PAD_V = 7;
94
94
  var SEGMENT_FONT = 14;
95
95
  var SEGMENT_WEIGHT = "600";
96
96
  var BORDER_WIDTH = 1;
97
- var SHADOW_COLOR = "#141e32";
98
- var SHADOW_OPACITY = 0.1;
99
- var SHADOW_RADIUS = 3;
100
- var SHADOW_OFFSET = { width: 0, height: 1 };
97
+ var SEGMENT_BOX_SHADOW = "0px 1px 3px rgba(20, 30, 50, 0.1)";
101
98
  var SHADOW_ELEVATION = 2;
102
99
  var TRANSPARENT = "transparent";
103
100
  var styles4 = StyleSheet.create({
@@ -116,10 +113,7 @@ var styles4 = StyleSheet.create({
116
113
  borderRadius: SEGMENT_RADIUS
117
114
  },
118
115
  segmentSelected: {
119
- shadowColor: SHADOW_COLOR,
120
- shadowOpacity: SHADOW_OPACITY,
121
- shadowRadius: SHADOW_RADIUS,
122
- shadowOffset: SHADOW_OFFSET,
116
+ boxShadow: SEGMENT_BOX_SHADOW,
123
117
  elevation: SHADOW_ELEVATION
124
118
  },
125
119
  label: {
@@ -337,6 +331,14 @@ function buildPortalPopoverStyle(rect, minWidth = 0) {
337
331
  function buildAnchorStackStyle(isMenuOpen) {
338
332
  return isMenuOpen ? { zIndex: ANCHOR_OPEN_Z_INDEX, elevation: MENU_ELEVATION } : null;
339
333
  }
334
+ function isAnchorOutOfView(rect, viewportHeight, viewportWidth) {
335
+ const hasLayout = rect.width > 0 && rect.bottom !== rect.top;
336
+ const hasViewport = viewportHeight > 0 && viewportWidth > 0;
337
+ if (!hasLayout || !hasViewport) return false;
338
+ const isAboveOrBelow = rect.bottom <= 0 || rect.top >= viewportHeight;
339
+ const isLeftOrRight = rect.left + rect.width <= 0 || rect.left >= viewportWidth;
340
+ return isAboveOrBelow || isLeftOrRight;
341
+ }
340
342
  var OPTION_BORDER_RADIUS = 4;
341
343
  var BODY_FONT_SIZE = 14;
342
344
  var SELECTED_FONT_WEIGHT = "600";
@@ -381,6 +383,57 @@ var OptionRow = ({
381
383
  }
382
384
  );
383
385
  };
386
+ var IS_WEB = Platform.OS === "web";
387
+ function toDomNode(containerRef) {
388
+ const node = containerRef.current;
389
+ if (node === null || typeof node.getBoundingClientRect !== "function") return null;
390
+ return node;
391
+ }
392
+ function readAnchorRect(containerRef) {
393
+ if (!IS_WEB) return null;
394
+ const node = toDomNode(containerRef);
395
+ if (node === null) return null;
396
+ const r = node.getBoundingClientRect();
397
+ return { top: r.top, left: r.left, width: r.width, bottom: r.bottom };
398
+ }
399
+ function observeLayout(node, onChange) {
400
+ if (typeof ResizeObserver !== "function") return null;
401
+ const observer = new ResizeObserver(onChange);
402
+ if (node !== null) observer.observe(node);
403
+ observer.observe(document.body);
404
+ return observer;
405
+ }
406
+ function useAnchorTracking(containerRef, onOutOfView) {
407
+ const [rect, setRect] = useState(() => readAnchorRect(containerRef));
408
+ useLayoutEffect(() => {
409
+ if (!IS_WEB) return void 0;
410
+ let frame = null;
411
+ let isDisposed = false;
412
+ const apply = () => {
413
+ frame = null;
414
+ if (isDisposed) return;
415
+ const next = readAnchorRect(containerRef);
416
+ setRect(next);
417
+ if (next !== null && isAnchorOutOfView(next, window.innerHeight, window.innerWidth)) onOutOfView();
418
+ };
419
+ const schedule = () => {
420
+ if (frame !== null) return;
421
+ frame = window.requestAnimationFrame(apply);
422
+ };
423
+ apply();
424
+ document.addEventListener("scroll", schedule, true);
425
+ window.addEventListener("resize", schedule);
426
+ const observer = observeLayout(toDomNode(containerRef), schedule);
427
+ return () => {
428
+ isDisposed = true;
429
+ if (frame !== null) window.cancelAnimationFrame(frame);
430
+ document.removeEventListener("scroll", schedule, true);
431
+ window.removeEventListener("resize", schedule);
432
+ if (observer !== null) observer.disconnect();
433
+ };
434
+ }, [containerRef, onOutOfView]);
435
+ return rect;
436
+ }
384
437
  function refContains(ref, target) {
385
438
  if (ref === void 0) return false;
386
439
  const node = ref.current;
@@ -391,32 +444,36 @@ var clampIndex = (index, itemCount) => {
391
444
  const lastIndex = itemCount - 1;
392
445
  return index > lastIndex ? lastIndex : index;
393
446
  };
447
+ function claimKey(event) {
448
+ event.preventDefault();
449
+ event.stopPropagation();
450
+ }
394
451
  function buildKeyHandler(params) {
395
452
  const { itemCount, onHighlightChange, onSelectHighlighted, onClose } = params;
396
453
  return (event) => {
397
454
  switch (event.key) {
398
455
  case "ArrowDown":
399
- event.preventDefault();
456
+ claimKey(event);
400
457
  onHighlightChange((prev) => clampIndex(prev + 1, itemCount));
401
458
  return;
402
459
  case "ArrowUp":
403
- event.preventDefault();
460
+ claimKey(event);
404
461
  onHighlightChange((prev) => clampIndex(prev - 1, itemCount));
405
462
  return;
406
463
  case "Home":
407
- event.preventDefault();
464
+ claimKey(event);
408
465
  onHighlightChange(() => 0);
409
466
  return;
410
467
  case "End":
411
- event.preventDefault();
468
+ claimKey(event);
412
469
  onHighlightChange(() => clampIndex(itemCount - 1, itemCount));
413
470
  return;
414
471
  case "Enter":
415
- event.preventDefault();
472
+ claimKey(event);
416
473
  onSelectHighlighted();
417
474
  return;
418
475
  case "Escape":
419
- event.preventDefault();
476
+ claimKey(event);
420
477
  onClose();
421
478
  return;
422
479
  }
@@ -433,15 +490,15 @@ function useMenuKeyboard(params) {
433
490
  const insideMenu = refContains(menuRef, target);
434
491
  if (!insideAnchor && !insideMenu) onClose();
435
492
  };
436
- document.addEventListener("keydown", onKeyDown);
493
+ document.addEventListener("keydown", onKeyDown, true);
437
494
  document.addEventListener("mousedown", onMouseDown);
438
495
  return () => {
439
- document.removeEventListener("keydown", onKeyDown);
496
+ document.removeEventListener("keydown", onKeyDown, true);
440
497
  document.removeEventListener("mousedown", onMouseDown);
441
498
  };
442
499
  }, [containerRef, menuRef, enabled, onClose, params]);
443
500
  }
444
- var IS_WEB = Platform.OS === "web";
501
+ var IS_WEB2 = Platform.OS === "web";
445
502
  var BORDER_RADIUS2 = 8;
446
503
  var BORDER_WIDTH3 = 1;
447
504
  var MENU_PADDING2 = 4;
@@ -462,30 +519,6 @@ var styles7 = StyleSheet.create({
462
519
  elevation: MENU_ELEVATION
463
520
  }
464
521
  });
465
- function readAnchorRect(containerRef) {
466
- if (!IS_WEB) return null;
467
- const node = containerRef.current;
468
- if (node === null || typeof node.getBoundingClientRect !== "function") return null;
469
- const r = node.getBoundingClientRect();
470
- return { top: r.top, left: r.left, width: r.width, bottom: r.bottom };
471
- }
472
- function useAnchorRect(containerRef) {
473
- const [rect, setRect] = useState(() => readAnchorRect(containerRef));
474
- useLayoutEffect(() => {
475
- if (!IS_WEB) return void 0;
476
- const measure = () => {
477
- setRect(readAnchorRect(containerRef));
478
- };
479
- measure();
480
- window.addEventListener("scroll", measure, true);
481
- window.addEventListener("resize", measure);
482
- return () => {
483
- window.removeEventListener("scroll", measure, true);
484
- window.removeEventListener("resize", measure);
485
- };
486
- }, [containerRef]);
487
- return rect;
488
- }
489
522
  var InlineMenu = ({
490
523
  testID,
491
524
  accessibilityLabel,
@@ -500,7 +533,7 @@ var InlineMenu = ({
500
533
  const { theme } = useUi();
501
534
  const { colors } = theme;
502
535
  const menuRef = useRef(null);
503
- const rect = useAnchorRect(containerRef);
536
+ const rect = useAnchorTracking(containerRef, onClose);
504
537
  const selectedIndex = useMemo(
505
538
  () => Math.max(0, options.findIndex((opt) => opt.value === value)),
506
539
  [options, value]
@@ -530,7 +563,7 @@ var InlineMenu = ({
530
563
  });
531
564
  const popoverStyle = useMemo(() => {
532
565
  const themed = { borderColor: colors.border, backgroundColor: colors.surface };
533
- if (IS_WEB)
566
+ if (IS_WEB2)
534
567
  return [
535
568
  buildPortalPopoverStyle(rect ?? { left: 0, width: 0, bottom: 0 }, menuMinWidth),
536
569
  themed
@@ -558,7 +591,7 @@ var InlineMenu = ({
558
591
  )) })
559
592
  }
560
593
  );
561
- if (IS_WEB) return createPortal(menu, document.body);
594
+ if (IS_WEB2) return createPortal(menu, document.body);
562
595
  return menu;
563
596
  };
564
597
  var MENU_BREAKPOINT = 768;
@@ -572,6 +605,7 @@ function useResolvedDropdownVariant(explicit) {
572
605
  const { width } = useWindowDimensions();
573
606
  return resolveDropdownVariant(explicit, width, Platform.OS);
574
607
  }
608
+ var IS_WEB3 = Platform.OS === "web";
575
609
  var BORDER_RADIUS3 = 8;
576
610
  var BORDER_WIDTH4 = 1;
577
611
  var BODY_FONT_SIZE2 = 14;
@@ -580,8 +614,21 @@ var MODAL_MIN_WIDTH = 200;
580
614
  var MODAL_MAX_HEIGHT = 300;
581
615
  var CONTAINER_PADDING_H = 12;
582
616
  var CONTAINER_PADDING_V = 10;
617
+ var SR_ONLY_SIZE = 1;
618
+ var SR_ONLY_OFFSET = -1;
619
+ function buildHintProps(hint, hintId) {
620
+ return IS_WEB3 ? { "aria-describedby": hintId } : { accessibilityHint: hint };
621
+ }
583
622
  var styles8 = StyleSheet.create({
584
623
  anchor: { position: "relative" },
624
+ srOnly: {
625
+ position: "absolute",
626
+ width: SR_ONLY_SIZE,
627
+ height: SR_ONLY_SIZE,
628
+ margin: SR_ONLY_OFFSET,
629
+ padding: 0,
630
+ overflow: "hidden"
631
+ },
585
632
  container: {
586
633
  borderWidth: BORDER_WIDTH4,
587
634
  borderRadius: BORDER_RADIUS3,
@@ -619,6 +666,7 @@ var ModalDropdown = ({
619
666
  const resolvedVariant = useResolvedDropdownVariant(variant);
620
667
  const isMenu = resolvedVariant === "menu" /* Menu */;
621
668
  const [isOpen, setIsOpen] = useState(false);
669
+ const hintId = `${testID}-hint`;
622
670
  const anchorRef = useRef(null);
623
671
  const dialogRef = useRef(null);
624
672
  useFocusTrap(dialogRef, isOpen && !isMenu);
@@ -673,7 +721,7 @@ var ModalDropdown = ({
673
721
  TouchableOpacity,
674
722
  {
675
723
  accessible: true,
676
- accessibilityHint,
724
+ ...buildHintProps(accessibilityHint, hintId),
677
725
  accessibilityLabel,
678
726
  accessibilityRole: "button",
679
727
  accessibilityState: { expanded: isOpen },
@@ -684,6 +732,7 @@ var ModalDropdown = ({
684
732
  children: renderTrigger !== void 0 ? renderTrigger({ label: selectedLabel, isOpen }) : /* @__PURE__ */ jsx(Text, { style: [styles8.selectedText, { color: colors.text }], children: selectedLabel })
685
733
  }
686
734
  ),
735
+ IS_WEB3 ? /* @__PURE__ */ jsx(Text, { nativeID: hintId, style: styles8.srOnly, testID: `${testID}-hint`, children: accessibilityHint }) : null,
687
736
  isMenu && isOpen ? /* @__PURE__ */ jsx(
688
737
  InlineMenu,
689
738
  {