react-smart-copy 1.0.0 → 1.1.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.cjs CHANGED
@@ -1219,6 +1219,36 @@ function LiveRegion({ message, politeness = "polite", visuallyHidden = true, sty
1219
1219
  });
1220
1220
  }
1221
1221
  //#endregion
1222
+ //#region src/react/TriggerLabel.tsx
1223
+ /**
1224
+ * Default trigger label shared by `CopyField` and `PasteField`: every state's
1225
+ * text is stacked in one grid cell, so the button is always as wide as its
1226
+ * longest label and never resizes (no layout shift). Only the active label is
1227
+ * visible and exposed to assistive tech; the crossfade is skipped when the
1228
+ * user prefers reduced motion.
1229
+ */
1230
+ function TriggerLabel({ status, labels }) {
1231
+ const reduceMotion = useMediaQuery("(prefers-reduced-motion: reduce)");
1232
+ return /* @__PURE__ */ (0, react_jsx_runtime.jsx)("span", {
1233
+ style: { display: "inline-grid" },
1234
+ children: Object.keys(labels).map((candidate) => {
1235
+ const active = candidate === status;
1236
+ return /* @__PURE__ */ (0, react_jsx_runtime.jsx)("span", {
1237
+ "data-trigger-label": candidate,
1238
+ "data-active": active ? "" : void 0,
1239
+ "aria-hidden": !active || void 0,
1240
+ style: {
1241
+ gridArea: "1 / 1",
1242
+ opacity: active ? 1 : 0,
1243
+ visibility: active ? "visible" : "hidden",
1244
+ ...reduceMotion ? {} : { transition: "opacity 150ms ease, visibility 150ms" }
1245
+ },
1246
+ children: labels[candidate]
1247
+ }, candidate);
1248
+ })
1249
+ });
1250
+ }
1251
+ //#endregion
1222
1252
  //#region src/react/CopyField.tsx
1223
1253
  const defaultCopyFieldMessages = {
1224
1254
  copied: "Copied to clipboard",
@@ -1250,7 +1280,7 @@ function getDisplayValue(value) {
1250
1280
  if (value.kind === "text") return value.value;
1251
1281
  return KIND_LABELS[value.kind] ?? null;
1252
1282
  }
1253
- const Root = /* @__PURE__ */ (0, react.forwardRef)(function CopyFieldRoot({ value, label, copyOptions, messages: messageOverrides, alwaysVisible = false, announce = true, pendingDelayMs, minPendingMs, children, onPointerEnter, onPointerLeave, onPointerDown, onFocus, onBlur, ...rest }, forwardedRef) {
1283
+ const Root$1 = /* @__PURE__ */ (0, react.forwardRef)(function CopyFieldRoot({ value, label, copyOptions, messages: messageOverrides, alwaysVisible = false, announce = true, pendingDelayMs, minPendingMs, children, onPointerEnter, onPointerLeave, onPointerDown, onFocus, onBlur, ...rest }, forwardedRef) {
1254
1284
  const copyState = useCopy(copyOptions);
1255
1285
  const displayStatus = useDisplayStatus(copyState.status, {
1256
1286
  ...pendingDelayMs === void 0 ? {} : { pendingDelayMs },
@@ -1310,7 +1340,7 @@ const Root = /* @__PURE__ */ (0, react.forwardRef)(function CopyFieldRoot({ valu
1310
1340
  })
1311
1341
  });
1312
1342
  });
1313
- const Label = /* @__PURE__ */ (0, react.forwardRef)(function CopyFieldLabel({ children, ...rest }, ref) {
1343
+ const Label$1 = /* @__PURE__ */ (0, react.forwardRef)(function CopyFieldLabel({ children, ...rest }, ref) {
1314
1344
  const field = useCopyField();
1315
1345
  return /* @__PURE__ */ (0, react_jsx_runtime.jsx)("span", {
1316
1346
  ...rest,
@@ -1328,44 +1358,12 @@ const Value = /* @__PURE__ */ (0, react.forwardRef)(function CopyFieldValue({ ch
1328
1358
  children: children ?? field.displayValue
1329
1359
  });
1330
1360
  });
1331
- const DEFAULT_TRIGGER_TEXT = {
1361
+ const DEFAULT_TRIGGER_TEXT$1 = {
1332
1362
  idle: "Copy",
1333
1363
  copying: "Copying…",
1334
1364
  copied: "Copied",
1335
1365
  error: "Retry"
1336
1366
  };
1337
- const TRIGGER_STATUSES = [
1338
- "idle",
1339
- "copying",
1340
- "copied",
1341
- "error"
1342
- ];
1343
- /**
1344
- * Default label: every state's text is stacked in the same grid cell, so the
1345
- * button is always as wide as its longest label and never resizes (no layout
1346
- * shift). Only the active one is visible; the others crossfade out.
1347
- */
1348
- function DefaultTriggerLabel({ status }) {
1349
- const reduceMotion = useMediaQuery("(prefers-reduced-motion: reduce)");
1350
- return /* @__PURE__ */ (0, react_jsx_runtime.jsx)("span", {
1351
- style: { display: "inline-grid" },
1352
- children: TRIGGER_STATUSES.map((candidate) => {
1353
- const active = candidate === status;
1354
- return /* @__PURE__ */ (0, react_jsx_runtime.jsx)("span", {
1355
- "data-trigger-label": candidate,
1356
- "data-active": active ? "" : void 0,
1357
- "aria-hidden": active ? void 0 : true,
1358
- style: {
1359
- gridArea: "1 / 1",
1360
- opacity: active ? 1 : 0,
1361
- visibility: active ? "visible" : "hidden",
1362
- ...reduceMotion ? {} : { transition: "opacity 150ms ease, visibility 150ms" }
1363
- },
1364
- children: DEFAULT_TRIGGER_TEXT[candidate]
1365
- }, candidate);
1366
- })
1367
- });
1368
- }
1369
1367
  /**
1370
1368
  * Headless compound component for a labelled, copyable value.
1371
1369
  *
@@ -1375,8 +1373,8 @@ function DefaultTriggerLabel({ status }) {
1375
1373
  * - `data-revealed`: present while the trigger should be visible
1376
1374
  */
1377
1375
  const CopyField = {
1378
- Root,
1379
- Label,
1376
+ Root: Root$1,
1377
+ Label: Label$1,
1380
1378
  Value,
1381
1379
  Trigger: /* @__PURE__ */ (0, react.forwardRef)(function CopyFieldTrigger({ children, onClick, "aria-label": ariaLabel, ...rest }, ref) {
1382
1380
  const field = useCopyField();
@@ -1387,20 +1385,23 @@ const CopyField = {
1387
1385
  revealed: field.revealed,
1388
1386
  canRetry: field.canRetry
1389
1387
  };
1390
- const computedAriaLabel = ariaLabel ?? field.messages.triggerLabel(field.label);
1391
- const safeAriaLabel = computedAriaLabel.length > 0 ? computedAriaLabel : `Copy ${field.label}`;
1388
+ const safeAriaLabel = (ariaLabel ?? field.messages.triggerLabel(field.label)) || defaultCopyFieldMessages.triggerLabel(field.label);
1392
1389
  return /* @__PURE__ */ (0, react_jsx_runtime.jsx)("button", {
1393
1390
  ...rest,
1394
1391
  ref,
1395
1392
  type: "button",
1396
1393
  "aria-label": safeAriaLabel,
1394
+ "aria-busy": field.status === "copying" ? true : void 0,
1397
1395
  "data-state": field.status,
1398
1396
  "data-display-state": field.displayStatus,
1399
1397
  "data-revealed": field.revealed ? "" : void 0,
1400
1398
  onClick: composeEventHandlers(onClick, () => {
1401
1399
  field.copyValue();
1402
1400
  }),
1403
- children: typeof children === "function" ? children(renderProps) : children ?? /* @__PURE__ */ (0, react_jsx_runtime.jsx)(DefaultTriggerLabel, { status: field.displayStatus })
1401
+ children: typeof children === "function" ? children(renderProps) : children ?? /* @__PURE__ */ (0, react_jsx_runtime.jsx)(TriggerLabel, {
1402
+ status: field.displayStatus,
1403
+ labels: DEFAULT_TRIGGER_TEXT$1
1404
+ })
1404
1405
  });
1405
1406
  })
1406
1407
  };
@@ -1452,6 +1453,240 @@ function usePaste(options = {}) {
1452
1453
  ]);
1453
1454
  }
1454
1455
  //#endregion
1456
+ //#region src/react/usePasteDisplayStatus.ts
1457
+ const DEFAULT_PASTE_PENDING_DELAY_MS = 150;
1458
+ const DEFAULT_PASTE_MIN_PENDING_MS = 400;
1459
+ /**
1460
+ * Presentation-only view of a paste status that never flickers.
1461
+ *
1462
+ * The real `status` is honest and can be `reading` for a single render frame
1463
+ * (plain-text reads take ~1-5 ms). Rendering that directly flashes a "Reading…"
1464
+ * label. This hook keeps showing the previous settled status during fast pastes,
1465
+ * and when a paste is genuinely slow it shows `reading` for a minimum time
1466
+ * instead of a blink.
1467
+ *
1468
+ * It never changes the state machine: use `status` for logic, this for pixels.
1469
+ */
1470
+ function usePasteDisplayStatus(status, options = {}) {
1471
+ const pendingDelayMs = Math.max(0, options.pendingDelayMs ?? 150);
1472
+ const minPendingMs = Math.max(0, options.minPendingMs ?? 400);
1473
+ const [settled, setSettled] = (0, react.useState)(status === "reading" ? "idle" : status);
1474
+ useIsomorphicLayoutEffect(() => {
1475
+ if (status !== "reading") setSettled(status);
1476
+ }, [status]);
1477
+ const [pendingShownAt, setPendingShownAt] = (0, react.useState)(null);
1478
+ (0, react.useEffect)(() => {
1479
+ if (status !== "reading") return void 0;
1480
+ const timer = setTimeout(() => {
1481
+ setPendingShownAt(Date.now());
1482
+ }, pendingDelayMs);
1483
+ return () => {
1484
+ clearTimeout(timer);
1485
+ };
1486
+ }, [status, pendingDelayMs]);
1487
+ (0, react.useEffect)(() => {
1488
+ if (status === "reading" || pendingShownAt === null) return void 0;
1489
+ const remaining = Math.max(0, minPendingMs - (Date.now() - pendingShownAt));
1490
+ const timer = setTimeout(() => {
1491
+ setPendingShownAt(null);
1492
+ }, remaining);
1493
+ return () => {
1494
+ clearTimeout(timer);
1495
+ };
1496
+ }, [
1497
+ status,
1498
+ pendingShownAt,
1499
+ minPendingMs
1500
+ ]);
1501
+ if (pendingShownAt !== null) return "reading";
1502
+ return status === "reading" ? settled : status;
1503
+ }
1504
+ //#endregion
1505
+ //#region src/react/PasteField.tsx
1506
+ const defaultPasteFieldMessages = {
1507
+ pasted: "Pasted from clipboard",
1508
+ error: describePasteError,
1509
+ triggerLabel: (label) => `Paste ${label}`,
1510
+ zoneLabel: (label) => `Paste area for ${label}`
1511
+ };
1512
+ const PasteFieldContext = /* @__PURE__ */ (0, react.createContext)(null);
1513
+ /** Access field state to build custom parts. Must be used inside `<PasteField.Root>`. */
1514
+ function usePasteField() {
1515
+ const context = (0, react.useContext)(PasteFieldContext);
1516
+ if (!context) throw new Error("usePasteField() must be used inside <PasteField.Root>.");
1517
+ return context;
1518
+ }
1519
+ const Root = /* @__PURE__ */ (0, react.forwardRef)(function PasteFieldRoot({ label, pasteOptions, messages: messageOverrides, alwaysVisible = false, announce = true, pendingDelayMs, minPendingMs, children, onPointerEnter, onPointerLeave, onPointerDown, onFocus, onBlur, ...rest }, forwardedRef) {
1520
+ const pasteState = usePaste(pasteOptions);
1521
+ const displayStatus = usePasteDisplayStatus(pasteState.status, {
1522
+ ...pendingDelayMs === void 0 ? {} : { pendingDelayMs },
1523
+ ...minPendingMs === void 0 ? {} : { minPendingMs }
1524
+ });
1525
+ const reveal = useRevealOnInteraction({ alwaysVisible });
1526
+ const baseId = (0, react.useId)();
1527
+ const labelId = `${baseId}-label`;
1528
+ const zoneId = `${baseId}-zone`;
1529
+ const messages = (0, react.useMemo)(() => ({
1530
+ ...defaultPasteFieldMessages,
1531
+ ...messageOverrides
1532
+ }), [messageOverrides]);
1533
+ const { paste: machinePaste } = pasteState;
1534
+ const doPaste = (0, react.useCallback)(() => machinePaste(), [machinePaste]);
1535
+ const { targetProps } = reveal;
1536
+ const ref = (0, react.useMemo)(() => mergeRefs(targetProps.ref, forwardedRef), [targetProps.ref, forwardedRef]);
1537
+ const context = (0, react.useMemo)(() => ({
1538
+ ...pasteState,
1539
+ label,
1540
+ labelId,
1541
+ zoneId,
1542
+ revealed: reveal.visible,
1543
+ revealReason: reveal.reason,
1544
+ messages,
1545
+ displayStatus,
1546
+ doPaste
1547
+ }), [
1548
+ pasteState,
1549
+ label,
1550
+ labelId,
1551
+ zoneId,
1552
+ reveal.visible,
1553
+ reveal.reason,
1554
+ messages,
1555
+ displayStatus,
1556
+ doPaste
1557
+ ]);
1558
+ const { state } = pasteState;
1559
+ const announcement = state.status === "read" ? typeof messages.pasted === "function" ? messages.pasted(state.result) : messages.pasted : state.status === "error" ? messages.error(state.error) : "";
1560
+ return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(PasteFieldContext.Provider, {
1561
+ value: context,
1562
+ children: /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("div", {
1563
+ ...rest,
1564
+ ref,
1565
+ "data-state": state.status,
1566
+ "data-display-state": displayStatus,
1567
+ "data-revealed": reveal.visible ? "" : void 0,
1568
+ onPointerEnter: composeEventHandlers(onPointerEnter, targetProps.onPointerEnter),
1569
+ onPointerLeave: composeEventHandlers(onPointerLeave, targetProps.onPointerLeave),
1570
+ onPointerDown: composeEventHandlers(onPointerDown, targetProps.onPointerDown),
1571
+ onFocus: composeEventHandlers(onFocus, targetProps.onFocus),
1572
+ onBlur: composeEventHandlers(onBlur, targetProps.onBlur),
1573
+ children: [children, announce ? /* @__PURE__ */ (0, react_jsx_runtime.jsx)(LiveRegion, { message: announcement }) : null]
1574
+ })
1575
+ });
1576
+ });
1577
+ const Label = /* @__PURE__ */ (0, react.forwardRef)(function PasteFieldLabel({ children, ...rest }, ref) {
1578
+ const field = usePasteField();
1579
+ return /* @__PURE__ */ (0, react_jsx_runtime.jsx)("span", {
1580
+ ...rest,
1581
+ ref,
1582
+ id: field.labelId,
1583
+ children: children ?? field.label
1584
+ });
1585
+ });
1586
+ const DEFAULT_STATUS_LABELS = {
1587
+ idle: "Ready",
1588
+ reading: "Reading…",
1589
+ read: "Pasted",
1590
+ error: "Error"
1591
+ };
1592
+ const Status = /* @__PURE__ */ (0, react.forwardRef)(function PasteFieldStatus({ children, labels, ...rest }, ref) {
1593
+ const field = usePasteField();
1594
+ const resolved = labels ? {
1595
+ ...DEFAULT_STATUS_LABELS,
1596
+ ...labels
1597
+ } : DEFAULT_STATUS_LABELS;
1598
+ return /* @__PURE__ */ (0, react_jsx_runtime.jsx)("span", {
1599
+ ...rest,
1600
+ ref,
1601
+ "data-state": field.status,
1602
+ "data-display-state": field.displayStatus,
1603
+ children: children ?? resolved[field.displayStatus]
1604
+ });
1605
+ });
1606
+ /**
1607
+ * A focusable paste target. Accepts keyboard paste (Ctrl/⌘+V) without a
1608
+ * permission prompt and renders `data-zone` for styling.
1609
+ *
1610
+ * Accessible name: `aria-label` prop, else `messages.zoneLabel(label)`
1611
+ * ("Paste area for Notes"). Pass `aria-labelledby` explicitly to name it from
1612
+ * another element instead.
1613
+ */
1614
+ const Zone = /* @__PURE__ */ (0, react.forwardRef)(function PasteFieldZone({ children, focusable = true, onPaste, "aria-label": ariaLabel, tabIndex, ...rest }, ref) {
1615
+ const field = usePasteField();
1616
+ const { targetProps } = field;
1617
+ const safeAriaLabel = (ariaLabel ?? field.messages.zoneLabel(field.label)) || defaultPasteFieldMessages.zoneLabel(field.label);
1618
+ return /* @__PURE__ */ (0, react_jsx_runtime.jsx)("div", {
1619
+ "aria-keyshortcuts": "Control+V Meta+V",
1620
+ ...rest,
1621
+ ref,
1622
+ id: field.zoneId,
1623
+ role: "region",
1624
+ "aria-label": safeAriaLabel,
1625
+ "data-zone": "",
1626
+ "data-state": field.status,
1627
+ "data-display-state": field.displayStatus,
1628
+ tabIndex: tabIndex ?? (focusable ? 0 : void 0),
1629
+ onPaste: composeEventHandlers(onPaste, targetProps.onPaste),
1630
+ children
1631
+ });
1632
+ });
1633
+ const DEFAULT_TRIGGER_TEXT = {
1634
+ idle: "Paste",
1635
+ reading: "Pasting…",
1636
+ read: "Pasted",
1637
+ error: "Retry"
1638
+ };
1639
+ /**
1640
+ * Headless compound component for a labelled, paste-ready field.
1641
+ *
1642
+ * Styling hooks on Root, Zone and Trigger:
1643
+ * - `data-display-state="idle|reading|read|error"`: flicker-free, style with this
1644
+ * - `data-state`: the real, instantaneous state, for logic and tests
1645
+ * - `data-revealed`: present while the trigger should be visible
1646
+ *
1647
+ * @example
1648
+ * ```tsx
1649
+ * <PasteField.Root label="Notes">
1650
+ * <PasteField.Label />
1651
+ * <PasteField.Zone />
1652
+ * <PasteField.Trigger />
1653
+ * </PasteField.Root>
1654
+ * ```
1655
+ */
1656
+ const PasteField = {
1657
+ Root,
1658
+ Label,
1659
+ Status,
1660
+ Zone,
1661
+ Trigger: /* @__PURE__ */ (0, react.forwardRef)(function PasteFieldTrigger({ children, onClick, "aria-label": ariaLabel, ...rest }, ref) {
1662
+ const field = usePasteField();
1663
+ const renderProps = {
1664
+ status: field.status,
1665
+ displayStatus: field.displayStatus,
1666
+ state: field.state,
1667
+ revealed: field.revealed
1668
+ };
1669
+ const safeAriaLabel = (ariaLabel ?? field.messages.triggerLabel(field.label)) || defaultPasteFieldMessages.triggerLabel(field.label);
1670
+ return /* @__PURE__ */ (0, react_jsx_runtime.jsx)("button", {
1671
+ ...rest,
1672
+ ref,
1673
+ type: "button",
1674
+ "aria-label": safeAriaLabel,
1675
+ "aria-busy": field.status === "reading" ? true : void 0,
1676
+ "data-state": field.status,
1677
+ "data-display-state": field.displayStatus,
1678
+ "data-revealed": field.revealed ? "" : void 0,
1679
+ onClick: composeEventHandlers(onClick, () => {
1680
+ field.doPaste();
1681
+ }),
1682
+ children: typeof children === "function" ? children(renderProps) : children ?? /* @__PURE__ */ (0, react_jsx_runtime.jsx)(TriggerLabel, {
1683
+ status: field.displayStatus,
1684
+ labels: DEFAULT_TRIGGER_TEXT
1685
+ })
1686
+ });
1687
+ })
1688
+ };
1689
+ //#endregion
1455
1690
  exports.CopyFailure = CopyFailure;
1456
1691
  exports.CopyField = CopyField;
1457
1692
  exports.CopyGroup = CopyGroup;
@@ -1460,12 +1695,15 @@ exports.DEFAULT_MIN_PENDING_MS = DEFAULT_MIN_PENDING_MS;
1460
1695
  exports.DEFAULT_PASTE_ACCEPT = DEFAULT_PASTE_ACCEPT;
1461
1696
  exports.DEFAULT_PASTE_MAX_BYTES = DEFAULT_PASTE_MAX_BYTES;
1462
1697
  exports.DEFAULT_PASTE_MAX_ITEMS = DEFAULT_PASTE_MAX_ITEMS;
1698
+ exports.DEFAULT_PASTE_MIN_PENDING_MS = DEFAULT_PASTE_MIN_PENDING_MS;
1699
+ exports.DEFAULT_PASTE_PENDING_DELAY_MS = DEFAULT_PASTE_PENDING_DELAY_MS;
1463
1700
  exports.DEFAULT_PENDING_DELAY_MS = DEFAULT_PENDING_DELAY_MS;
1464
1701
  exports.DEFAULT_RESET_AFTER_MS = DEFAULT_RESET_AFTER_MS;
1465
1702
  exports.IDLE_STATE = IDLE_STATE;
1466
1703
  exports.IMAGE_MIME_TYPE = IMAGE_MIME_TYPE;
1467
1704
  exports.LiveRegion = LiveRegion;
1468
1705
  exports.PASTE_IDLE_STATE = PASTE_IDLE_STATE;
1706
+ exports.PasteField = PasteField;
1469
1707
  exports.SVG_MIME_TYPE = SVG_MIME_TYPE;
1470
1708
  exports.canRetryState = canRetryState;
1471
1709
  exports.copyFailure = copyFailure;
@@ -1476,6 +1714,7 @@ exports.createCopyError = createCopyError;
1476
1714
  exports.createCopyMachine = createCopyMachine;
1477
1715
  exports.createPasteMachine = createPasteMachine;
1478
1716
  exports.defaultCopyFieldMessages = defaultCopyFieldMessages;
1717
+ exports.defaultPasteFieldMessages = defaultPasteFieldMessages;
1479
1718
  exports.describeCopyError = describeCopyError;
1480
1719
  exports.describePasteError = describePasteError;
1481
1720
  exports.isBlob = isBlob;
@@ -1496,6 +1735,8 @@ exports.useCopyField = useCopyField;
1496
1735
  exports.useCopyGroup = useCopyGroup;
1497
1736
  exports.useDisplayStatus = useDisplayStatus;
1498
1737
  exports.usePaste = usePaste;
1738
+ exports.usePasteDisplayStatus = usePasteDisplayStatus;
1739
+ exports.usePasteField = usePasteField;
1499
1740
  exports.useRevealOnInteraction = useRevealOnInteraction;
1500
1741
  exports.validatePayload = validatePayload;
1501
1742
  exports.visuallyHiddenStyle = visuallyHiddenStyle;