@alien_org/react 0.1.2 → 0.1.3

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
@@ -91,6 +91,84 @@ function useAlien() {
91
91
  return context;
92
92
  }
93
93
 
94
+ //#endregion
95
+ //#region src/hooks/useClipboard.ts
96
+ /**
97
+ * Hook for clipboard operations.
98
+ *
99
+ * @example
100
+ * ```tsx
101
+ * function ClipboardDemo() {
102
+ * const { writeText, readText, isReading, errorCode, supported } = useClipboard();
103
+ *
104
+ * if (!supported) return null;
105
+ *
106
+ * return (
107
+ * <>
108
+ * <button onClick={() => writeText('Hello!')}>Copy</button>
109
+ * <button
110
+ * onClick={async () => {
111
+ * const text = await readText();
112
+ * if (text !== null) console.log('Pasted:', text);
113
+ * }}
114
+ * disabled={isReading}
115
+ * >
116
+ * Paste
117
+ * </button>
118
+ * {errorCode && <span>Error: {errorCode}</span>}
119
+ * </>
120
+ * );
121
+ * }
122
+ * ```
123
+ */
124
+ function useClipboard(options = {}) {
125
+ const { timeout = 5e3 } = options;
126
+ const { contractVersion, isBridgeAvailable: isBridgeAvailable$1 } = useAlien();
127
+ const [isReading, setIsReading] = (0, react.useState)(false);
128
+ const [errorCode, setErrorCode] = (0, react.useState)(null);
129
+ const supported = contractVersion ? (0, _alien_org_contract.isMethodSupported)("clipboard:write", contractVersion) && (0, _alien_org_contract.isMethodSupported)("clipboard:read", contractVersion) : true;
130
+ const writeText = (0, react.useCallback)((text) => {
131
+ if (!isBridgeAvailable$1) return;
132
+ if (contractVersion && !(0, _alien_org_contract.isMethodSupported)("clipboard:write", contractVersion)) return;
133
+ (0, _alien_org_bridge.send)("clipboard:write", { text });
134
+ }, [isBridgeAvailable$1, contractVersion]);
135
+ const readText = (0, react.useCallback)(async () => {
136
+ if (!isBridgeAvailable$1) return null;
137
+ if (contractVersion && !(0, _alien_org_contract.isMethodSupported)("clipboard:read", contractVersion)) return null;
138
+ setIsReading(true);
139
+ setErrorCode(null);
140
+ try {
141
+ const response = await (0, _alien_org_bridge.request)("clipboard:read", {}, "clipboard:response", { timeout });
142
+ if (response.errorCode) {
143
+ setErrorCode(response.errorCode);
144
+ return null;
145
+ }
146
+ return response.text;
147
+ } catch {
148
+ return null;
149
+ } finally {
150
+ setIsReading(false);
151
+ }
152
+ }, [
153
+ isBridgeAvailable$1,
154
+ contractVersion,
155
+ timeout
156
+ ]);
157
+ return (0, react.useMemo)(() => ({
158
+ writeText,
159
+ readText,
160
+ isReading,
161
+ errorCode,
162
+ supported
163
+ }), [
164
+ writeText,
165
+ readText,
166
+ isReading,
167
+ errorCode,
168
+ supported
169
+ ]);
170
+ }
171
+
94
172
  //#endregion
95
173
  //#region src/hooks/useEvent.ts
96
174
  /**
@@ -539,6 +617,7 @@ Object.defineProperty(exports, 'send', {
539
617
  }
540
618
  });
541
619
  exports.useAlien = useAlien;
620
+ exports.useClipboard = useClipboard;
542
621
  exports.useEvent = useEvent;
543
622
  exports.useIsMethodSupported = useIsMethodSupported;
544
623
  exports.useLaunchParams = useLaunchParams;
package/dist/index.d.cts CHANGED
@@ -479,6 +479,58 @@ declare class MethodNotSupportedError extends ReactSDKError {
479
479
  */
480
480
  declare function useAlien(): AlienContextValue;
481
481
  //#endregion
482
+ //#region src/hooks/useClipboard.d.ts
483
+ /** Clipboard error codes from the host app. */
484
+ type ClipboardErrorCode = 'permission_denied' | 'unavailable';
485
+ interface UseClipboardOptions {
486
+ /**
487
+ * Timeout for clipboard read in milliseconds.
488
+ * @default 5000
489
+ */
490
+ timeout?: number;
491
+ }
492
+ interface UseClipboardReturn {
493
+ /** Write text to clipboard. Fire-and-forget. */
494
+ writeText: (text: string) => void;
495
+ /** Read text from clipboard. Returns text or null on failure. */
496
+ readText: () => Promise<string | null>;
497
+ /** Whether a read operation is in progress. */
498
+ isReading: boolean;
499
+ /** Error code from the last failed read operation. */
500
+ errorCode: ClipboardErrorCode | null;
501
+ /** Whether clipboard methods are supported by the host app. */
502
+ supported: boolean;
503
+ }
504
+ /**
505
+ * Hook for clipboard operations.
506
+ *
507
+ * @example
508
+ * ```tsx
509
+ * function ClipboardDemo() {
510
+ * const { writeText, readText, isReading, errorCode, supported } = useClipboard();
511
+ *
512
+ * if (!supported) return null;
513
+ *
514
+ * return (
515
+ * <>
516
+ * <button onClick={() => writeText('Hello!')}>Copy</button>
517
+ * <button
518
+ * onClick={async () => {
519
+ * const text = await readText();
520
+ * if (text !== null) console.log('Pasted:', text);
521
+ * }}
522
+ * disabled={isReading}
523
+ * >
524
+ * Paste
525
+ * </button>
526
+ * {errorCode && <span>Error: {errorCode}</span>}
527
+ * </>
528
+ * );
529
+ * }
530
+ * ```
531
+ */
532
+ declare function useClipboard(options?: UseClipboardOptions): UseClipboardReturn;
533
+ //#endregion
482
534
  //#region src/hooks/useEvent.d.ts
483
535
  type EventCallback<E extends EventName> = (payload: EventPayload<E>) => void;
484
536
  /**
@@ -762,4 +814,4 @@ interface UsePaymentReturn {
762
814
  */
763
815
  declare function usePayment(options?: UsePaymentOptions): UsePaymentReturn;
764
816
  //#endregion
765
- export { AlienProvider, type AlienProviderProps, BridgeError, BridgeTimeoutError, BridgeUnavailableError, BridgeWindowUnavailableError, type EventName, type EventPayload, type MethodName, MethodNotSupportedError, type MethodPayload, type MethodSupportResult, type PaymentCallbacks, type PaymentErrorCode, type PaymentParams, type PaymentResponseStatus, type PaymentResult, type PaymentStatus, ReactSDKError, type RequestOptions, type UseMethodExecuteResult, type UseMethodOptions, type UsePaymentOptions, type UsePaymentReturn, type Version, getMethodMinVersion, isMethodSupported, send, useAlien, useEvent, useIsMethodSupported, useLaunchParams, useMethod, usePayment };
817
+ export { AlienProvider, type AlienProviderProps, BridgeError, BridgeTimeoutError, BridgeUnavailableError, BridgeWindowUnavailableError, type ClipboardErrorCode, type EventName, type EventPayload, type MethodName, MethodNotSupportedError, type MethodPayload, type MethodSupportResult, type PaymentCallbacks, type PaymentErrorCode, type PaymentParams, type PaymentResponseStatus, type PaymentResult, type PaymentStatus, ReactSDKError, type RequestOptions, type UseClipboardOptions, type UseClipboardReturn, type UseMethodExecuteResult, type UseMethodOptions, type UsePaymentOptions, type UsePaymentReturn, type Version, getMethodMinVersion, isMethodSupported, send, useAlien, useClipboard, useEvent, useIsMethodSupported, useLaunchParams, useMethod, usePayment };
package/dist/index.d.mts CHANGED
@@ -479,6 +479,58 @@ declare class MethodNotSupportedError extends ReactSDKError {
479
479
  */
480
480
  declare function useAlien(): AlienContextValue;
481
481
  //#endregion
482
+ //#region src/hooks/useClipboard.d.ts
483
+ /** Clipboard error codes from the host app. */
484
+ type ClipboardErrorCode = 'permission_denied' | 'unavailable';
485
+ interface UseClipboardOptions {
486
+ /**
487
+ * Timeout for clipboard read in milliseconds.
488
+ * @default 5000
489
+ */
490
+ timeout?: number;
491
+ }
492
+ interface UseClipboardReturn {
493
+ /** Write text to clipboard. Fire-and-forget. */
494
+ writeText: (text: string) => void;
495
+ /** Read text from clipboard. Returns text or null on failure. */
496
+ readText: () => Promise<string | null>;
497
+ /** Whether a read operation is in progress. */
498
+ isReading: boolean;
499
+ /** Error code from the last failed read operation. */
500
+ errorCode: ClipboardErrorCode | null;
501
+ /** Whether clipboard methods are supported by the host app. */
502
+ supported: boolean;
503
+ }
504
+ /**
505
+ * Hook for clipboard operations.
506
+ *
507
+ * @example
508
+ * ```tsx
509
+ * function ClipboardDemo() {
510
+ * const { writeText, readText, isReading, errorCode, supported } = useClipboard();
511
+ *
512
+ * if (!supported) return null;
513
+ *
514
+ * return (
515
+ * <>
516
+ * <button onClick={() => writeText('Hello!')}>Copy</button>
517
+ * <button
518
+ * onClick={async () => {
519
+ * const text = await readText();
520
+ * if (text !== null) console.log('Pasted:', text);
521
+ * }}
522
+ * disabled={isReading}
523
+ * >
524
+ * Paste
525
+ * </button>
526
+ * {errorCode && <span>Error: {errorCode}</span>}
527
+ * </>
528
+ * );
529
+ * }
530
+ * ```
531
+ */
532
+ declare function useClipboard(options?: UseClipboardOptions): UseClipboardReturn;
533
+ //#endregion
482
534
  //#region src/hooks/useEvent.d.ts
483
535
  type EventCallback<E extends EventName> = (payload: EventPayload<E>) => void;
484
536
  /**
@@ -762,4 +814,4 @@ interface UsePaymentReturn {
762
814
  */
763
815
  declare function usePayment(options?: UsePaymentOptions): UsePaymentReturn;
764
816
  //#endregion
765
- export { AlienProvider, type AlienProviderProps, BridgeError, BridgeTimeoutError, BridgeUnavailableError, BridgeWindowUnavailableError, type EventName, type EventPayload, type MethodName, MethodNotSupportedError, type MethodPayload, type MethodSupportResult, type PaymentCallbacks, type PaymentErrorCode, type PaymentParams, type PaymentResponseStatus, type PaymentResult, type PaymentStatus, ReactSDKError, type RequestOptions, type UseMethodExecuteResult, type UseMethodOptions, type UsePaymentOptions, type UsePaymentReturn, type Version, getMethodMinVersion, isMethodSupported, send, useAlien, useEvent, useIsMethodSupported, useLaunchParams, useMethod, usePayment };
817
+ export { AlienProvider, type AlienProviderProps, BridgeError, BridgeTimeoutError, BridgeUnavailableError, BridgeWindowUnavailableError, type ClipboardErrorCode, type EventName, type EventPayload, type MethodName, MethodNotSupportedError, type MethodPayload, type MethodSupportResult, type PaymentCallbacks, type PaymentErrorCode, type PaymentParams, type PaymentResponseStatus, type PaymentResult, type PaymentStatus, ReactSDKError, type RequestOptions, type UseClipboardOptions, type UseClipboardReturn, type UseMethodExecuteResult, type UseMethodOptions, type UsePaymentOptions, type UsePaymentReturn, type Version, getMethodMinVersion, isMethodSupported, send, useAlien, useClipboard, useEvent, useIsMethodSupported, useLaunchParams, useMethod, usePayment };
package/dist/index.mjs CHANGED
@@ -91,6 +91,84 @@ function useAlien() {
91
91
  return context;
92
92
  }
93
93
 
94
+ //#endregion
95
+ //#region src/hooks/useClipboard.ts
96
+ /**
97
+ * Hook for clipboard operations.
98
+ *
99
+ * @example
100
+ * ```tsx
101
+ * function ClipboardDemo() {
102
+ * const { writeText, readText, isReading, errorCode, supported } = useClipboard();
103
+ *
104
+ * if (!supported) return null;
105
+ *
106
+ * return (
107
+ * <>
108
+ * <button onClick={() => writeText('Hello!')}>Copy</button>
109
+ * <button
110
+ * onClick={async () => {
111
+ * const text = await readText();
112
+ * if (text !== null) console.log('Pasted:', text);
113
+ * }}
114
+ * disabled={isReading}
115
+ * >
116
+ * Paste
117
+ * </button>
118
+ * {errorCode && <span>Error: {errorCode}</span>}
119
+ * </>
120
+ * );
121
+ * }
122
+ * ```
123
+ */
124
+ function useClipboard(options = {}) {
125
+ const { timeout = 5e3 } = options;
126
+ const { contractVersion, isBridgeAvailable: isBridgeAvailable$1 } = useAlien();
127
+ const [isReading, setIsReading] = useState(false);
128
+ const [errorCode, setErrorCode] = useState(null);
129
+ const supported = contractVersion ? isMethodSupported$1("clipboard:write", contractVersion) && isMethodSupported$1("clipboard:read", contractVersion) : true;
130
+ const writeText = useCallback((text) => {
131
+ if (!isBridgeAvailable$1) return;
132
+ if (contractVersion && !isMethodSupported$1("clipboard:write", contractVersion)) return;
133
+ send$1("clipboard:write", { text });
134
+ }, [isBridgeAvailable$1, contractVersion]);
135
+ const readText = useCallback(async () => {
136
+ if (!isBridgeAvailable$1) return null;
137
+ if (contractVersion && !isMethodSupported$1("clipboard:read", contractVersion)) return null;
138
+ setIsReading(true);
139
+ setErrorCode(null);
140
+ try {
141
+ const response = await request("clipboard:read", {}, "clipboard:response", { timeout });
142
+ if (response.errorCode) {
143
+ setErrorCode(response.errorCode);
144
+ return null;
145
+ }
146
+ return response.text;
147
+ } catch {
148
+ return null;
149
+ } finally {
150
+ setIsReading(false);
151
+ }
152
+ }, [
153
+ isBridgeAvailable$1,
154
+ contractVersion,
155
+ timeout
156
+ ]);
157
+ return useMemo(() => ({
158
+ writeText,
159
+ readText,
160
+ isReading,
161
+ errorCode,
162
+ supported
163
+ }), [
164
+ writeText,
165
+ readText,
166
+ isReading,
167
+ errorCode,
168
+ supported
169
+ ]);
170
+ }
171
+
94
172
  //#endregion
95
173
  //#region src/hooks/useEvent.ts
96
174
  /**
@@ -513,4 +591,4 @@ function usePayment(options = {}) {
513
591
  }
514
592
 
515
593
  //#endregion
516
- export { AlienProvider, BridgeError, BridgeTimeoutError, BridgeUnavailableError, BridgeWindowUnavailableError, MethodNotSupportedError, ReactSDKError, getMethodMinVersion, isMethodSupported, send, useAlien, useEvent, useIsMethodSupported, useLaunchParams, useMethod, usePayment };
594
+ export { AlienProvider, BridgeError, BridgeTimeoutError, BridgeUnavailableError, BridgeWindowUnavailableError, MethodNotSupportedError, ReactSDKError, getMethodMinVersion, isMethodSupported, send, useAlien, useClipboard, useEvent, useIsMethodSupported, useLaunchParams, useMethod, usePayment };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@alien_org/react",
3
- "version": "0.1.2",
3
+ "version": "0.1.3",
4
4
  "type": "module",
5
5
  "main": "./dist/index.cjs",
6
6
  "module": "./dist/index.mjs",