@dloizides/ui-layout 1.13.0 → 1.15.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
@@ -62,7 +62,19 @@ var LAYOUT_I18N = {
62
62
  upgradePromptCta: "settings.billing.upgradePrompt.cta",
63
63
  upgradePromptCtaHint: "settings.billing.upgradePrompt.ctaHint",
64
64
  upgradePromptDismiss: "settings.billing.upgradePrompt.dismiss",
65
- 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"
66
78
  };
67
79
  var styles2 = StyleSheet.create({
68
80
  heading: {
@@ -1266,6 +1278,185 @@ var TruncatedText = ({
1266
1278
  );
1267
1279
  };
1268
1280
 
1269
- 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 copy button.
1384
+ *
1385
+ * `flex: 1` IS LOAD-BEARING — do not weaken it to `flexShrink: 1`. A shrink-only wrapper
1386
+ * SHRINK-WRAPS its content, which makes the measured parent width depend on the very string we
1387
+ * are computing: truncate -> wrapper narrows -> re-measure smaller -> truncate harder. Verified
1388
+ * in Chrome: with `flexShrink: 1` a 506px-wide container and a 306px one both rendered the SAME
1389
+ * 196px string, i.e. the id was cut short with 300px of room going spare. `flex: 1` makes the
1390
+ * wrapper fill the row's leftover width instead, so its size is independent of what we put in
1391
+ * it — the same reason `useAvailableWidth` measures the parent and not the element.
1392
+ *
1393
+ * `minWidth: 0` is what actually permits a flex child to go below its content width.
1394
+ */
1395
+ valueWrap: { flex: 1, minWidth: 0 },
1396
+ value: { fontSize: VALUE_FONT_SIZE, fontWeight: "600" },
1397
+ action: {
1398
+ fontSize: ACTION_FONT_SIZE,
1399
+ fontWeight: "600",
1400
+ paddingHorizontal: ACTION_PADDING_H,
1401
+ paddingVertical: ACTION_PADDING_V,
1402
+ borderRadius: ACTION_RADIUS
1403
+ }
1404
+ });
1405
+ var TONE_STEP = "500";
1406
+ function useActionState(status) {
1407
+ const { theme } = useUi();
1408
+ return useMemo(() => {
1409
+ const primary = theme.palette.primary[TONE_STEP];
1410
+ if (status === "copied" /* Copied */) {
1411
+ return { key: LAYOUT_I18N.copied, color: theme.semantic.success?.[TONE_STEP] ?? primary };
1412
+ }
1413
+ if (status === "failed" /* Failed */) {
1414
+ return { key: LAYOUT_I18N.copyFailed, color: theme.semantic.error[TONE_STEP] };
1415
+ }
1416
+ return { key: LAYOUT_I18N.copy, color: primary };
1417
+ }, [status, theme]);
1418
+ }
1419
+ var CopyableId = ({
1420
+ value,
1421
+ label,
1422
+ style,
1423
+ testID,
1424
+ accessibilityLabel,
1425
+ writer
1426
+ }) => {
1427
+ const { theme, t } = useUi();
1428
+ const { colors } = theme;
1429
+ const { status, copy } = useCopyToClipboard(value, writer);
1430
+ const action = useActionState(status);
1431
+ return /* @__PURE__ */ jsxs(View, { style: [styles13.cell, style], testID, children: [
1432
+ label !== void 0 && /* @__PURE__ */ jsx(Text, { style: [styles13.label, { color: colors.textSecondary }], children: label }),
1433
+ /* @__PURE__ */ jsxs(View, { style: styles13.row, children: [
1434
+ /* @__PURE__ */ jsx(View, { style: styles13.valueWrap, children: /* @__PURE__ */ jsx(
1435
+ TruncatedText,
1436
+ {
1437
+ value,
1438
+ style: [styles13.value, { color: colors.text }],
1439
+ testID: `${testID}-value`,
1440
+ accessibilityLabel: accessibilityLabel ?? value,
1441
+ accessibilityHint: t(LAYOUT_I18N.copyableIdHint)
1442
+ }
1443
+ ) }),
1444
+ /* @__PURE__ */ jsx(
1445
+ Text,
1446
+ {
1447
+ onPress: copy,
1448
+ accessibilityRole: "button",
1449
+ accessibilityLabel: t(action.key),
1450
+ accessibilityHint: t(LAYOUT_I18N.copyHint),
1451
+ testID: `${testID}-copy`,
1452
+ style: [styles13.action, { color: action.color }],
1453
+ children: t(action.key)
1454
+ }
1455
+ )
1456
+ ] })
1457
+ ] });
1458
+ };
1459
+
1460
+ 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 };
1270
1461
  //# sourceMappingURL=index.mjs.map
1271
1462
  //# sourceMappingURL=index.mjs.map