@dloizides/ui-layout 1.6.0 → 1.7.1

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
@@ -1,7 +1,8 @@
1
- import React8, { createContext, useMemo, useRef, useEffect, useState, useCallback, useContext } from 'react';
2
- import { StyleSheet, View, Text, Pressable, TouchableOpacity, Platform, Modal, FlatList, ScrollView, Animated, Easing, useWindowDimensions, LayoutAnimation, UIManager } from 'react-native';
1
+ import React8, { createContext, useMemo, useRef, useEffect, useState, useCallback, useContext, useLayoutEffect } from 'react';
2
+ import { StyleSheet, Platform, View, Text, Pressable, TouchableOpacity, Modal, FlatList, ScrollView, Animated, Easing, useWindowDimensions, LayoutAnimation, UIManager } from 'react-native';
3
3
  import { MODAL_OVERLAY_COLOR, useUi } from '@dloizides/ui-feedback';
4
4
  import { jsx, jsxs } from 'react/jsx-runtime';
5
+ import { createPortal } from 'react-dom';
5
6
  import { SvgIcon } from '@dloizides/ui-icons';
6
7
 
7
8
  // src/Section/Section.tsx
@@ -305,6 +306,37 @@ var DropdownVariant = /* @__PURE__ */ ((DropdownVariant2) => {
305
306
  DropdownVariant2["Modal"] = "modal";
306
307
  return DropdownVariant2;
307
308
  })(DropdownVariant || {});
309
+
310
+ // src/ModalDropdown/menuStacking.ts
311
+ var MENU_Z_INDEX = 1e3;
312
+ var ANCHOR_OPEN_Z_INDEX = 1e3;
313
+ var MENU_ELEVATION = 8;
314
+ var MENU_TOP_GAP = 4;
315
+ var MENU_MAX_HEIGHT = 300;
316
+ var BORDER_RADIUS = 8;
317
+ var BORDER_WIDTH2 = 1;
318
+ var MENU_PADDING = 4;
319
+ var MENU_BOX_SHADOW = "0px 2px 8px rgba(0, 0, 0, 0.15)";
320
+ function buildPortalPopoverStyle(rect) {
321
+ return {
322
+ // RN's ViewStyle union omits 'fixed', but react-native-web honours it at runtime.
323
+ // eslint-disable-next-line @typescript-eslint/consistent-type-assertions -- web-only position value
324
+ position: "fixed",
325
+ top: rect.bottom + MENU_TOP_GAP,
326
+ left: rect.left,
327
+ width: rect.width,
328
+ zIndex: MENU_Z_INDEX,
329
+ borderWidth: BORDER_WIDTH2,
330
+ borderRadius: BORDER_RADIUS,
331
+ padding: MENU_PADDING,
332
+ maxHeight: MENU_MAX_HEIGHT,
333
+ boxShadow: MENU_BOX_SHADOW,
334
+ elevation: MENU_ELEVATION
335
+ };
336
+ }
337
+ function buildAnchorStackStyle(isMenuOpen) {
338
+ return isMenuOpen ? { zIndex: ANCHOR_OPEN_Z_INDEX, elevation: MENU_ELEVATION } : null;
339
+ }
308
340
  var OPTION_BORDER_RADIUS = 4;
309
341
  var BODY_FONT_SIZE = 14;
310
342
  var SELECTED_FONT_WEIGHT = "600";
@@ -349,6 +381,11 @@ var OptionRow = ({
349
381
  }
350
382
  );
351
383
  };
384
+ function refContains(ref, target) {
385
+ if (ref === void 0) return false;
386
+ const node = ref.current;
387
+ return node !== null && target !== null && node.contains(target);
388
+ }
352
389
  var clampIndex = (index, itemCount) => {
353
390
  if (index < 0) return 0;
354
391
  const lastIndex = itemCount - 1;
@@ -386,15 +423,15 @@ function buildKeyHandler(params) {
386
423
  };
387
424
  }
388
425
  function useMenuKeyboard(params) {
389
- const { containerRef, enabled, onClose } = params;
426
+ const { containerRef, menuRef, enabled, onClose } = params;
390
427
  useEffect(() => {
391
428
  if (Platform.OS !== "web" || !enabled) return;
392
429
  const onKeyDown = buildKeyHandler(params);
393
430
  const onMouseDown = (event) => {
394
- const node = containerRef.current;
395
431
  const target = event.target instanceof Node ? event.target : null;
396
- const clickedOutside = node !== null && (target === null || !node.contains(target));
397
- if (clickedOutside) onClose();
432
+ const insideAnchor = refContains(containerRef, target);
433
+ const insideMenu = refContains(menuRef, target);
434
+ if (!insideAnchor && !insideMenu) onClose();
398
435
  };
399
436
  document.addEventListener("keydown", onKeyDown);
400
437
  document.addEventListener("mousedown", onMouseDown);
@@ -402,17 +439,14 @@ function useMenuKeyboard(params) {
402
439
  document.removeEventListener("keydown", onKeyDown);
403
440
  document.removeEventListener("mousedown", onMouseDown);
404
441
  };
405
- }, [containerRef, enabled, onClose, params]);
442
+ }, [containerRef, menuRef, enabled, onClose, params]);
406
443
  }
407
- var BORDER_RADIUS = 8;
408
- var BORDER_WIDTH2 = 1;
409
- var MENU_PADDING = 4;
410
- var MENU_MAX_HEIGHT = 300;
411
- var MENU_TOP_GAP = 4;
412
- var MENU_Z_INDEX = 1e3;
413
- var MENU_ELEVATION = 8;
414
- var MENU_BOX_SHADOW = "0px 2px 8px rgba(0, 0, 0, 0.15)";
444
+ var IS_WEB = Platform.OS === "web";
445
+ var BORDER_RADIUS2 = 8;
446
+ var BORDER_WIDTH3 = 1;
447
+ var MENU_PADDING2 = 4;
415
448
  var styles7 = StyleSheet.create({
449
+ /** Native (in-tree) popover — anchored absolutely just under the trigger. */
416
450
  popover: {
417
451
  position: "absolute",
418
452
  top: "100%",
@@ -420,14 +454,38 @@ var styles7 = StyleSheet.create({
420
454
  right: 0,
421
455
  marginTop: MENU_TOP_GAP,
422
456
  zIndex: MENU_Z_INDEX,
423
- borderWidth: BORDER_WIDTH2,
424
- borderRadius: BORDER_RADIUS,
425
- padding: MENU_PADDING,
457
+ borderWidth: BORDER_WIDTH3,
458
+ borderRadius: BORDER_RADIUS2,
459
+ padding: MENU_PADDING2,
426
460
  maxHeight: MENU_MAX_HEIGHT,
427
461
  boxShadow: MENU_BOX_SHADOW,
428
462
  elevation: MENU_ELEVATION
429
463
  }
430
464
  });
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
+ }
431
489
  var InlineMenu = ({
432
490
  testID,
433
491
  accessibilityLabel,
@@ -439,6 +497,8 @@ var InlineMenu = ({
439
497
  }) => {
440
498
  const { theme } = useUi();
441
499
  const { colors } = theme;
500
+ const menuRef = useRef(null);
501
+ const rect = useAnchorRect(containerRef);
442
502
  const selectedIndex = useMemo(
443
503
  () => Math.max(0, options.findIndex((opt) => opt.value === value)),
444
504
  [options, value]
@@ -459,19 +519,22 @@ var InlineMenu = ({
459
519
  );
460
520
  useMenuKeyboard({
461
521
  containerRef,
522
+ menuRef,
462
523
  enabled: true,
463
524
  itemCount: options.length,
464
525
  onHighlightChange: setHighlightedIndex,
465
526
  onSelectHighlighted: selectHighlighted,
466
527
  onClose
467
528
  });
468
- const popoverStyle = useMemo(
469
- () => [styles7.popover, { borderColor: colors.border, backgroundColor: colors.surface }],
470
- [colors.border, colors.surface]
471
- );
472
- return /* @__PURE__ */ jsx(
529
+ const popoverStyle = useMemo(() => {
530
+ const themed = { borderColor: colors.border, backgroundColor: colors.surface };
531
+ if (IS_WEB) return [buildPortalPopoverStyle(rect ?? { left: 0, width: 0, bottom: 0 }), themed];
532
+ return [styles7.popover, themed];
533
+ }, [colors.border, colors.surface, rect]);
534
+ const menu = /* @__PURE__ */ jsx(
473
535
  View,
474
536
  {
537
+ ref: menuRef,
475
538
  accessibilityRole: "menu",
476
539
  "aria-label": accessibilityLabel,
477
540
  style: popoverStyle,
@@ -489,6 +552,8 @@ var InlineMenu = ({
489
552
  )) })
490
553
  }
491
554
  );
555
+ if (IS_WEB) return createPortal(menu, document.body);
556
+ return menu;
492
557
  };
493
558
  var MENU_BREAKPOINT = 768;
494
559
  var WEB_PLATFORM = "web";
@@ -501,8 +566,8 @@ function useResolvedDropdownVariant(explicit) {
501
566
  const { width } = useWindowDimensions();
502
567
  return resolveDropdownVariant(explicit, width, Platform.OS);
503
568
  }
504
- var BORDER_RADIUS2 = 8;
505
- var BORDER_WIDTH3 = 1;
569
+ var BORDER_RADIUS3 = 8;
570
+ var BORDER_WIDTH4 = 1;
506
571
  var BODY_FONT_SIZE2 = 14;
507
572
  var MODAL_PADDING = 8;
508
573
  var MODAL_MIN_WIDTH = 200;
@@ -512,8 +577,8 @@ var CONTAINER_PADDING_V = 10;
512
577
  var styles8 = StyleSheet.create({
513
578
  anchor: { position: "relative" },
514
579
  container: {
515
- borderWidth: BORDER_WIDTH3,
516
- borderRadius: BORDER_RADIUS2,
580
+ borderWidth: BORDER_WIDTH4,
581
+ borderRadius: BORDER_RADIUS3,
517
582
  paddingHorizontal: CONTAINER_PADDING_H,
518
583
  paddingVertical: CONTAINER_PADDING_V
519
584
  },
@@ -525,7 +590,7 @@ var styles8 = StyleSheet.create({
525
590
  backgroundColor: MODAL_OVERLAY_COLOR
526
591
  },
527
592
  modalContent: {
528
- borderRadius: BORDER_RADIUS2,
593
+ borderRadius: BORDER_RADIUS3,
529
594
  padding: MODAL_PADDING,
530
595
  minWidth: MODAL_MIN_WIDTH,
531
596
  maxHeight: MODAL_MAX_HEIGHT
@@ -569,6 +634,10 @@ var ModalDropdown = ({
569
634
  () => [styles8.container, { borderColor: colors.border, backgroundColor: colors.surface }],
570
635
  [colors.border, colors.surface]
571
636
  );
637
+ const anchorStyle = useMemo(
638
+ () => [styles8.anchor, buildAnchorStackStyle(isMenu && isOpen)],
639
+ [isMenu, isOpen]
640
+ );
572
641
  const modalContentStyle = useMemo(
573
642
  () => [styles8.modalContent, { backgroundColor: colors.surface }],
574
643
  [colors.surface]
@@ -586,7 +655,7 @@ var ModalDropdown = ({
586
655
  [handleSelect, testID, value]
587
656
  );
588
657
  const keyExtractor = useCallback((item) => String(item.value), []);
589
- return /* @__PURE__ */ jsxs(View, { ref: anchorRef, style: styles8.anchor, children: [
658
+ return /* @__PURE__ */ jsxs(View, { ref: anchorRef, style: anchorStyle, children: [
590
659
  /* @__PURE__ */ jsx(
591
660
  TouchableOpacity,
592
661
  {
@@ -614,30 +683,32 @@ var ModalDropdown = ({
614
683
  onSelect: handleSelect
615
684
  }
616
685
  ) : null,
617
- !isMenu ? /* @__PURE__ */ jsx(Modal, { transparent: true, animationType: "fade", visible: isOpen, onRequestClose: handleClose, children: /* @__PURE__ */ jsx(
618
- TouchableOpacity,
619
- {
620
- accessible: true,
621
- accessibilityHint: t("common.dismissDropdownHint"),
622
- accessibilityLabel: t("common.dismissDropdown"),
623
- accessibilityRole: "button",
624
- activeOpacity: 1,
625
- style: styles8.modalOverlay,
626
- testID: `${testID}-backdrop`,
627
- onPress: handleClose,
628
- children: /* @__PURE__ */ jsx(
629
- View,
630
- {
631
- ref: dialogRef,
632
- accessibilityViewIsModal: true,
633
- "aria-label": accessibilityLabel,
634
- role: "dialog",
635
- style: modalContentStyle,
636
- children: /* @__PURE__ */ jsx(FlatList, { data: [...options], keyExtractor, renderItem: renderModalOption })
637
- }
638
- )
639
- }
640
- ) }) : null
686
+ !isMenu ? /* @__PURE__ */ jsx(Modal, { transparent: true, animationType: "fade", visible: isOpen, onRequestClose: handleClose, children: /* @__PURE__ */ jsxs(View, { style: styles8.modalOverlay, children: [
687
+ /* @__PURE__ */ jsx(
688
+ TouchableOpacity,
689
+ {
690
+ accessible: true,
691
+ accessibilityHint: t("common.dismissDropdownHint"),
692
+ accessibilityLabel: t("common.dismissDropdown"),
693
+ accessibilityRole: "button",
694
+ activeOpacity: 1,
695
+ style: StyleSheet.absoluteFill,
696
+ testID: `${testID}-backdrop`,
697
+ onPress: handleClose
698
+ }
699
+ ),
700
+ /* @__PURE__ */ jsx(
701
+ View,
702
+ {
703
+ ref: dialogRef,
704
+ accessibilityViewIsModal: true,
705
+ "aria-label": accessibilityLabel,
706
+ role: "dialog",
707
+ style: modalContentStyle,
708
+ children: /* @__PURE__ */ jsx(FlatList, { data: [...options], keyExtractor, renderItem: renderModalOption })
709
+ }
710
+ )
711
+ ] }) }) : null
641
712
  ] });
642
713
  };
643
714
  var CLOSE_ICON_SIZE = 18;