@xpayeg/react 2.4.0 → 3.0.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 → .types}/index.d.cts +71 -80
- package/{dist → .types}/index.d.mts +71 -80
- package/CHANGELOG.md +36 -30
- package/README.md +139 -137
- package/dist/index.cjs +19 -47
- package/dist/index.mjs +19 -47
- package/package.json +11 -28
package/dist/index.mjs
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { createContext, useCallback, useContext, useEffect, useLayoutEffect, useMemo, useRef, useState } from "react";
|
|
2
2
|
import { jsx } from "react/jsx-runtime";
|
|
3
|
-
//#region src/context.tsx
|
|
4
3
|
const XPayContext = createContext(null);
|
|
5
4
|
const ElementsContext = createContext(null);
|
|
6
5
|
/** Build a minimal XPayError for SDK-level failures (elements not initialized, etc.) */
|
|
@@ -268,9 +267,9 @@ function useCheckoutActions(ctx) {
|
|
|
268
267
|
changeAppearanceFn: useCallback((appearance) => {
|
|
269
268
|
elements?.changeAppearance(appearance);
|
|
270
269
|
}, [elements]),
|
|
271
|
-
onChangeFn: useCallback((
|
|
270
|
+
onChangeFn: useCallback((event, handler) => {
|
|
272
271
|
elements?.on(event, handler);
|
|
273
|
-
}
|
|
272
|
+
}, [elements]),
|
|
274
273
|
getElementsFn: useCallback(() => {
|
|
275
274
|
if (!elements) throw new Error("Elements not initialized");
|
|
276
275
|
return elements;
|
|
@@ -295,8 +294,6 @@ function useXPay() {
|
|
|
295
294
|
function useElements() {
|
|
296
295
|
return useContext(ElementsContext);
|
|
297
296
|
}
|
|
298
|
-
//#endregion
|
|
299
|
-
//#region src/hooks.ts
|
|
300
297
|
/**
|
|
301
298
|
* Convenience hook for payment confirmation.
|
|
302
299
|
*
|
|
@@ -322,8 +319,6 @@ function useConfirmPayment() {
|
|
|
322
319
|
isConfirming: false
|
|
323
320
|
};
|
|
324
321
|
}
|
|
325
|
-
//#endregion
|
|
326
|
-
//#region src/utils/useAttachEvent.ts
|
|
327
322
|
/**
|
|
328
323
|
* Attach an event listener to an element without causing listener churn on re-renders.
|
|
329
324
|
*
|
|
@@ -351,21 +346,6 @@ function useAttachEvent(element, event, cb) {
|
|
|
351
346
|
element
|
|
352
347
|
]);
|
|
353
348
|
}
|
|
354
|
-
//#endregion
|
|
355
|
-
//#region src/utils/usePrevious.ts
|
|
356
|
-
/**
|
|
357
|
-
* Returns the previous value of a variable.
|
|
358
|
-
* Useful for detecting prop changes between renders.
|
|
359
|
-
*/
|
|
360
|
-
function usePrevious(value) {
|
|
361
|
-
const ref = useRef(void 0);
|
|
362
|
-
useEffect(() => {
|
|
363
|
-
ref.current = value;
|
|
364
|
-
}, [value]);
|
|
365
|
-
return ref.current;
|
|
366
|
-
}
|
|
367
|
-
//#endregion
|
|
368
|
-
//#region src/utils/extractAllowedOptionsUpdates.ts
|
|
369
349
|
/**
|
|
370
350
|
* Extract only the changed, mutable options from a new options object.
|
|
371
351
|
*
|
|
@@ -407,19 +387,15 @@ function isEqual(a, b) {
|
|
|
407
387
|
}
|
|
408
388
|
return false;
|
|
409
389
|
}
|
|
410
|
-
//#endregion
|
|
411
|
-
//#region src/utils/isServer.ts
|
|
412
390
|
/** True when running in a server environment (SSR/RSC) */
|
|
413
391
|
const isServer = typeof window === "undefined";
|
|
414
|
-
//#endregion
|
|
415
|
-
//#region src/PaymentElement.tsx
|
|
416
392
|
const IMMUTABLE_OPTS = [];
|
|
417
393
|
const PaymentElementClient = ({ options, onReady, onChange, onLoaderStart, onLoadError, className, id }) => {
|
|
418
394
|
const elements = useElements();
|
|
419
395
|
const containerRef = useRef(null);
|
|
420
396
|
const elementRef = useRef(null);
|
|
421
397
|
const [element, setElement] = useState(null);
|
|
422
|
-
const
|
|
398
|
+
const previousOptions = useRef(void 0);
|
|
423
399
|
useLayoutEffect(() => {
|
|
424
400
|
if (elementRef.current !== null || !elements || !containerRef.current) return;
|
|
425
401
|
const el = elements.create("payment", options);
|
|
@@ -439,14 +415,12 @@ const PaymentElementClient = ({ options, onReady, onChange, onLoaderStart, onLoa
|
|
|
439
415
|
};
|
|
440
416
|
}, []);
|
|
441
417
|
useEffect(() => {
|
|
418
|
+
const prevOptions = previousOptions.current;
|
|
419
|
+
previousOptions.current = options;
|
|
442
420
|
if (!element || !options) return;
|
|
443
421
|
const updates = extractAllowedOptionsUpdates(options, prevOptions, IMMUTABLE_OPTS);
|
|
444
422
|
if (updates && "update" in element) element.update(updates);
|
|
445
|
-
}, [
|
|
446
|
-
options,
|
|
447
|
-
prevOptions,
|
|
448
|
-
element
|
|
449
|
-
]);
|
|
423
|
+
}, [options, element]);
|
|
450
424
|
useAttachEvent(element, "ready", onReady);
|
|
451
425
|
useAttachEvent(element, "change", onChange);
|
|
452
426
|
useAttachEvent(element, "loaderstart", onLoaderStart);
|
|
@@ -481,8 +455,6 @@ const PaymentElementServer = ({ className, id }) => {
|
|
|
481
455
|
* ```
|
|
482
456
|
*/
|
|
483
457
|
const PaymentElement = isServer ? PaymentElementServer : PaymentElementClient;
|
|
484
|
-
//#endregion
|
|
485
|
-
//#region src/CheckoutButton.tsx
|
|
486
458
|
/**
|
|
487
459
|
* Button that opens the drop-in checkout modal on click.
|
|
488
460
|
*
|
|
@@ -505,24 +477,24 @@ const PaymentElement = isServer ? PaymentElementServer : PaymentElementClient;
|
|
|
505
477
|
*/
|
|
506
478
|
const CheckoutButton = ({ clientSecret, children = "Pay", checkoutOptions, className, disabled }) => {
|
|
507
479
|
const xpay = useXPay();
|
|
480
|
+
const handleClick = useCallback(() => {
|
|
481
|
+
if (!xpay) return;
|
|
482
|
+
xpay.checkout({
|
|
483
|
+
clientSecret,
|
|
484
|
+
mode: "modal",
|
|
485
|
+
...checkoutOptions
|
|
486
|
+
}).open();
|
|
487
|
+
}, [
|
|
488
|
+
xpay,
|
|
489
|
+
clientSecret,
|
|
490
|
+
checkoutOptions
|
|
491
|
+
]);
|
|
508
492
|
return /* @__PURE__ */ jsx("button", {
|
|
509
493
|
type: "button",
|
|
510
|
-
onClick:
|
|
511
|
-
if (!xpay) return;
|
|
512
|
-
xpay.checkout({
|
|
513
|
-
clientSecret,
|
|
514
|
-
mode: "modal",
|
|
515
|
-
...checkoutOptions
|
|
516
|
-
}).open();
|
|
517
|
-
}, [
|
|
518
|
-
xpay,
|
|
519
|
-
clientSecret,
|
|
520
|
-
checkoutOptions
|
|
521
|
-
]),
|
|
494
|
+
onClick: handleClick,
|
|
522
495
|
disabled: disabled || !xpay,
|
|
523
496
|
className,
|
|
524
497
|
children
|
|
525
498
|
});
|
|
526
499
|
};
|
|
527
|
-
//#endregion
|
|
528
500
|
export { CheckoutButton, PaymentElement, XPayProvider, useCheckout, useConfirmPayment, useElements, useXPay };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@xpayeg/react",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "3.0.0",
|
|
4
4
|
"description": "XPay React components — PaymentElement, CheckoutButton, and hooks",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
@@ -10,15 +10,15 @@
|
|
|
10
10
|
},
|
|
11
11
|
"main": "./dist/index.cjs",
|
|
12
12
|
"module": "./dist/index.mjs",
|
|
13
|
-
"types": "
|
|
13
|
+
"types": "./.types/index.d.mts",
|
|
14
14
|
"exports": {
|
|
15
15
|
".": {
|
|
16
16
|
"import": {
|
|
17
|
-
"types": "
|
|
17
|
+
"types": "./.types/index.d.mts",
|
|
18
18
|
"default": "./dist/index.mjs"
|
|
19
19
|
},
|
|
20
20
|
"require": {
|
|
21
|
-
"types": "
|
|
21
|
+
"types": "./.types/index.d.cts",
|
|
22
22
|
"default": "./dist/index.cjs"
|
|
23
23
|
}
|
|
24
24
|
}
|
|
@@ -27,31 +27,19 @@
|
|
|
27
27
|
"access": "public"
|
|
28
28
|
},
|
|
29
29
|
"files": [
|
|
30
|
-
"dist",
|
|
30
|
+
"dist/index.mjs",
|
|
31
|
+
"dist/index.cjs",
|
|
31
32
|
"README.md",
|
|
32
33
|
"CHANGELOG.md",
|
|
33
|
-
"LICENSE"
|
|
34
|
+
"LICENSE",
|
|
35
|
+
".types/index.d.mts",
|
|
36
|
+
".types/index.d.cts"
|
|
34
37
|
],
|
|
35
38
|
"peerDependencies": {
|
|
36
|
-
"@xpayeg/sdk": "^
|
|
39
|
+
"@xpayeg/sdk": "^3.0.0",
|
|
37
40
|
"react": "^18 || ^19",
|
|
38
41
|
"react-dom": "^18 || ^19"
|
|
39
42
|
},
|
|
40
|
-
"devDependencies": {
|
|
41
|
-
"@arethetypeswrong/cli": "^0.18.2",
|
|
42
|
-
"@rollup/plugin-node-resolve": "^16.0.3",
|
|
43
|
-
"@types/react": "19.2.7",
|
|
44
|
-
"@types/react-dom": "19.2.3",
|
|
45
|
-
"publint": "^0.3.21",
|
|
46
|
-
"react": "^19.2.6",
|
|
47
|
-
"react-dom": "^19.2.6",
|
|
48
|
-
"rollup": "^4.60.4",
|
|
49
|
-
"rollup-plugin-dts": "^6.4.1",
|
|
50
|
-
"tsdown": "^0.22.0",
|
|
51
|
-
"typescript": "5.9.3",
|
|
52
|
-
"@xpay/tsconfig": "0.0.0",
|
|
53
|
-
"@xpayeg/sdk": "2.4.0"
|
|
54
|
-
},
|
|
55
43
|
"repository": {
|
|
56
44
|
"type": "git",
|
|
57
45
|
"url": "git+https://github.com/xpayeg/xpay-react.git"
|
|
@@ -67,10 +55,5 @@
|
|
|
67
55
|
"egypt",
|
|
68
56
|
"checkout",
|
|
69
57
|
"sdk"
|
|
70
|
-
]
|
|
71
|
-
"scripts": {
|
|
72
|
-
"build": "tsdown && rollup -c rollup.dts.config.mjs",
|
|
73
|
-
"lint:pkg": "publint && attw --pack .",
|
|
74
|
-
"typecheck": "tsc --noEmit"
|
|
75
|
-
}
|
|
58
|
+
]
|
|
76
59
|
}
|