@dloizides/ui-layout 1.22.1 → 1.24.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,12 +1,13 @@
1
1
  import React10, { 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';
2
+ import { StyleSheet, Platform, View, Text, Pressable, TouchableOpacity, Modal, Animated, FlatList, ScrollView, Easing, useWindowDimensions } 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 { MIN_TOUCH_TARGET_PX, useA11y } from '@dloizides/a11y';
6
+ import { useEnterExit, Collapse } from '@dloizides/ui-motion';
6
7
  import { createPortal } from 'react-dom';
7
8
  import { SvgIcon } from '@dloizides/ui-icons';
8
9
  import { IconButton, Button } from '@dloizides/ui-buttons';
9
- import { useReducedMotion, prefersReducedMotion } from '@dloizides/rn-web-hooks';
10
+ import { useReducedMotion } from '@dloizides/rn-web-hooks';
10
11
 
11
12
  // src/Section/Section.tsx
12
13
  var styles = StyleSheet.create({
@@ -321,14 +322,21 @@ var BORDER_RADIUS = 8;
321
322
  var BORDER_WIDTH2 = 1;
322
323
  var MENU_PADDING = 4;
323
324
  var MENU_BOX_SHADOW = "0px 2px 8px rgba(0, 0, 0, 0.15)";
324
- function buildPortalPopoverStyle(rect, minWidth = 0) {
325
+ var MENU_VIEWPORT_MARGIN = 8;
326
+ function buildPortalPopoverStyle(rect, minWidth = 0, viewportWidth = 0) {
327
+ const width = Math.max(rect.width, minWidth);
328
+ let left = rect.left;
329
+ if (viewportWidth > 0) {
330
+ const maxLeft = viewportWidth - width - MENU_VIEWPORT_MARGIN;
331
+ left = Math.max(MENU_VIEWPORT_MARGIN, Math.min(rect.left, maxLeft));
332
+ }
325
333
  return {
326
334
  // RN's ViewStyle union omits 'fixed', but react-native-web honours it at runtime.
327
335
  // eslint-disable-next-line @typescript-eslint/consistent-type-assertions -- web-only position value
328
336
  position: "fixed",
329
337
  top: rect.bottom + MENU_TOP_GAP,
330
- left: rect.left,
331
- width: Math.max(rect.width, minWidth),
338
+ left,
339
+ width,
332
340
  zIndex: MENU_Z_INDEX,
333
341
  borderWidth: BORDER_WIDTH2,
334
342
  borderRadius: BORDER_RADIUS,
@@ -558,7 +566,11 @@ var styles6 = StyleSheet.create({
558
566
  maxHeight: MENU_MAX_HEIGHT,
559
567
  boxShadow: MENU_BOX_SHADOW,
560
568
  elevation: MENU_ELEVATION
561
- }
569
+ },
570
+ // The enter/exit fade+scale lives on an INNER wrapper so the outer popover keeps its raw DOM
571
+ // node (the `menuRef` outside-click / keyboard logic and the web portal target depend on it).
572
+ // `transformOrigin: 'top'` makes the scale grow downward FROM the anchor rather than the centre.
573
+ animated: { transformOrigin: "top" }
562
574
  });
563
575
  var InlineMenu = ({
564
576
  testID,
@@ -568,6 +580,7 @@ var InlineMenu = ({
568
580
  containerRef,
569
581
  optionTestID,
570
582
  menuMinWidth = 0,
583
+ animatedStyle,
571
584
  onSelect,
572
585
  onClose
573
586
  }) => {
@@ -604,11 +617,13 @@ var InlineMenu = ({
604
617
  });
605
618
  const popoverStyle = useMemo(() => {
606
619
  const themed = { borderColor: colors.border, backgroundColor: colors.surface };
607
- if (IS_WEB2)
620
+ if (IS_WEB2) {
621
+ const viewportWidth = typeof window !== "undefined" ? window.innerWidth : 0;
608
622
  return [
609
- buildPortalPopoverStyle(rect ?? { left: 0, width: 0, bottom: 0 }, menuMinWidth),
623
+ buildPortalPopoverStyle(rect ?? { left: 0, width: 0, bottom: 0 }, menuMinWidth, viewportWidth),
610
624
  themed
611
625
  ];
626
+ }
612
627
  return [styles6.popover, { minWidth: menuMinWidth }, themed];
613
628
  }, [colors.border, colors.surface, menuMinWidth, rect]);
614
629
  const menu = /* @__PURE__ */ jsx(
@@ -619,7 +634,7 @@ var InlineMenu = ({
619
634
  "aria-label": accessibilityLabel,
620
635
  style: popoverStyle,
621
636
  testID: `${testID}-menu`,
622
- children: /* @__PURE__ */ jsx(ScrollView, { keyboardShouldPersistTaps: "handled", children: options.map((option, index) => /* @__PURE__ */ jsx(
637
+ children: /* @__PURE__ */ jsx(Animated.View, { style: [styles6.animated, animatedStyle], children: /* @__PURE__ */ jsx(ScrollView, { keyboardShouldPersistTaps: "handled", children: options.map((option, index) => /* @__PURE__ */ jsx(
623
638
  OptionRow,
624
639
  {
625
640
  isHighlighted: index === highlightedIndex,
@@ -629,7 +644,7 @@ var InlineMenu = ({
629
644
  onSelect: () => onSelect(option.value)
630
645
  },
631
646
  String(option.value)
632
- )) })
647
+ )) }) })
633
648
  }
634
649
  );
635
650
  if (IS_WEB2) return createPortal(menu, document.body);
@@ -662,6 +677,8 @@ var HAMBURGER_GLYPH = "\u2630";
662
677
  var HAMBURGER_FONT_SIZE = 18;
663
678
  var CARET_OPEN_ROTATION = "180deg";
664
679
  var CARET_CLOSED_ROTATION = "0deg";
680
+ var POPOVER_ENTER_SCALE = 0.96;
681
+ var POPOVER_MOTION_MS = 120;
665
682
  var styles7 = StyleSheet.create({
666
683
  anchor: { position: "relative" },
667
684
  container: {
@@ -741,6 +758,16 @@ var ModalDropdown = ({
741
758
  const anchorRef = useRef(null);
742
759
  const dialogRef = useRef(null);
743
760
  useFocusTrap(dialogRef, isOpen && !isMenu);
761
+ const menuAnim = useEnterExit({
762
+ visible: isMenu && isOpen,
763
+ fromScale: POPOVER_ENTER_SCALE,
764
+ duration: POPOVER_MOTION_MS
765
+ });
766
+ const modalAnim = useEnterExit({
767
+ visible: !isMenu && isOpen,
768
+ fromScale: POPOVER_ENTER_SCALE,
769
+ duration: POPOVER_MOTION_MS
770
+ });
744
771
  const selectedLabel = useMemo(() => {
745
772
  const found = options.find((opt) => opt.value === value);
746
773
  return found?.label ?? t(LAYOUT_I18N.selectPlaceholder);
@@ -815,10 +842,11 @@ var ModalDropdown = ({
815
842
  ] }) : /* @__PURE__ */ jsx(Text, { style: [styles7.selectedText, { color: colors.text }], children: selectedLabel }),
816
843
  hintNode
817
844
  ] }),
818
- isMenu && isOpen ? /* @__PURE__ */ jsx(
845
+ isMenu && menuAnim.mounted ? /* @__PURE__ */ jsx(
819
846
  InlineMenu,
820
847
  {
821
848
  accessibilityLabel,
849
+ animatedStyle: menuAnim.style,
822
850
  containerRef: anchorRef,
823
851
  menuMinWidth,
824
852
  optionTestID: optionTestIDFor,
@@ -829,7 +857,7 @@ var ModalDropdown = ({
829
857
  onSelect: handleSelect
830
858
  }
831
859
  ) : null,
832
- !isMenu ? /* @__PURE__ */ jsx(Modal, { transparent: true, animationType: "fade", visible: isOpen, onRequestClose: handleClose, children: /* @__PURE__ */ jsxs(View, { style: styles7.modalOverlay, children: [
860
+ !isMenu ? /* @__PURE__ */ jsx(Modal, { transparent: true, visible: modalAnim.mounted, onRequestClose: handleClose, children: /* @__PURE__ */ jsxs(View, { style: styles7.modalOverlay, children: [
833
861
  /* @__PURE__ */ jsx(
834
862
  TouchableOpacity,
835
863
  {
@@ -844,13 +872,13 @@ var ModalDropdown = ({
844
872
  }
845
873
  ),
846
874
  /* @__PURE__ */ jsx(
847
- View,
875
+ Animated.View,
848
876
  {
849
877
  ref: dialogRef,
850
878
  accessibilityViewIsModal: true,
851
879
  "aria-label": accessibilityLabel,
852
880
  role: "dialog",
853
- style: modalContentStyle,
881
+ style: [modalContentStyle, modalAnim.style],
854
882
  children: /* @__PURE__ */ jsx(FlatList, { data: [...options], keyExtractor, renderItem: renderModalOption })
855
883
  }
856
884
  )
@@ -1116,7 +1144,9 @@ var UpgradePrompt = ({
1116
1144
  );
1117
1145
  };
1118
1146
  var CLOSE_ICON_SIZE = 18;
1147
+ var PANEL_ENTER_SCALE = 0.96;
1119
1148
  var styles10 = StyleSheet.create({
1149
+ animatedFill: { flex: 1 },
1120
1150
  container: {
1121
1151
  flex: 1,
1122
1152
  padding: 16
@@ -1140,6 +1170,7 @@ var ModalShell = ({
1140
1170
  showClose = true
1141
1171
  }) => {
1142
1172
  const { theme, t } = useUi();
1173
+ const { style: sheetStyle, mounted } = useEnterExit({ visible, fromScale: PANEL_ENTER_SCALE });
1143
1174
  const containerStyle = React10.useMemo(
1144
1175
  () => [styles10.container, { backgroundColor: theme.colors.background }, contentStyle],
1145
1176
  [theme.colors.background, contentStyle]
@@ -1149,11 +1180,11 @@ var ModalShell = ({
1149
1180
  return /* @__PURE__ */ jsx(
1150
1181
  Modal,
1151
1182
  {
1152
- animationType: "slide",
1183
+ transparent: true,
1153
1184
  testID: LAYOUT_TEST_IDS.modalShell,
1154
- visible,
1185
+ visible: mounted,
1155
1186
  onRequestClose: onCancel,
1156
- children: /* @__PURE__ */ jsxs(
1187
+ children: /* @__PURE__ */ jsx(Animated.View, { style: [styles10.animatedFill, sheetStyle], children: /* @__PURE__ */ jsxs(
1157
1188
  ScrollView,
1158
1189
  {
1159
1190
  accessibilityViewIsModal: true,
@@ -1179,7 +1210,7 @@ var ModalShell = ({
1179
1210
  children
1180
1211
  ]
1181
1212
  }
1182
- )
1213
+ ) })
1183
1214
  }
1184
1215
  );
1185
1216
  };
@@ -1213,15 +1244,20 @@ var HEADER_GAP = 12;
1213
1244
  var FOOTER_MARGIN = 16;
1214
1245
  var HEADER_MARGIN = 12;
1215
1246
  var DIALOG_MAX_HEIGHT = "90%";
1247
+ var PANEL_ENTER_SCALE2 = 0.96;
1216
1248
  var SIZE_MAX_WIDTH = { sm: 360, md: 480, lg: 640 };
1217
1249
  var styles11 = StyleSheet.create({
1218
1250
  overlay: {
1219
1251
  flex: 1,
1220
1252
  justifyContent: "center",
1221
1253
  alignItems: "center",
1222
- padding: OVERLAY_PADDING,
1223
- backgroundColor: MODAL_OVERLAY_COLOR
1254
+ padding: OVERLAY_PADDING
1224
1255
  },
1256
+ // The scrim is its OWN animated layer (behind the tap-catcher and the panel) so its opacity
1257
+ // can fade independently of the panel's fade+scale. `animationType` is a native-only no-op on
1258
+ // react-native-web, which is why the fade is driven by `Animated` instead. `pointerEvents` lives
1259
+ // in style (not the deprecated prop) so taps fall through to the sibling tap-catcher below.
1260
+ scrim: { backgroundColor: MODAL_OVERLAY_COLOR, pointerEvents: "none" },
1225
1261
  dialog: {
1226
1262
  width: "100%",
1227
1263
  borderRadius: DIALOG_RADIUS,
@@ -1252,6 +1288,9 @@ var Modal3 = ({
1252
1288
  }) => {
1253
1289
  const { theme, t } = useUi();
1254
1290
  const dialogRef = useRef(null);
1291
+ const backdrop = useEnterExit({ visible });
1292
+ const panel = useEnterExit({ visible, fromScale: PANEL_ENTER_SCALE2 });
1293
+ const mounted = panel.mounted || backdrop.mounted;
1255
1294
  useFocusTrap(dialogRef, visible);
1256
1295
  useEscapeKey(visible, onClose);
1257
1296
  useScrollLock(visible);
@@ -1262,7 +1301,13 @@ var Modal3 = ({
1262
1301
  [theme.colors.surface, size]
1263
1302
  );
1264
1303
  const hasHeader = title !== void 0 || showClose;
1265
- return /* @__PURE__ */ jsx(Modal, { transparent: true, animationType: "fade", visible, onRequestClose: onClose, children: /* @__PURE__ */ jsxs(View, { style: styles11.overlay, children: [
1304
+ return /* @__PURE__ */ jsx(Modal, { transparent: true, visible: mounted, onRequestClose: onClose, children: /* @__PURE__ */ jsxs(View, { style: styles11.overlay, children: [
1305
+ /* @__PURE__ */ jsx(
1306
+ Animated.View,
1307
+ {
1308
+ style: [StyleSheet.absoluteFill, styles11.scrim, { opacity: backdrop.style.opacity }]
1309
+ }
1310
+ ),
1266
1311
  /* @__PURE__ */ jsx(
1267
1312
  TouchableOpacity,
1268
1313
  {
@@ -1277,13 +1322,13 @@ var Modal3 = ({
1277
1322
  }
1278
1323
  ),
1279
1324
  /* @__PURE__ */ jsxs(
1280
- View,
1325
+ Animated.View,
1281
1326
  {
1282
1327
  ref: dialogRef,
1283
1328
  accessibilityViewIsModal: true,
1284
1329
  "aria-label": accessibleName,
1285
1330
  role: "dialog",
1286
- style: dialogStyle,
1331
+ style: [dialogStyle, panel.style],
1287
1332
  testID,
1288
1333
  children: [
1289
1334
  hasHeader ? /* @__PURE__ */ jsxs(View, { style: styles11.headerRow, children: [
@@ -1380,26 +1425,6 @@ function useAccordionContext() {
1380
1425
  }
1381
1426
  return value;
1382
1427
  }
1383
- var androidEnabled = false;
1384
- function enableAndroidLayoutAnimationOnce() {
1385
- if (androidEnabled) return;
1386
- androidEnabled = true;
1387
- const isAndroid = Platform.OS === "android";
1388
- const setter = UIManager.setLayoutAnimationEnabledExperimental;
1389
- if (isAndroid && typeof setter === "function") {
1390
- setter(true);
1391
- }
1392
- }
1393
- function animateNextLayout() {
1394
- if (prefersReducedMotion()) return;
1395
- enableAndroidLayoutAnimationOnce();
1396
- const configure = LayoutAnimation.configureNext;
1397
- if (typeof configure !== "function") return;
1398
- try {
1399
- configure(LayoutAnimation.Presets.easeInEaseOut);
1400
- } catch {
1401
- }
1402
- }
1403
1428
  var BOXED_GAP = 12;
1404
1429
  var styles13 = StyleSheet.create({
1405
1430
  container: { alignSelf: "stretch" },
@@ -1440,7 +1465,6 @@ var Accordion = ({
1440
1465
  );
1441
1466
  const toggle = useCallback(
1442
1467
  (id) => {
1443
- animateNextLayout();
1444
1468
  const next = new Set(openSet);
1445
1469
  const willOpen = !next.has(id);
1446
1470
  if (!allowMultiple) next.clear();
@@ -1627,7 +1651,7 @@ var AccordionItem = ({
1627
1651
  ]
1628
1652
  }
1629
1653
  ),
1630
- expanded ? /* @__PURE__ */ jsx(
1654
+ /* @__PURE__ */ jsx(Collapse, { open: expanded, testID: `${headerTestID}-collapse`, children: /* @__PURE__ */ jsx(
1631
1655
  View,
1632
1656
  {
1633
1657
  accessibilityLabel: resolvedLabel,
@@ -1637,7 +1661,7 @@ var AccordionItem = ({
1637
1661
  testID: bodyTestID,
1638
1662
  children
1639
1663
  }
1640
- ) : null
1664
+ ) })
1641
1665
  ] });
1642
1666
  };
1643
1667