@dloizides/ui-layout 1.42.0 → 1.43.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
@@ -1,5 +1,5 @@
1
1
  import React14, { createContext, useMemo, useState, useRef, useEffect, useCallback, useContext, useLayoutEffect } from 'react';
2
- import { StyleSheet, Platform, View, Text, useWindowDimensions, Modal as Modal$1, Animated, TouchableOpacity, ScrollView, Pressable, FlatList, Easing } from 'react-native';
2
+ import { StyleSheet, Platform, View, Text, useWindowDimensions, Modal as Modal$1, Animated, ScrollView, TouchableOpacity, Pressable, FlatList, Easing } from 'react-native';
3
3
  import { MODAL_OVERLAY_COLOR, useUi } from '@dloizides/ui-feedback';
4
4
  import { jsx, jsxs, Fragment } from 'react/jsx-runtime';
5
5
  import { IconButton, Button } from '@dloizides/ui-buttons';
@@ -420,8 +420,14 @@ function getFocusableElements(container) {
420
420
  function savePreviousFocus() {
421
421
  return document.activeElement instanceof HTMLElement ? document.activeElement : null;
422
422
  }
423
+ var MAX_ATTACH_FRAMES = 10;
424
+ function findNamed(domNode, initialFocusTestID) {
425
+ if (initialFocusTestID === void 0) return null;
426
+ const selector = `[data-testid="${initialFocusTestID}"]`;
427
+ return domNode.matches(selector) ? domNode : domNode.querySelector(selector);
428
+ }
423
429
  function focusInitial(domNode, initialFocusTestID) {
424
- const named = initialFocusTestID === void 0 ? null : domNode.querySelector(`[data-testid="${initialFocusTestID}"]`);
430
+ const named = findNamed(domNode, initialFocusTestID);
425
431
  if (named) {
426
432
  named.focus();
427
433
  return;
@@ -455,15 +461,29 @@ function useFocusTrap(containerRef, enabled, initialFocusTestID) {
455
461
  const previousFocusRef = useRef(null);
456
462
  useEffect(() => {
457
463
  if (Platform.OS !== "web" || !enabled) return;
458
- const domNode = containerRef.current;
459
- if (!domNode) return;
460
- const previous = savePreviousFocus();
461
- previousFocusRef.current = previous !== null && domNode.contains(previous) ? null : previous;
462
- queueMicrotask(() => focusInitial(domNode, initialFocusTestID));
463
- const listener = (e) => handleKeyDown(domNode, e);
464
- domNode.addEventListener("keydown", listener);
464
+ let domNode = null;
465
+ let frame = 0;
466
+ let attempts = 0;
467
+ const listener = (e) => {
468
+ if (domNode) handleKeyDown(domNode, e);
469
+ };
470
+ const attach = () => {
471
+ const node = containerRef.current;
472
+ if (!node) {
473
+ attempts += 1;
474
+ if (attempts <= MAX_ATTACH_FRAMES) frame = requestAnimationFrame(attach);
475
+ return;
476
+ }
477
+ domNode = node;
478
+ const previous = savePreviousFocus();
479
+ previousFocusRef.current = previous !== null && node.contains(previous) ? null : previous;
480
+ queueMicrotask(() => focusInitial(node, initialFocusTestID));
481
+ node.addEventListener("keydown", listener);
482
+ };
483
+ attach();
465
484
  return () => {
466
- domNode.removeEventListener("keydown", listener);
485
+ cancelAnimationFrame(frame);
486
+ domNode?.removeEventListener("keydown", listener);
467
487
  previousFocusRef.current?.focus();
468
488
  previousFocusRef.current = null;
469
489
  };
@@ -502,7 +522,9 @@ var styles6 = StyleSheet.create({
502
522
  // react-native-web, which is why the fade is driven by `Animated` instead. `pointerEvents` lives
503
523
  // in style (not the deprecated prop) so taps fall through to the sibling tap-catcher below.
504
524
  scrim: { backgroundColor: MODAL_OVERLAY_COLOR, pointerEvents: "none" },
525
+ backdrop: { zIndex: 0 },
505
526
  dialog: {
527
+ zIndex: 1,
506
528
  width: "100%",
507
529
  borderRadius: DIALOG_RADIUS,
508
530
  padding: DIALOG_PADDING,
@@ -565,6 +587,7 @@ var Modal = ({
565
587
  [theme.colors.surface, size]
566
588
  );
567
589
  const hasHeader = title !== void 0 || showClose;
590
+ const focusesDialog = initialFocusTestID === testID;
568
591
  const hardwareBackClose = Platform.OS === "web" ? void 0 : onClose;
569
592
  return /* @__PURE__ */ jsx(Modal$1, { transparent: true, visible: mounted, onRequestClose: hardwareBackClose, children: /* @__PURE__ */ jsxs(View, { style: styles6.overlay, children: [
570
593
  /* @__PURE__ */ jsx(
@@ -573,19 +596,6 @@ var Modal = ({
573
596
  style: [StyleSheet.absoluteFill, styles6.scrim, { opacity: backdrop.style.opacity }]
574
597
  }
575
598
  ),
576
- /* @__PURE__ */ jsx(
577
- TouchableOpacity,
578
- {
579
- accessible: true,
580
- accessibilityHint: t(LAYOUT_I18N.dismissDropdownHint),
581
- accessibilityLabel: t(LAYOUT_I18N.dismissDropdown),
582
- accessibilityRole: "button",
583
- activeOpacity: 1,
584
- style: StyleSheet.absoluteFill,
585
- testID: `${testID}-backdrop`,
586
- onPress: onClose
587
- }
588
- ),
589
599
  /* @__PURE__ */ jsxs(
590
600
  Animated.View,
591
601
  {
@@ -594,6 +604,7 @@ var Modal = ({
594
604
  "aria-label": accessibleName,
595
605
  role: "dialog",
596
606
  style: [dialogStyle, panel.style],
607
+ tabIndex: focusesDialog ? -1 : void 0,
597
608
  testID,
598
609
  children: [
599
610
  hasHeader ? /* @__PURE__ */ jsxs(View, { style: styles6.headerRow, children: [
@@ -617,6 +628,20 @@ var Modal = ({
617
628
  footer !== void 0 ? /* @__PURE__ */ jsx(ModalFooter, { testID: `${testID}-footer`, children: footer }) : null
618
629
  ]
619
630
  }
631
+ ),
632
+ /* @__PURE__ */ jsx(
633
+ TouchableOpacity,
634
+ {
635
+ accessible: true,
636
+ accessibilityHint: t(LAYOUT_I18N.dismissDropdownHint),
637
+ accessibilityLabel: t(LAYOUT_I18N.dismissDropdown),
638
+ accessibilityRole: "button",
639
+ activeOpacity: 1,
640
+ style: [StyleSheet.absoluteFill, styles6.backdrop],
641
+ focusable: false,
642
+ testID: `${testID}-backdrop`,
643
+ onPress: onClose
644
+ }
620
645
  )
621
646
  ] }) });
622
647
  };
@@ -2044,6 +2069,14 @@ var BottomSheet = ({
2044
2069
  )
2045
2070
  ] }) });
2046
2071
  };
2072
+
2073
+ // src/Modal/ConfirmDialogFocus.ts
2074
+ var ConfirmDialogFocus = /* @__PURE__ */ ((ConfirmDialogFocus2) => {
2075
+ ConfirmDialogFocus2["Cancel"] = "cancel";
2076
+ ConfirmDialogFocus2["Confirm"] = "confirm";
2077
+ ConfirmDialogFocus2["None"] = "none";
2078
+ return ConfirmDialogFocus2;
2079
+ })(ConfirmDialogFocus || {});
2047
2080
  var MESSAGE_FONT_SIZE2 = 14;
2048
2081
  var MESSAGE_LINE_HEIGHT = 20;
2049
2082
  var styles18 = StyleSheet.create({
@@ -2059,11 +2092,17 @@ var ConfirmDialog = ({
2059
2092
  onConfirm,
2060
2093
  onCancel,
2061
2094
  busy = false,
2095
+ initialFocus = "cancel" /* Cancel */,
2062
2096
  testID = LAYOUT_TEST_IDS.confirmDialog
2063
2097
  }) => {
2064
2098
  const { theme, t } = useUi();
2065
2099
  const resolvedConfirm = confirmLabel ?? t(LAYOUT_I18N.confirm);
2066
2100
  const resolvedCancel = cancelLabel ?? t(LAYOUT_I18N.cancel);
2101
+ const focusTestID = {
2102
+ ["cancel" /* Cancel */]: `${testID}-cancel`,
2103
+ ["confirm" /* Confirm */]: `${testID}-confirm`,
2104
+ ["none" /* None */]: testID
2105
+ };
2067
2106
  const footer = /* @__PURE__ */ jsxs(Fragment, { children: [
2068
2107
  /* @__PURE__ */ jsx(
2069
2108
  Button,
@@ -2094,6 +2133,7 @@ var ConfirmDialog = ({
2094
2133
  Modal,
2095
2134
  {
2096
2135
  footer,
2136
+ initialFocusTestID: focusTestID[initialFocus],
2097
2137
  showClose: false,
2098
2138
  size: "sm",
2099
2139
  testID,
@@ -3053,6 +3093,6 @@ var ListRow = ({
3053
3093
  ] });
3054
3094
  };
3055
3095
 
3056
- export { A4_PAGE, API_COMMIT_SEPARATOR, Accordion, AccordionItem, AppText, BUILD_INFO_PREFIX, BottomSheet, BuildInfoFooter, COPY_FEEDBACK_MS, ConfirmDialog, CopyStatus, CopyableId, DEFAULT_BILLING_ROUTE, DropdownVariant, ELLIPSIS, FormDialog, HEADER_STACK_BREAKPOINT, Heading, LAYOUT_COLLAPSE_BREAKPOINT, LAYOUT_I18N, LAYOUT_TEST_IDS, ListRow, MIN_TARGET_PX, MODAL_FOOTER_STACK_BELOW, Modal, ModalDropdown, ModalShell, PageFrame, RowSelectedStyle, ScreenColumn, Section, SectionVariant, SegmentedControl, SelectedMarker, SideSheet, StatusBadge, StickyPreview, TYPE_SCALE_BREAKPOINT, Tabs, TabsCollapsedTrigger, TextKind, TruncatedText, UpgradePrompt, copyText, formatBuildInfo, formatUrlForDisplay, isKeyboardOpen, isModalFooterStacked, isWideWebViewport, pageScale, resolveRowLook, scaledFrame, shortenCommit, trimTrailingSlash, truncateMiddle, useApiCommit, useCopyToClipboard, useFocusTrap, useIsWideViewport, useListRowTone, useVisibleViewport };
3096
+ export { A4_PAGE, API_COMMIT_SEPARATOR, Accordion, AccordionItem, AppText, BUILD_INFO_PREFIX, BottomSheet, BuildInfoFooter, COPY_FEEDBACK_MS, ConfirmDialog, ConfirmDialogFocus, CopyStatus, CopyableId, DEFAULT_BILLING_ROUTE, DropdownVariant, ELLIPSIS, FormDialog, HEADER_STACK_BREAKPOINT, Heading, LAYOUT_COLLAPSE_BREAKPOINT, LAYOUT_I18N, LAYOUT_TEST_IDS, ListRow, MIN_TARGET_PX, MODAL_FOOTER_STACK_BELOW, Modal, ModalDropdown, ModalShell, PageFrame, RowSelectedStyle, ScreenColumn, Section, SectionVariant, SegmentedControl, SelectedMarker, SideSheet, StatusBadge, StickyPreview, TYPE_SCALE_BREAKPOINT, Tabs, TabsCollapsedTrigger, TextKind, TruncatedText, UpgradePrompt, copyText, formatBuildInfo, formatUrlForDisplay, isKeyboardOpen, isModalFooterStacked, isWideWebViewport, pageScale, resolveRowLook, scaledFrame, shortenCommit, trimTrailingSlash, truncateMiddle, useApiCommit, useCopyToClipboard, useFocusTrap, useIsWideViewport, useListRowTone, useVisibleViewport };
3057
3097
  //# sourceMappingURL=index.mjs.map
3058
3098
  //# sourceMappingURL=index.mjs.map