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 +269 -8
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +111 -1
- package/dist/index.d.ts +111 -1
- package/dist/index.js +264 -9
- package/dist/index.js.map +1 -1
- package/package.json +7 -1
package/dist/index.d.cts
CHANGED
|
@@ -690,5 +690,115 @@ export declare function CopyGroup({ children, coordinator }: CopyGroupProps): im
|
|
|
690
690
|
/** The nearest `<CopyGroup>`'s coordinator, or `null` outside any group. */
|
|
691
691
|
export declare function useCopyGroup(): CopyCoordinator | null;
|
|
692
692
|
//#endregion
|
|
693
|
-
|
|
693
|
+
//#region src/react/usePasteDisplayStatus.d.ts
|
|
694
|
+
export declare const DEFAULT_PASTE_PENDING_DELAY_MS = 150;
|
|
695
|
+
export declare const DEFAULT_PASTE_MIN_PENDING_MS = 400;
|
|
696
|
+
interface UsePasteDisplayStatusOptions {
|
|
697
|
+
/** Only show `reading` if the paste takes longer than this. Default 150 ms. */
|
|
698
|
+
readonly pendingDelayMs?: number;
|
|
699
|
+
/** Once `reading` is shown, keep it visible at least this long. Default 400 ms. */
|
|
700
|
+
readonly minPendingMs?: number;
|
|
701
|
+
}
|
|
702
|
+
/**
|
|
703
|
+
* Presentation-only view of a paste status that never flickers.
|
|
704
|
+
*
|
|
705
|
+
* The real `status` is honest and can be `reading` for a single render frame
|
|
706
|
+
* (plain-text reads take ~1-5 ms). Rendering that directly flashes a "Reading…"
|
|
707
|
+
* label. This hook keeps showing the previous settled status during fast pastes,
|
|
708
|
+
* and when a paste is genuinely slow it shows `reading` for a minimum time
|
|
709
|
+
* instead of a blink.
|
|
710
|
+
*
|
|
711
|
+
* It never changes the state machine: use `status` for logic, this for pixels.
|
|
712
|
+
*/
|
|
713
|
+
export declare function usePasteDisplayStatus(status: PasteStatus, options?: UsePasteDisplayStatusOptions): PasteStatus;
|
|
714
|
+
//#endregion
|
|
715
|
+
//#region src/react/PasteField.d.ts
|
|
716
|
+
interface PasteFieldMessages {
|
|
717
|
+
/** Announced to screen readers on successful paste. */
|
|
718
|
+
readonly pasted: string | ((result: PasteResult) => string);
|
|
719
|
+
/** Announced on failure. Defaults to {@link describePasteError}. */
|
|
720
|
+
readonly error: (error: CopyError) => string;
|
|
721
|
+
/**
|
|
722
|
+
* Accessible name of the trigger button. Receives the field's `label` prop.
|
|
723
|
+
* The returned string must not be empty.
|
|
724
|
+
*/
|
|
725
|
+
readonly triggerLabel: (label: string) => string;
|
|
726
|
+
/**
|
|
727
|
+
* Accessible name of the drop zone / paste target area.
|
|
728
|
+
* Receives the field's `label` prop.
|
|
729
|
+
*/
|
|
730
|
+
readonly zoneLabel: (label: string) => string;
|
|
731
|
+
}
|
|
732
|
+
export declare const defaultPasteFieldMessages: PasteFieldMessages;
|
|
733
|
+
interface PasteFieldContextValue extends UsePasteResult {
|
|
734
|
+
readonly label: string;
|
|
735
|
+
readonly labelId: string;
|
|
736
|
+
readonly zoneId: string;
|
|
737
|
+
readonly revealed: boolean;
|
|
738
|
+
readonly revealReason: RevealReason | null;
|
|
739
|
+
readonly messages: PasteFieldMessages;
|
|
740
|
+
/** Flicker-free status for rendering. Use `status` for logic, this for pixels. */
|
|
741
|
+
readonly displayStatus: PasteStatus;
|
|
742
|
+
/** Triggers a programmatic paste (via Clipboard API, may prompt for permission). */
|
|
743
|
+
readonly doPaste: () => Promise<PasteOutcome>;
|
|
744
|
+
}
|
|
745
|
+
/** Access field state to build custom parts. Must be used inside `<PasteField.Root>`. */
|
|
746
|
+
export declare function usePasteField(): PasteFieldContextValue;
|
|
747
|
+
interface PasteFieldRootProps extends Omit<HTMLAttributes<HTMLDivElement>, 'onPaste' | 'onError'>, UsePasteDisplayStatusOptions {
|
|
748
|
+
/** Human label — used for the trigger's and zone's accessible names. */
|
|
749
|
+
readonly label: string;
|
|
750
|
+
readonly pasteOptions?: UsePasteOptions;
|
|
751
|
+
readonly messages?: Partial<PasteFieldMessages>;
|
|
752
|
+
/** Skip hover/focus reveal and always show the trigger. */
|
|
753
|
+
readonly alwaysVisible?: boolean;
|
|
754
|
+
/** Render the built-in live region. Disable if your app has a global announcer. Default `true`. */
|
|
755
|
+
readonly announce?: boolean;
|
|
756
|
+
}
|
|
757
|
+
type PasteFieldLabelProps = HTMLAttributes<HTMLSpanElement>;
|
|
758
|
+
interface PasteFieldStatusProps extends HTMLAttributes<HTMLSpanElement> {
|
|
759
|
+
/** Map each paste status to a label. Defaults to English strings. */
|
|
760
|
+
readonly labels?: Partial<Readonly<Record<PasteStatus, string>>>;
|
|
761
|
+
}
|
|
762
|
+
interface PasteFieldZoneProps extends HTMLAttributes<HTMLDivElement> {
|
|
763
|
+
/** When true, the zone is always focusable (tabIndex=0). Default true. */
|
|
764
|
+
readonly focusable?: boolean;
|
|
765
|
+
}
|
|
766
|
+
interface PasteFieldTriggerRenderProps {
|
|
767
|
+
/** The real state. Use for logic. */
|
|
768
|
+
readonly status: PasteStatus;
|
|
769
|
+
/** Flicker-free state. Use for what you render. */
|
|
770
|
+
readonly displayStatus: PasteStatus;
|
|
771
|
+
readonly state: PasteState;
|
|
772
|
+
readonly revealed: boolean;
|
|
773
|
+
}
|
|
774
|
+
interface PasteFieldTriggerProps extends Omit<ButtonHTMLAttributes<HTMLButtonElement>, 'children' | 'type'> {
|
|
775
|
+
/** Static content, or a render function receiving the live state. */
|
|
776
|
+
readonly children?: ReactNode | ((props: PasteFieldTriggerRenderProps) => ReactNode);
|
|
777
|
+
}
|
|
778
|
+
/**
|
|
779
|
+
* Headless compound component for a labelled, paste-ready field.
|
|
780
|
+
*
|
|
781
|
+
* Styling hooks on Root, Zone and Trigger:
|
|
782
|
+
* - `data-display-state="idle|reading|read|error"`: flicker-free, style with this
|
|
783
|
+
* - `data-state`: the real, instantaneous state, for logic and tests
|
|
784
|
+
* - `data-revealed`: present while the trigger should be visible
|
|
785
|
+
*
|
|
786
|
+
* @example
|
|
787
|
+
* ```tsx
|
|
788
|
+
* <PasteField.Root label="Notes">
|
|
789
|
+
* <PasteField.Label />
|
|
790
|
+
* <PasteField.Zone />
|
|
791
|
+
* <PasteField.Trigger />
|
|
792
|
+
* </PasteField.Root>
|
|
793
|
+
* ```
|
|
794
|
+
*/
|
|
795
|
+
export declare const PasteField: {
|
|
796
|
+
readonly Root: import("react").ForwardRefExoticComponent<PasteFieldRootProps & import("react").RefAttributes<HTMLDivElement>>;
|
|
797
|
+
readonly Label: import("react").ForwardRefExoticComponent<PasteFieldLabelProps & import("react").RefAttributes<HTMLSpanElement>>;
|
|
798
|
+
readonly Status: import("react").ForwardRefExoticComponent<PasteFieldStatusProps & import("react").RefAttributes<HTMLSpanElement>>;
|
|
799
|
+
readonly Zone: import("react").ForwardRefExoticComponent<PasteFieldZoneProps & import("react").RefAttributes<HTMLDivElement>>;
|
|
800
|
+
readonly Trigger: import("react").ForwardRefExoticComponent<PasteFieldTriggerProps & import("react").RefAttributes<HTMLButtonElement>>;
|
|
801
|
+
};
|
|
802
|
+
//#endregion
|
|
803
|
+
export type { BrowserPasteAdapterOptions, ClipboardItemLike, CopyFieldContextValue, CopyFieldLabelProps, CopyFieldMessages, CopyFieldRootProps, CopyFieldTriggerProps, CopyFieldTriggerRenderProps, CopyFieldValueProps, CopyGroupProps, DataTransferLike, LiveRegionProps, PasteAccept, PasteAcceptShorthand, PasteAdapter, PasteClipboardLike, PasteEnvironment, PasteFieldContextValue, PasteFieldLabelProps, PasteFieldMessages, PasteFieldRootProps, PasteFieldStatusProps, PasteFieldTriggerProps, PasteFieldTriggerRenderProps, PasteFieldZoneProps, PasteItem, PasteLimits, PasteReadOptions, PasteResult, PasteSource, PasteTargetProps, ResolvedPasteReadOptions, RevealReason, RevealTargetProps, UseCopyOptions, UseCopyResult, UseDisplayStatusOptions, UsePasteDisplayStatusOptions, UsePasteOptions, UsePasteResult, UseRevealOnInteractionOptions, UseRevealOnInteractionResult };
|
|
694
804
|
//# sourceMappingURL=index.d.cts.map
|
package/dist/index.d.ts
CHANGED
|
@@ -690,5 +690,115 @@ export declare function CopyGroup({ children, coordinator }: CopyGroupProps): im
|
|
|
690
690
|
/** The nearest `<CopyGroup>`'s coordinator, or `null` outside any group. */
|
|
691
691
|
export declare function useCopyGroup(): CopyCoordinator | null;
|
|
692
692
|
//#endregion
|
|
693
|
-
|
|
693
|
+
//#region src/react/usePasteDisplayStatus.d.ts
|
|
694
|
+
export declare const DEFAULT_PASTE_PENDING_DELAY_MS = 150;
|
|
695
|
+
export declare const DEFAULT_PASTE_MIN_PENDING_MS = 400;
|
|
696
|
+
interface UsePasteDisplayStatusOptions {
|
|
697
|
+
/** Only show `reading` if the paste takes longer than this. Default 150 ms. */
|
|
698
|
+
readonly pendingDelayMs?: number;
|
|
699
|
+
/** Once `reading` is shown, keep it visible at least this long. Default 400 ms. */
|
|
700
|
+
readonly minPendingMs?: number;
|
|
701
|
+
}
|
|
702
|
+
/**
|
|
703
|
+
* Presentation-only view of a paste status that never flickers.
|
|
704
|
+
*
|
|
705
|
+
* The real `status` is honest and can be `reading` for a single render frame
|
|
706
|
+
* (plain-text reads take ~1-5 ms). Rendering that directly flashes a "Reading…"
|
|
707
|
+
* label. This hook keeps showing the previous settled status during fast pastes,
|
|
708
|
+
* and when a paste is genuinely slow it shows `reading` for a minimum time
|
|
709
|
+
* instead of a blink.
|
|
710
|
+
*
|
|
711
|
+
* It never changes the state machine: use `status` for logic, this for pixels.
|
|
712
|
+
*/
|
|
713
|
+
export declare function usePasteDisplayStatus(status: PasteStatus, options?: UsePasteDisplayStatusOptions): PasteStatus;
|
|
714
|
+
//#endregion
|
|
715
|
+
//#region src/react/PasteField.d.ts
|
|
716
|
+
interface PasteFieldMessages {
|
|
717
|
+
/** Announced to screen readers on successful paste. */
|
|
718
|
+
readonly pasted: string | ((result: PasteResult) => string);
|
|
719
|
+
/** Announced on failure. Defaults to {@link describePasteError}. */
|
|
720
|
+
readonly error: (error: CopyError) => string;
|
|
721
|
+
/**
|
|
722
|
+
* Accessible name of the trigger button. Receives the field's `label` prop.
|
|
723
|
+
* The returned string must not be empty.
|
|
724
|
+
*/
|
|
725
|
+
readonly triggerLabel: (label: string) => string;
|
|
726
|
+
/**
|
|
727
|
+
* Accessible name of the drop zone / paste target area.
|
|
728
|
+
* Receives the field's `label` prop.
|
|
729
|
+
*/
|
|
730
|
+
readonly zoneLabel: (label: string) => string;
|
|
731
|
+
}
|
|
732
|
+
export declare const defaultPasteFieldMessages: PasteFieldMessages;
|
|
733
|
+
interface PasteFieldContextValue extends UsePasteResult {
|
|
734
|
+
readonly label: string;
|
|
735
|
+
readonly labelId: string;
|
|
736
|
+
readonly zoneId: string;
|
|
737
|
+
readonly revealed: boolean;
|
|
738
|
+
readonly revealReason: RevealReason | null;
|
|
739
|
+
readonly messages: PasteFieldMessages;
|
|
740
|
+
/** Flicker-free status for rendering. Use `status` for logic, this for pixels. */
|
|
741
|
+
readonly displayStatus: PasteStatus;
|
|
742
|
+
/** Triggers a programmatic paste (via Clipboard API, may prompt for permission). */
|
|
743
|
+
readonly doPaste: () => Promise<PasteOutcome>;
|
|
744
|
+
}
|
|
745
|
+
/** Access field state to build custom parts. Must be used inside `<PasteField.Root>`. */
|
|
746
|
+
export declare function usePasteField(): PasteFieldContextValue;
|
|
747
|
+
interface PasteFieldRootProps extends Omit<HTMLAttributes<HTMLDivElement>, 'onPaste' | 'onError'>, UsePasteDisplayStatusOptions {
|
|
748
|
+
/** Human label — used for the trigger's and zone's accessible names. */
|
|
749
|
+
readonly label: string;
|
|
750
|
+
readonly pasteOptions?: UsePasteOptions;
|
|
751
|
+
readonly messages?: Partial<PasteFieldMessages>;
|
|
752
|
+
/** Skip hover/focus reveal and always show the trigger. */
|
|
753
|
+
readonly alwaysVisible?: boolean;
|
|
754
|
+
/** Render the built-in live region. Disable if your app has a global announcer. Default `true`. */
|
|
755
|
+
readonly announce?: boolean;
|
|
756
|
+
}
|
|
757
|
+
type PasteFieldLabelProps = HTMLAttributes<HTMLSpanElement>;
|
|
758
|
+
interface PasteFieldStatusProps extends HTMLAttributes<HTMLSpanElement> {
|
|
759
|
+
/** Map each paste status to a label. Defaults to English strings. */
|
|
760
|
+
readonly labels?: Partial<Readonly<Record<PasteStatus, string>>>;
|
|
761
|
+
}
|
|
762
|
+
interface PasteFieldZoneProps extends HTMLAttributes<HTMLDivElement> {
|
|
763
|
+
/** When true, the zone is always focusable (tabIndex=0). Default true. */
|
|
764
|
+
readonly focusable?: boolean;
|
|
765
|
+
}
|
|
766
|
+
interface PasteFieldTriggerRenderProps {
|
|
767
|
+
/** The real state. Use for logic. */
|
|
768
|
+
readonly status: PasteStatus;
|
|
769
|
+
/** Flicker-free state. Use for what you render. */
|
|
770
|
+
readonly displayStatus: PasteStatus;
|
|
771
|
+
readonly state: PasteState;
|
|
772
|
+
readonly revealed: boolean;
|
|
773
|
+
}
|
|
774
|
+
interface PasteFieldTriggerProps extends Omit<ButtonHTMLAttributes<HTMLButtonElement>, 'children' | 'type'> {
|
|
775
|
+
/** Static content, or a render function receiving the live state. */
|
|
776
|
+
readonly children?: ReactNode | ((props: PasteFieldTriggerRenderProps) => ReactNode);
|
|
777
|
+
}
|
|
778
|
+
/**
|
|
779
|
+
* Headless compound component for a labelled, paste-ready field.
|
|
780
|
+
*
|
|
781
|
+
* Styling hooks on Root, Zone and Trigger:
|
|
782
|
+
* - `data-display-state="idle|reading|read|error"`: flicker-free, style with this
|
|
783
|
+
* - `data-state`: the real, instantaneous state, for logic and tests
|
|
784
|
+
* - `data-revealed`: present while the trigger should be visible
|
|
785
|
+
*
|
|
786
|
+
* @example
|
|
787
|
+
* ```tsx
|
|
788
|
+
* <PasteField.Root label="Notes">
|
|
789
|
+
* <PasteField.Label />
|
|
790
|
+
* <PasteField.Zone />
|
|
791
|
+
* <PasteField.Trigger />
|
|
792
|
+
* </PasteField.Root>
|
|
793
|
+
* ```
|
|
794
|
+
*/
|
|
795
|
+
export declare const PasteField: {
|
|
796
|
+
readonly Root: import("react").ForwardRefExoticComponent<PasteFieldRootProps & import("react").RefAttributes<HTMLDivElement>>;
|
|
797
|
+
readonly Label: import("react").ForwardRefExoticComponent<PasteFieldLabelProps & import("react").RefAttributes<HTMLSpanElement>>;
|
|
798
|
+
readonly Status: import("react").ForwardRefExoticComponent<PasteFieldStatusProps & import("react").RefAttributes<HTMLSpanElement>>;
|
|
799
|
+
readonly Zone: import("react").ForwardRefExoticComponent<PasteFieldZoneProps & import("react").RefAttributes<HTMLDivElement>>;
|
|
800
|
+
readonly Trigger: import("react").ForwardRefExoticComponent<PasteFieldTriggerProps & import("react").RefAttributes<HTMLButtonElement>>;
|
|
801
|
+
};
|
|
802
|
+
//#endregion
|
|
803
|
+
export type { BrowserPasteAdapterOptions, ClipboardItemLike, CopyFieldContextValue, CopyFieldLabelProps, CopyFieldMessages, CopyFieldRootProps, CopyFieldTriggerProps, CopyFieldTriggerRenderProps, CopyFieldValueProps, CopyGroupProps, DataTransferLike, LiveRegionProps, PasteAccept, PasteAcceptShorthand, PasteAdapter, PasteClipboardLike, PasteEnvironment, PasteFieldContextValue, PasteFieldLabelProps, PasteFieldMessages, PasteFieldRootProps, PasteFieldStatusProps, PasteFieldTriggerProps, PasteFieldTriggerRenderProps, PasteFieldZoneProps, PasteItem, PasteLimits, PasteReadOptions, PasteResult, PasteSource, PasteTargetProps, ResolvedPasteReadOptions, RevealReason, RevealTargetProps, UseCopyOptions, UseCopyResult, UseDisplayStatusOptions, UsePasteDisplayStatusOptions, UsePasteOptions, UsePasteResult, UseRevealOnInteractionOptions, UseRevealOnInteractionResult };
|
|
694
804
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.js
CHANGED
|
@@ -1249,7 +1249,7 @@ function getDisplayValue(value) {
|
|
|
1249
1249
|
if (value.kind === "text") return value.value;
|
|
1250
1250
|
return KIND_LABELS[value.kind] ?? null;
|
|
1251
1251
|
}
|
|
1252
|
-
const Root = /* @__PURE__ */ forwardRef(function CopyFieldRoot({ value, label, copyOptions, messages: messageOverrides, alwaysVisible = false, announce = true, pendingDelayMs, minPendingMs, children, onPointerEnter, onPointerLeave, onPointerDown, onFocus, onBlur, ...rest }, forwardedRef) {
|
|
1252
|
+
const Root$1 = /* @__PURE__ */ forwardRef(function CopyFieldRoot({ value, label, copyOptions, messages: messageOverrides, alwaysVisible = false, announce = true, pendingDelayMs, minPendingMs, children, onPointerEnter, onPointerLeave, onPointerDown, onFocus, onBlur, ...rest }, forwardedRef) {
|
|
1253
1253
|
const copyState = useCopy(copyOptions);
|
|
1254
1254
|
const displayStatus = useDisplayStatus(copyState.status, {
|
|
1255
1255
|
...pendingDelayMs === void 0 ? {} : { pendingDelayMs },
|
|
@@ -1309,7 +1309,7 @@ const Root = /* @__PURE__ */ forwardRef(function CopyFieldRoot({ value, label, c
|
|
|
1309
1309
|
})
|
|
1310
1310
|
});
|
|
1311
1311
|
});
|
|
1312
|
-
const Label = /* @__PURE__ */ forwardRef(function CopyFieldLabel({ children, ...rest }, ref) {
|
|
1312
|
+
const Label$1 = /* @__PURE__ */ forwardRef(function CopyFieldLabel({ children, ...rest }, ref) {
|
|
1313
1313
|
const field = useCopyField();
|
|
1314
1314
|
return /* @__PURE__ */ jsx("span", {
|
|
1315
1315
|
...rest,
|
|
@@ -1327,7 +1327,7 @@ const Value = /* @__PURE__ */ forwardRef(function CopyFieldValue({ children, ...
|
|
|
1327
1327
|
children: children ?? field.displayValue
|
|
1328
1328
|
});
|
|
1329
1329
|
});
|
|
1330
|
-
const DEFAULT_TRIGGER_TEXT = {
|
|
1330
|
+
const DEFAULT_TRIGGER_TEXT$1 = {
|
|
1331
1331
|
idle: "Copy",
|
|
1332
1332
|
copying: "Copying…",
|
|
1333
1333
|
copied: "Copied",
|
|
@@ -1344,7 +1344,7 @@ const TRIGGER_STATUSES = [
|
|
|
1344
1344
|
* button is always as wide as its longest label and never resizes (no layout
|
|
1345
1345
|
* shift). Only the active one is visible; the others crossfade out.
|
|
1346
1346
|
*/
|
|
1347
|
-
function DefaultTriggerLabel({ status }) {
|
|
1347
|
+
function DefaultTriggerLabel$1({ status }) {
|
|
1348
1348
|
const reduceMotion = useMediaQuery("(prefers-reduced-motion: reduce)");
|
|
1349
1349
|
return /* @__PURE__ */ jsx("span", {
|
|
1350
1350
|
style: { display: "inline-grid" },
|
|
@@ -1360,7 +1360,7 @@ function DefaultTriggerLabel({ status }) {
|
|
|
1360
1360
|
visibility: active ? "visible" : "hidden",
|
|
1361
1361
|
...reduceMotion ? {} : { transition: "opacity 150ms ease, visibility 150ms" }
|
|
1362
1362
|
},
|
|
1363
|
-
children: DEFAULT_TRIGGER_TEXT[candidate]
|
|
1363
|
+
children: DEFAULT_TRIGGER_TEXT$1[candidate]
|
|
1364
1364
|
}, candidate);
|
|
1365
1365
|
})
|
|
1366
1366
|
});
|
|
@@ -1374,8 +1374,8 @@ function DefaultTriggerLabel({ status }) {
|
|
|
1374
1374
|
* - `data-revealed`: present while the trigger should be visible
|
|
1375
1375
|
*/
|
|
1376
1376
|
const CopyField = {
|
|
1377
|
-
Root,
|
|
1378
|
-
Label,
|
|
1377
|
+
Root: Root$1,
|
|
1378
|
+
Label: Label$1,
|
|
1379
1379
|
Value,
|
|
1380
1380
|
Trigger: /* @__PURE__ */ forwardRef(function CopyFieldTrigger({ children, onClick, "aria-label": ariaLabel, ...rest }, ref) {
|
|
1381
1381
|
const field = useCopyField();
|
|
@@ -1399,7 +1399,7 @@ const CopyField = {
|
|
|
1399
1399
|
onClick: composeEventHandlers(onClick, () => {
|
|
1400
1400
|
field.copyValue();
|
|
1401
1401
|
}),
|
|
1402
|
-
children: typeof children === "function" ? children(renderProps) : children ?? /* @__PURE__ */ jsx(DefaultTriggerLabel, { status: field.displayStatus })
|
|
1402
|
+
children: typeof children === "function" ? children(renderProps) : children ?? /* @__PURE__ */ jsx(DefaultTriggerLabel$1, { status: field.displayStatus })
|
|
1403
1403
|
});
|
|
1404
1404
|
})
|
|
1405
1405
|
};
|
|
@@ -1451,6 +1451,261 @@ function usePaste(options = {}) {
|
|
|
1451
1451
|
]);
|
|
1452
1452
|
}
|
|
1453
1453
|
//#endregion
|
|
1454
|
-
|
|
1454
|
+
//#region src/react/usePasteDisplayStatus.ts
|
|
1455
|
+
const DEFAULT_PASTE_PENDING_DELAY_MS = 150;
|
|
1456
|
+
const DEFAULT_PASTE_MIN_PENDING_MS = 400;
|
|
1457
|
+
/**
|
|
1458
|
+
* Presentation-only view of a paste status that never flickers.
|
|
1459
|
+
*
|
|
1460
|
+
* The real `status` is honest and can be `reading` for a single render frame
|
|
1461
|
+
* (plain-text reads take ~1-5 ms). Rendering that directly flashes a "Reading…"
|
|
1462
|
+
* label. This hook keeps showing the previous settled status during fast pastes,
|
|
1463
|
+
* and when a paste is genuinely slow it shows `reading` for a minimum time
|
|
1464
|
+
* instead of a blink.
|
|
1465
|
+
*
|
|
1466
|
+
* It never changes the state machine: use `status` for logic, this for pixels.
|
|
1467
|
+
*/
|
|
1468
|
+
function usePasteDisplayStatus(status, options = {}) {
|
|
1469
|
+
const pendingDelayMs = Math.max(0, options.pendingDelayMs ?? 150);
|
|
1470
|
+
const minPendingMs = Math.max(0, options.minPendingMs ?? 400);
|
|
1471
|
+
const [settled, setSettled] = useState(status === "reading" ? "idle" : status);
|
|
1472
|
+
useIsomorphicLayoutEffect(() => {
|
|
1473
|
+
if (status !== "reading") setSettled(status);
|
|
1474
|
+
}, [status]);
|
|
1475
|
+
const [pendingShownAt, setPendingShownAt] = useState(null);
|
|
1476
|
+
useEffect(() => {
|
|
1477
|
+
if (status !== "reading") return void 0;
|
|
1478
|
+
const timer = setTimeout(() => {
|
|
1479
|
+
setPendingShownAt(Date.now());
|
|
1480
|
+
}, pendingDelayMs);
|
|
1481
|
+
return () => {
|
|
1482
|
+
clearTimeout(timer);
|
|
1483
|
+
};
|
|
1484
|
+
}, [status, pendingDelayMs]);
|
|
1485
|
+
useEffect(() => {
|
|
1486
|
+
if (status === "reading" || pendingShownAt === null) return void 0;
|
|
1487
|
+
const remaining = Math.max(0, minPendingMs - (Date.now() - pendingShownAt));
|
|
1488
|
+
const timer = setTimeout(() => {
|
|
1489
|
+
setPendingShownAt(null);
|
|
1490
|
+
}, remaining);
|
|
1491
|
+
return () => {
|
|
1492
|
+
clearTimeout(timer);
|
|
1493
|
+
};
|
|
1494
|
+
}, [
|
|
1495
|
+
status,
|
|
1496
|
+
pendingShownAt,
|
|
1497
|
+
minPendingMs
|
|
1498
|
+
]);
|
|
1499
|
+
if (pendingShownAt !== null) return "reading";
|
|
1500
|
+
return status === "reading" ? settled : status;
|
|
1501
|
+
}
|
|
1502
|
+
//#endregion
|
|
1503
|
+
//#region src/react/PasteField.tsx
|
|
1504
|
+
const defaultPasteFieldMessages = {
|
|
1505
|
+
pasted: "Pasted from clipboard",
|
|
1506
|
+
error: describePasteError,
|
|
1507
|
+
triggerLabel: (label) => `Paste ${label}`,
|
|
1508
|
+
zoneLabel: (label) => `Paste area for ${label}`
|
|
1509
|
+
};
|
|
1510
|
+
const PasteFieldContext = /* @__PURE__ */ createContext(null);
|
|
1511
|
+
/** Access field state to build custom parts. Must be used inside `<PasteField.Root>`. */
|
|
1512
|
+
function usePasteField() {
|
|
1513
|
+
const context = useContext(PasteFieldContext);
|
|
1514
|
+
if (!context) throw new Error("usePasteField() must be used inside <PasteField.Root>.");
|
|
1515
|
+
return context;
|
|
1516
|
+
}
|
|
1517
|
+
const Root = /* @__PURE__ */ forwardRef(function PasteFieldRoot({ label, pasteOptions, messages: messageOverrides, alwaysVisible = false, announce = true, pendingDelayMs, minPendingMs, children, onPointerEnter, onPointerLeave, onPointerDown, onFocus, onBlur, ...rest }, forwardedRef) {
|
|
1518
|
+
const pasteState = usePaste(pasteOptions);
|
|
1519
|
+
const displayStatus = usePasteDisplayStatus(pasteState.status, {
|
|
1520
|
+
...pendingDelayMs === void 0 ? {} : { pendingDelayMs },
|
|
1521
|
+
...minPendingMs === void 0 ? {} : { minPendingMs }
|
|
1522
|
+
});
|
|
1523
|
+
const reveal = useRevealOnInteraction({ alwaysVisible });
|
|
1524
|
+
const baseId = useId();
|
|
1525
|
+
const labelId = `${baseId}-label`;
|
|
1526
|
+
const zoneId = `${baseId}-zone`;
|
|
1527
|
+
const messages = useMemo(() => ({
|
|
1528
|
+
...defaultPasteFieldMessages,
|
|
1529
|
+
...messageOverrides
|
|
1530
|
+
}), [messageOverrides]);
|
|
1531
|
+
const { paste: machinePaste } = pasteState;
|
|
1532
|
+
const doPaste = useCallback(() => machinePaste(), [machinePaste]);
|
|
1533
|
+
const { targetProps } = reveal;
|
|
1534
|
+
const ref = useMemo(() => mergeRefs(targetProps.ref, forwardedRef), [targetProps.ref, forwardedRef]);
|
|
1535
|
+
const context = useMemo(() => ({
|
|
1536
|
+
...pasteState,
|
|
1537
|
+
label,
|
|
1538
|
+
labelId,
|
|
1539
|
+
zoneId,
|
|
1540
|
+
revealed: reveal.visible,
|
|
1541
|
+
revealReason: reveal.reason,
|
|
1542
|
+
messages,
|
|
1543
|
+
displayStatus,
|
|
1544
|
+
doPaste
|
|
1545
|
+
}), [
|
|
1546
|
+
pasteState,
|
|
1547
|
+
label,
|
|
1548
|
+
labelId,
|
|
1549
|
+
zoneId,
|
|
1550
|
+
reveal.visible,
|
|
1551
|
+
reveal.reason,
|
|
1552
|
+
messages,
|
|
1553
|
+
displayStatus,
|
|
1554
|
+
doPaste
|
|
1555
|
+
]);
|
|
1556
|
+
const { state } = pasteState;
|
|
1557
|
+
const announcement = state.status === "read" ? typeof messages.pasted === "function" ? messages.pasted(state.result) : messages.pasted : state.status === "error" ? messages.error(state.error) : "";
|
|
1558
|
+
return /* @__PURE__ */ jsx(PasteFieldContext.Provider, {
|
|
1559
|
+
value: context,
|
|
1560
|
+
children: /* @__PURE__ */ jsxs("div", {
|
|
1561
|
+
...rest,
|
|
1562
|
+
ref,
|
|
1563
|
+
"data-state": state.status,
|
|
1564
|
+
"data-display-state": displayStatus,
|
|
1565
|
+
"data-revealed": reveal.visible ? "" : void 0,
|
|
1566
|
+
onPointerEnter: composeEventHandlers(onPointerEnter, targetProps.onPointerEnter),
|
|
1567
|
+
onPointerLeave: composeEventHandlers(onPointerLeave, targetProps.onPointerLeave),
|
|
1568
|
+
onPointerDown: composeEventHandlers(onPointerDown, targetProps.onPointerDown),
|
|
1569
|
+
onFocus: composeEventHandlers(onFocus, targetProps.onFocus),
|
|
1570
|
+
onBlur: composeEventHandlers(onBlur, targetProps.onBlur),
|
|
1571
|
+
children: [children, announce ? /* @__PURE__ */ jsx(LiveRegion, { message: announcement }) : null]
|
|
1572
|
+
})
|
|
1573
|
+
});
|
|
1574
|
+
});
|
|
1575
|
+
const Label = /* @__PURE__ */ forwardRef(function PasteFieldLabel({ children, ...rest }, ref) {
|
|
1576
|
+
const field = usePasteField();
|
|
1577
|
+
return /* @__PURE__ */ jsx("span", {
|
|
1578
|
+
...rest,
|
|
1579
|
+
ref,
|
|
1580
|
+
id: field.labelId,
|
|
1581
|
+
children: children ?? field.label
|
|
1582
|
+
});
|
|
1583
|
+
});
|
|
1584
|
+
const DEFAULT_STATUS_LABELS = {
|
|
1585
|
+
idle: "Ready",
|
|
1586
|
+
reading: "Reading…",
|
|
1587
|
+
read: "Pasted",
|
|
1588
|
+
error: "Error"
|
|
1589
|
+
};
|
|
1590
|
+
const Status = /* @__PURE__ */ forwardRef(function PasteFieldStatus({ children, labels, ...rest }, ref) {
|
|
1591
|
+
const field = usePasteField();
|
|
1592
|
+
const resolved = labels ? {
|
|
1593
|
+
...DEFAULT_STATUS_LABELS,
|
|
1594
|
+
...labels
|
|
1595
|
+
} : DEFAULT_STATUS_LABELS;
|
|
1596
|
+
return /* @__PURE__ */ jsx("span", {
|
|
1597
|
+
...rest,
|
|
1598
|
+
ref,
|
|
1599
|
+
"data-state": field.status,
|
|
1600
|
+
"data-display-state": field.displayStatus,
|
|
1601
|
+
children: children ?? resolved[field.displayStatus]
|
|
1602
|
+
});
|
|
1603
|
+
});
|
|
1604
|
+
/**
|
|
1605
|
+
* A drop/paste target area. Spread `usePaste`'s `targetProps` here so the
|
|
1606
|
+
* zone accepts keyboard paste events. Also renders a `data-zone` attribute
|
|
1607
|
+
* so it can be styled separately from the root.
|
|
1608
|
+
*/
|
|
1609
|
+
const Zone = /* @__PURE__ */ forwardRef(function PasteFieldZone({ children, focusable = true, onPaste, "aria-label": ariaLabel, tabIndex, ...rest }, ref) {
|
|
1610
|
+
const field = usePasteField();
|
|
1611
|
+
const { targetProps } = field;
|
|
1612
|
+
const computedAriaLabel = ariaLabel ?? field.messages.zoneLabel(field.label);
|
|
1613
|
+
const safeAriaLabel = computedAriaLabel.length > 0 ? computedAriaLabel : `Paste area for ${field.label}`;
|
|
1614
|
+
return /* @__PURE__ */ jsx("div", {
|
|
1615
|
+
...rest,
|
|
1616
|
+
ref,
|
|
1617
|
+
id: field.zoneId,
|
|
1618
|
+
role: "region",
|
|
1619
|
+
"aria-label": safeAriaLabel,
|
|
1620
|
+
"aria-labelledby": field.labelId,
|
|
1621
|
+
"data-zone": "",
|
|
1622
|
+
"data-state": field.status,
|
|
1623
|
+
"data-display-state": field.displayStatus,
|
|
1624
|
+
tabIndex: tabIndex ?? (focusable ? 0 : void 0),
|
|
1625
|
+
onPaste: composeEventHandlers(onPaste, targetProps.onPaste),
|
|
1626
|
+
children
|
|
1627
|
+
});
|
|
1628
|
+
});
|
|
1629
|
+
const DEFAULT_TRIGGER_TEXT = {
|
|
1630
|
+
idle: "Paste",
|
|
1631
|
+
reading: "Pasting…",
|
|
1632
|
+
read: "Pasted",
|
|
1633
|
+
error: "Retry"
|
|
1634
|
+
};
|
|
1635
|
+
const PASTE_STATUSES = [
|
|
1636
|
+
"idle",
|
|
1637
|
+
"reading",
|
|
1638
|
+
"read",
|
|
1639
|
+
"error"
|
|
1640
|
+
];
|
|
1641
|
+
function DefaultTriggerLabel({ status }) {
|
|
1642
|
+
return /* @__PURE__ */ jsx("span", {
|
|
1643
|
+
style: { display: "inline-grid" },
|
|
1644
|
+
children: PASTE_STATUSES.map((candidate) => {
|
|
1645
|
+
const active = candidate === status;
|
|
1646
|
+
return /* @__PURE__ */ jsx("span", {
|
|
1647
|
+
"data-trigger-label": candidate,
|
|
1648
|
+
"data-active": active ? "" : void 0,
|
|
1649
|
+
"aria-hidden": active ? void 0 : true,
|
|
1650
|
+
style: {
|
|
1651
|
+
gridArea: "1 / 1",
|
|
1652
|
+
opacity: active ? 1 : 0,
|
|
1653
|
+
visibility: active ? "visible" : "hidden",
|
|
1654
|
+
transition: "opacity 150ms ease, visibility 150ms"
|
|
1655
|
+
},
|
|
1656
|
+
children: DEFAULT_TRIGGER_TEXT[candidate]
|
|
1657
|
+
}, candidate);
|
|
1658
|
+
})
|
|
1659
|
+
});
|
|
1660
|
+
}
|
|
1661
|
+
/**
|
|
1662
|
+
* Headless compound component for a labelled, paste-ready field.
|
|
1663
|
+
*
|
|
1664
|
+
* Styling hooks on Root, Zone and Trigger:
|
|
1665
|
+
* - `data-display-state="idle|reading|read|error"`: flicker-free, style with this
|
|
1666
|
+
* - `data-state`: the real, instantaneous state, for logic and tests
|
|
1667
|
+
* - `data-revealed`: present while the trigger should be visible
|
|
1668
|
+
*
|
|
1669
|
+
* @example
|
|
1670
|
+
* ```tsx
|
|
1671
|
+
* <PasteField.Root label="Notes">
|
|
1672
|
+
* <PasteField.Label />
|
|
1673
|
+
* <PasteField.Zone />
|
|
1674
|
+
* <PasteField.Trigger />
|
|
1675
|
+
* </PasteField.Root>
|
|
1676
|
+
* ```
|
|
1677
|
+
*/
|
|
1678
|
+
const PasteField = {
|
|
1679
|
+
Root,
|
|
1680
|
+
Label,
|
|
1681
|
+
Status,
|
|
1682
|
+
Zone,
|
|
1683
|
+
Trigger: /* @__PURE__ */ forwardRef(function PasteFieldTrigger({ children, onClick, "aria-label": ariaLabel, ...rest }, ref) {
|
|
1684
|
+
const field = usePasteField();
|
|
1685
|
+
const renderProps = {
|
|
1686
|
+
status: field.status,
|
|
1687
|
+
displayStatus: field.displayStatus,
|
|
1688
|
+
state: field.state,
|
|
1689
|
+
revealed: field.revealed
|
|
1690
|
+
};
|
|
1691
|
+
const computedAriaLabel = ariaLabel ?? field.messages.triggerLabel(field.label);
|
|
1692
|
+
const safeAriaLabel = computedAriaLabel.length > 0 ? computedAriaLabel : `Paste ${field.label}`;
|
|
1693
|
+
return /* @__PURE__ */ jsx("button", {
|
|
1694
|
+
...rest,
|
|
1695
|
+
ref,
|
|
1696
|
+
type: "button",
|
|
1697
|
+
"aria-label": safeAriaLabel,
|
|
1698
|
+
"data-state": field.status,
|
|
1699
|
+
"data-display-state": field.displayStatus,
|
|
1700
|
+
"data-revealed": field.revealed ? "" : void 0,
|
|
1701
|
+
onClick: composeEventHandlers(onClick, () => {
|
|
1702
|
+
field.doPaste();
|
|
1703
|
+
}),
|
|
1704
|
+
children: typeof children === "function" ? children(renderProps) : children ?? /* @__PURE__ */ jsx(DefaultTriggerLabel, { status: field.displayStatus })
|
|
1705
|
+
});
|
|
1706
|
+
})
|
|
1707
|
+
};
|
|
1708
|
+
//#endregion
|
|
1709
|
+
export { CopyFailure, CopyField, CopyGroup, DEFAULT_MAX_RETRIES, DEFAULT_MIN_PENDING_MS, DEFAULT_PASTE_ACCEPT, DEFAULT_PASTE_MAX_BYTES, DEFAULT_PASTE_MAX_ITEMS, DEFAULT_PASTE_MIN_PENDING_MS, DEFAULT_PASTE_PENDING_DELAY_MS, DEFAULT_PENDING_DELAY_MS, DEFAULT_RESET_AFTER_MS, IDLE_STATE, IMAGE_MIME_TYPE, LiveRegion, PASTE_IDLE_STATE, PasteField, SVG_MIME_TYPE, canRetryState, copyFailure, createBrowserClipboardAdapter, createBrowserPasteAdapter, createCopyCoordinator, createCopyError, createCopyMachine, createPasteMachine, defaultCopyFieldMessages, defaultPasteFieldMessages, describeCopyError, describePasteError, isBlob, isCopyFailure, isRetryableError, isTextualMimeType, matchesMimePattern, normalizeMimeType, normalizePayload, resolveMaxRetries, resolvePasteReadOptions, serializeJson, sniffImageBytes, sniffImageType, toCopyError, useCopy, useCopyField, useCopyGroup, useDisplayStatus, usePaste, usePasteDisplayStatus, usePasteField, useRevealOnInteraction, validatePayload, visuallyHiddenStyle };
|
|
1455
1710
|
|
|
1456
1711
|
//# sourceMappingURL=index.js.map
|