@dloizides/ui-layout 1.12.2 → 1.15.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
@@ -2,8 +2,10 @@ import React8, { createContext, useMemo, useRef, useEffect, useState, useCallbac
2
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 { useA11y } from '@dloizides/a11y';
5
6
  import { createPortal } from 'react-dom';
6
7
  import { SvgIcon } from '@dloizides/ui-icons';
8
+ import { useReducedMotion, prefersReducedMotion } from '@dloizides/rn-web-hooks';
7
9
 
8
10
  // src/Section/Section.tsx
9
11
  var styles = StyleSheet.create({
@@ -60,7 +62,19 @@ var LAYOUT_I18N = {
60
62
  upgradePromptCta: "settings.billing.upgradePrompt.cta",
61
63
  upgradePromptCtaHint: "settings.billing.upgradePrompt.ctaHint",
62
64
  upgradePromptDismiss: "settings.billing.upgradePrompt.dismiss",
63
- upgradePromptDismissHint: "settings.billing.upgradePrompt.dismissHint"
65
+ upgradePromptDismissHint: "settings.billing.upgradePrompt.dismissHint",
66
+ /** `CopyableId`: the copy control's label in each of its three states, plus its hint. */
67
+ copy: "common.copy",
68
+ copyHint: "common.copyHint",
69
+ copied: "common.copied",
70
+ /**
71
+ * Shown when the clipboard write genuinely failed — non-secure context, denied permission.
72
+ * This string reaching a user is CORRECT behaviour, not a defect: the alternative is them
73
+ * believing they hold an identifier they do not.
74
+ */
75
+ copyFailed: "common.copyFailed",
76
+ /** `CopyableId`: hint on the value itself, explaining it is shortened for display. */
77
+ copyableIdHint: "common.copyableIdHint"
64
78
  };
65
79
  var styles2 = StyleSheet.create({
66
80
  heading: {
@@ -664,7 +678,6 @@ function useResolvedDropdownVariant(explicit) {
664
678
  const { width } = useWindowDimensions();
665
679
  return resolveDropdownVariant(explicit, width, Platform.OS);
666
680
  }
667
- var IS_WEB3 = Platform.OS === "web";
668
681
  var BORDER_RADIUS3 = 8;
669
682
  var BORDER_WIDTH4 = 1;
670
683
  var BODY_FONT_SIZE2 = 14;
@@ -673,21 +686,8 @@ var MODAL_MIN_WIDTH = 200;
673
686
  var MODAL_MAX_HEIGHT = 300;
674
687
  var CONTAINER_PADDING_H = 12;
675
688
  var CONTAINER_PADDING_V = 10;
676
- var SR_ONLY_SIZE = 1;
677
- var SR_ONLY_OFFSET = -1;
678
- function buildHintProps(hint, hintId) {
679
- return IS_WEB3 ? { "aria-describedby": hintId } : { accessibilityHint: hint };
680
- }
681
689
  var styles8 = StyleSheet.create({
682
690
  anchor: { position: "relative" },
683
- srOnly: {
684
- position: "absolute",
685
- width: SR_ONLY_SIZE,
686
- height: SR_ONLY_SIZE,
687
- margin: SR_ONLY_OFFSET,
688
- padding: 0,
689
- overflow: "hidden"
690
- },
691
691
  container: {
692
692
  borderWidth: BORDER_WIDTH4,
693
693
  borderRadius: BORDER_RADIUS3,
@@ -725,7 +725,13 @@ var ModalDropdown = ({
725
725
  const resolvedVariant = useResolvedDropdownVariant(variant);
726
726
  const isMenu = resolvedVariant === "menu" /* Menu */;
727
727
  const [isOpen, setIsOpen] = useState(false);
728
- const hintId = `${testID}-hint`;
728
+ const { a11yProps, hintNode } = useA11y({
729
+ label: accessibilityLabel,
730
+ hint: accessibilityHint,
731
+ role: "button",
732
+ state: { expanded: isOpen },
733
+ testID
734
+ });
729
735
  const anchorRef = useRef(null);
730
736
  const dialogRef = useRef(null);
731
737
  useFocusTrap(dialogRef, isOpen && !isMenu);
@@ -776,22 +782,10 @@ var ModalDropdown = ({
776
782
  );
777
783
  const keyExtractor = useCallback((item) => String(item.value), []);
778
784
  return /* @__PURE__ */ jsxs(View, { ref: anchorRef, style: anchorStyle, children: [
779
- /* @__PURE__ */ jsx(
780
- TouchableOpacity,
781
- {
782
- accessible: true,
783
- ...buildHintProps(accessibilityHint, hintId),
784
- accessibilityLabel,
785
- accessibilityRole: "button",
786
- accessibilityState: { expanded: isOpen },
787
- "aria-expanded": isOpen,
788
- style: containerStyle,
789
- testID,
790
- onPress: handleToggle,
791
- children: renderTrigger !== void 0 ? renderTrigger({ label: selectedLabel, isOpen }) : /* @__PURE__ */ jsx(Text, { style: [styles8.selectedText, { color: colors.text }], children: selectedLabel })
792
- }
793
- ),
794
- IS_WEB3 ? /* @__PURE__ */ jsx(Text, { nativeID: hintId, style: styles8.srOnly, testID: `${testID}-hint`, children: accessibilityHint }) : null,
785
+ /* @__PURE__ */ jsxs(TouchableOpacity, { ...a11yProps, style: containerStyle, onPress: handleToggle, children: [
786
+ renderTrigger !== void 0 ? renderTrigger({ label: selectedLabel, isOpen }) : /* @__PURE__ */ jsx(Text, { style: [styles8.selectedText, { color: colors.text }], children: selectedLabel }),
787
+ hintNode
788
+ ] }),
795
789
  isMenu && isOpen ? /* @__PURE__ */ jsx(
796
790
  InlineMenu,
797
791
  {
@@ -920,10 +914,6 @@ function enableAndroidLayoutAnimationOnce() {
920
914
  setter(true);
921
915
  }
922
916
  }
923
- function prefersReducedMotion() {
924
- if (Platform.OS !== "web" || typeof window === "undefined" || typeof window.matchMedia !== "function") return false;
925
- return window.matchMedia("(prefers-reduced-motion: reduce)").matches;
926
- }
927
917
  function animateNextLayout() {
928
918
  if (prefersReducedMotion()) return;
929
919
  enableAndroidLayoutAnimationOnce();
@@ -1049,10 +1039,6 @@ var styles11 = StyleSheet.create({
1049
1039
  });
1050
1040
  var ACTIVATION_KEYS = ["Enter", " ", "Spacebar"];
1051
1041
  var INSTANT_DURATION_MS = 0;
1052
- function prefersReducedMotion2() {
1053
- if (Platform.OS !== "web" || typeof window === "undefined" || typeof window.matchMedia !== "function") return false;
1054
- return window.matchMedia("(prefers-reduced-motion: reduce)").matches;
1055
- }
1056
1042
  var AccordionItem = ({
1057
1043
  id,
1058
1044
  title,
@@ -1074,14 +1060,15 @@ var AccordionItem = ({
1074
1060
  const bodyTestID = `${headerTestID}-body`;
1075
1061
  const chevronTestID = `${headerTestID}-chevron`;
1076
1062
  const [rotation] = useState(() => new Animated.Value(expanded ? ROTATE_EXPANDED : ROTATE_COLLAPSED));
1063
+ const reducedMotion = useReducedMotion();
1077
1064
  useEffect(() => {
1078
1065
  Animated.timing(rotation, {
1079
1066
  toValue: expanded ? ROTATE_EXPANDED : ROTATE_COLLAPSED,
1080
- duration: prefersReducedMotion2() ? INSTANT_DURATION_MS : ROTATE_DURATION_MS,
1067
+ duration: reducedMotion ? INSTANT_DURATION_MS : ROTATE_DURATION_MS,
1081
1068
  easing: Easing.out(Easing.ease),
1082
1069
  useNativeDriver: Platform.OS !== "web"
1083
1070
  }).start();
1084
- }, [expanded, rotation]);
1071
+ }, [expanded, rotation, reducedMotion]);
1085
1072
  const rotate = rotation.interpolate({
1086
1073
  inputRange: [ROTATE_COLLAPSED, ROTATE_EXPANDED],
1087
1074
  outputRange: [COLLAPSED_DEG, boxed ? BOXED_EXPANDED_DEG : PLAIN_EXPANDED_DEG]
@@ -1199,7 +1186,7 @@ function formatUrlForDisplay(value) {
1199
1186
  return trimmed.length > 0 ? trimmed : value;
1200
1187
  }
1201
1188
  var UNMEASURED = 0;
1202
- var IS_WEB4 = Platform.OS === "web";
1189
+ var IS_WEB3 = Platform.OS === "web";
1203
1190
  function measureParentInnerWidth(node) {
1204
1191
  const parent = node.parentElement;
1205
1192
  if (parent === null) return UNMEASURED;
@@ -1211,12 +1198,12 @@ function useAvailableWidth() {
1211
1198
  const [width, setWidth] = useState(UNMEASURED);
1212
1199
  const nodeRef = useRef(null);
1213
1200
  const measureRef = useCallback((node) => {
1214
- if (!IS_WEB4) return;
1201
+ if (!IS_WEB3) return;
1215
1202
  nodeRef.current = node;
1216
1203
  if (nodeRef.current !== null) setWidth(measureParentInnerWidth(nodeRef.current));
1217
1204
  }, []);
1218
1205
  useEffect(() => {
1219
- if (!IS_WEB4) return void 0;
1206
+ if (!IS_WEB3) return void 0;
1220
1207
  const remeasure = () => {
1221
1208
  const node = nodeRef.current;
1222
1209
  if (node !== null) setWidth(measureParentInnerWidth(node));
@@ -1225,7 +1212,7 @@ function useAvailableWidth() {
1225
1212
  return () => window.removeEventListener("resize", remeasure);
1226
1213
  }, []);
1227
1214
  const onLayout = useCallback((event) => {
1228
- if (IS_WEB4) return;
1215
+ if (IS_WEB3) return;
1229
1216
  setWidth(event.nativeEvent.layout.width);
1230
1217
  }, []);
1231
1218
  return { width, measureRef, onLayout };
@@ -1291,6 +1278,176 @@ var TruncatedText = ({
1291
1278
  );
1292
1279
  };
1293
1280
 
1294
- export { Accordion, AccordionItem, DEFAULT_BILLING_ROUTE, DropdownVariant, ELLIPSIS, Heading, LAYOUT_I18N, LAYOUT_TEST_IDS, ModalDropdown, ModalShell, Section, SegmentedControl, StatusBadge, TruncatedText, UpgradePrompt, formatUrlForDisplay, truncateMiddle, useFocusTrap };
1281
+ // src/CopyableId/CopyStatus.ts
1282
+ var CopyStatus = /* @__PURE__ */ ((CopyStatus2) => {
1283
+ CopyStatus2["Idle"] = "idle";
1284
+ CopyStatus2["Copied"] = "copied";
1285
+ CopyStatus2["Failed"] = "failed";
1286
+ return CopyStatus2;
1287
+ })(CopyStatus || {});
1288
+
1289
+ // src/CopyableId/copyText.ts
1290
+ var OFFSCREEN_POSITION = "-9999px";
1291
+ function hasDom() {
1292
+ return typeof document !== "undefined" && document.body !== null;
1293
+ }
1294
+ function copyViaExecCommand(value) {
1295
+ if (!hasDom()) return false;
1296
+ const area = document.createElement("textarea");
1297
+ area.value = value;
1298
+ area.readOnly = true;
1299
+ area.setAttribute("aria-hidden", "true");
1300
+ area.style.position = "fixed";
1301
+ area.style.top = OFFSCREEN_POSITION;
1302
+ area.style.left = OFFSCREEN_POSITION;
1303
+ area.style.opacity = "0";
1304
+ document.body.appendChild(area);
1305
+ try {
1306
+ area.select();
1307
+ area.setSelectionRange(0, value.length);
1308
+ return document.execCommand("copy");
1309
+ } catch {
1310
+ return false;
1311
+ } finally {
1312
+ document.body.removeChild(area);
1313
+ }
1314
+ }
1315
+ function hasAsyncClipboard() {
1316
+ return typeof navigator !== "undefined" && typeof navigator.clipboard?.writeText === "function";
1317
+ }
1318
+ async function copyText(value, writer) {
1319
+ if (value === "") return false;
1320
+ if (writer !== void 0) {
1321
+ try {
1322
+ await writer(value);
1323
+ return true;
1324
+ } catch {
1325
+ }
1326
+ }
1327
+ if (hasAsyncClipboard()) {
1328
+ try {
1329
+ await navigator.clipboard.writeText(value);
1330
+ return true;
1331
+ } catch {
1332
+ }
1333
+ }
1334
+ return copyViaExecCommand(value);
1335
+ }
1336
+
1337
+ // src/CopyableId/useCopyToClipboard.ts
1338
+ var COPY_FEEDBACK_MS = 2e3;
1339
+ function useCopyToClipboard(value, writer) {
1340
+ const [status, setStatus] = useState("idle" /* Idle */);
1341
+ const timerRef = useRef(null);
1342
+ const mountedRef = useRef(true);
1343
+ useEffect(() => {
1344
+ mountedRef.current = true;
1345
+ return () => {
1346
+ mountedRef.current = false;
1347
+ if (timerRef.current !== null) clearTimeout(timerRef.current);
1348
+ };
1349
+ }, []);
1350
+ const copy = useCallback(() => {
1351
+ void (async () => {
1352
+ const ok = await copyText(value, writer);
1353
+ if (!mountedRef.current) return;
1354
+ setStatus(ok ? "copied" /* Copied */ : "failed" /* Failed */);
1355
+ if (timerRef.current !== null) clearTimeout(timerRef.current);
1356
+ timerRef.current = setTimeout(() => {
1357
+ if (mountedRef.current) setStatus("idle" /* Idle */);
1358
+ }, COPY_FEEDBACK_MS);
1359
+ })();
1360
+ }, [value, writer]);
1361
+ return { status, copy };
1362
+ }
1363
+ var LABEL_FONT_SIZE = 11;
1364
+ var VALUE_FONT_SIZE = 12;
1365
+ var ACTION_FONT_SIZE = 12;
1366
+ var ROW_GAP = 8;
1367
+ var CELL_GAP = 2;
1368
+ var LABEL_LETTER_SPACING = 0.4;
1369
+ var ACTION_PADDING_H = 6;
1370
+ var ACTION_PADDING_V = 2;
1371
+ var ACTION_RADIUS = 6;
1372
+ var styles13 = StyleSheet.create({
1373
+ cell: { gap: CELL_GAP },
1374
+ label: {
1375
+ fontSize: LABEL_FONT_SIZE,
1376
+ fontWeight: "700",
1377
+ letterSpacing: LABEL_LETTER_SPACING,
1378
+ textTransform: "uppercase"
1379
+ },
1380
+ row: { flexDirection: "row", alignItems: "center", gap: ROW_GAP },
1381
+ /**
1382
+ * The truncation width comes from this wrapper, not from the row: `TruncatedText` measures its
1383
+ * PARENT (see `useAvailableWidth`), and the row also holds the button. `minWidth: 0` is what
1384
+ * actually lets a flex child shrink below its content width — without it the row just grows and
1385
+ * nothing ever truncates.
1386
+ */
1387
+ valueWrap: { flexShrink: 1, minWidth: 0 },
1388
+ value: { fontSize: VALUE_FONT_SIZE, fontWeight: "600" },
1389
+ action: {
1390
+ fontSize: ACTION_FONT_SIZE,
1391
+ fontWeight: "600",
1392
+ paddingHorizontal: ACTION_PADDING_H,
1393
+ paddingVertical: ACTION_PADDING_V,
1394
+ borderRadius: ACTION_RADIUS
1395
+ }
1396
+ });
1397
+ var TONE_STEP = "500";
1398
+ function useActionState(status) {
1399
+ const { theme } = useUi();
1400
+ return useMemo(() => {
1401
+ const primary = theme.palette.primary[TONE_STEP];
1402
+ if (status === "copied" /* Copied */) {
1403
+ return { key: LAYOUT_I18N.copied, color: theme.semantic.success?.[TONE_STEP] ?? primary };
1404
+ }
1405
+ if (status === "failed" /* Failed */) {
1406
+ return { key: LAYOUT_I18N.copyFailed, color: theme.semantic.error[TONE_STEP] };
1407
+ }
1408
+ return { key: LAYOUT_I18N.copy, color: primary };
1409
+ }, [status, theme]);
1410
+ }
1411
+ var CopyableId = ({
1412
+ value,
1413
+ label,
1414
+ testID,
1415
+ accessibilityLabel,
1416
+ writer
1417
+ }) => {
1418
+ const { theme, t } = useUi();
1419
+ const { colors } = theme;
1420
+ const { status, copy } = useCopyToClipboard(value, writer);
1421
+ const action = useActionState(status);
1422
+ return /* @__PURE__ */ jsxs(View, { style: styles13.cell, testID, children: [
1423
+ label !== void 0 && /* @__PURE__ */ jsx(Text, { style: [styles13.label, { color: colors.textSecondary }], children: label }),
1424
+ /* @__PURE__ */ jsxs(View, { style: styles13.row, children: [
1425
+ /* @__PURE__ */ jsx(View, { style: styles13.valueWrap, children: /* @__PURE__ */ jsx(
1426
+ TruncatedText,
1427
+ {
1428
+ value,
1429
+ style: [styles13.value, { color: colors.text }],
1430
+ testID: `${testID}-value`,
1431
+ accessibilityLabel: accessibilityLabel ?? value,
1432
+ accessibilityHint: t(LAYOUT_I18N.copyableIdHint)
1433
+ }
1434
+ ) }),
1435
+ /* @__PURE__ */ jsx(
1436
+ Text,
1437
+ {
1438
+ onPress: copy,
1439
+ accessibilityRole: "button",
1440
+ accessibilityLabel: t(action.key),
1441
+ accessibilityHint: t(LAYOUT_I18N.copyHint),
1442
+ testID: `${testID}-copy`,
1443
+ style: [styles13.action, { color: action.color }],
1444
+ children: t(action.key)
1445
+ }
1446
+ )
1447
+ ] })
1448
+ ] });
1449
+ };
1450
+
1451
+ export { Accordion, AccordionItem, COPY_FEEDBACK_MS, CopyStatus, CopyableId, DEFAULT_BILLING_ROUTE, DropdownVariant, ELLIPSIS, Heading, LAYOUT_I18N, LAYOUT_TEST_IDS, ModalDropdown, ModalShell, Section, SegmentedControl, StatusBadge, TruncatedText, UpgradePrompt, copyText, formatUrlForDisplay, truncateMiddle, useCopyToClipboard, useFocusTrap };
1295
1452
  //# sourceMappingURL=index.mjs.map
1296
1453
  //# sourceMappingURL=index.mjs.map