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