@maytes/checkout-button 1.1.0 → 1.2.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/CHANGELOG.md +79 -0
- package/README.md +97 -53
- package/dist/checkout-button.c640180e.js +156 -0
- package/dist/checkout-button.c640180e.js.map +1 -0
- package/dist/checkout-button.cjs +280 -145
- package/dist/checkout-button.cjs.map +1 -1
- package/dist/checkout-button.d.cts +40 -4
- package/dist/checkout-button.d.ts +40 -4
- package/dist/{checkout-button.f96c49ee.mjs → checkout-button.d6e444ee.mjs} +280 -145
- package/dist/checkout-button.d6e444ee.mjs.map +1 -0
- package/dist/{checkout-button.178d8394.cjs → checkout-button.d7bc9aea.cjs} +280 -145
- package/dist/checkout-button.d7bc9aea.cjs.map +1 -0
- package/dist/checkout-button.element.6c530c24.js +155 -0
- package/dist/checkout-button.element.6c530c24.js.map +1 -0
- package/dist/checkout-button.element.js +155 -0
- package/dist/checkout-button.element.js.map +1 -0
- package/dist/checkout-button.js +19 -14
- package/dist/checkout-button.js.map +1 -1
- package/dist/checkout-button.mjs +280 -145
- package/dist/checkout-button.mjs.map +1 -1
- package/dist/element.cjs +201 -0
- package/dist/element.cjs.map +1 -0
- package/dist/element.d.cts +44 -0
- package/dist/element.d.ts +44 -0
- package/dist/element.mjs +180 -0
- package/dist/element.mjs.map +1 -0
- package/dist/integrity.json +37 -26
- package/dist/react.cjs +139 -0
- package/dist/react.cjs.map +1 -0
- package/dist/react.d.cts +30 -0
- package/dist/react.d.ts +30 -0
- package/dist/react.mjs +117 -0
- package/dist/react.mjs.map +1 -0
- package/docs/guides/angular.md +56 -0
- package/docs/guides/api.md +108 -0
- package/docs/guides/events.md +48 -0
- package/docs/guides/launch-behaviour.md +41 -0
- package/docs/guides/nextjs.md +49 -0
- package/docs/guides/own-button.md +41 -0
- package/docs/guides/quickstart-html.md +31 -0
- package/docs/guides/quickstart-npm.md +34 -0
- package/docs/guides/react.md +88 -0
- package/docs/guides/security-csp.md +32 -0
- package/docs/guides/server.md +76 -0
- package/docs/guides/solid.md +44 -0
- package/docs/guides/stripe-payment-element.md +14 -0
- package/docs/guides/svelte.md +46 -0
- package/docs/guides/troubleshooting.md +17 -0
- package/docs/guides/vue-nuxt.md +64 -0
- package/docs/guides/web-component.md +109 -0
- package/package.json +40 -3
- package/dist/checkout-button.178d8394.cjs.map +0 -1
- package/dist/checkout-button.811263f9.js +0 -151
- package/dist/checkout-button.811263f9.js.map +0 -1
- package/dist/checkout-button.f96c49ee.mjs.map +0 -1
package/dist/element.mjs
ADDED
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
// src/element.ts
|
|
2
|
+
import { Maytes, MaytesError, MaytesErrorCode } from "@maytes/checkout-button";
|
|
3
|
+
var MAYTES_CHECKOUT_BUTTON_TAG = "maytes-checkout-button";
|
|
4
|
+
var OBSERVED_ATTRIBUTES = ["environment", "nonce", "mode", "label", "block", "radius", "height"];
|
|
5
|
+
var INSTANCE_ATTRIBUTES = /* @__PURE__ */ new Set(["environment", "nonce"]);
|
|
6
|
+
var FORWARDED_EVENTS = /* @__PURE__ */ new Map([
|
|
7
|
+
["maytes:checkout-opened", "maytes-opened"],
|
|
8
|
+
["maytes:checkout-closed", "maytes-closed"],
|
|
9
|
+
["maytes:checkout-redirected", "maytes-redirected"],
|
|
10
|
+
["maytes:checkout-failed", "maytes-failed"]
|
|
11
|
+
]);
|
|
12
|
+
var LAUNCH_ENDING_EVENTS = /* @__PURE__ */ new Set(["maytes:checkout-closed", "maytes:checkout-redirected", "maytes:checkout-failed"]);
|
|
13
|
+
var BaseElement = typeof HTMLElement === "undefined" ? class {
|
|
14
|
+
} : HTMLElement;
|
|
15
|
+
function numberAttribute(value) {
|
|
16
|
+
if (value === null || value.trim() === "") return void 0;
|
|
17
|
+
return Number(value);
|
|
18
|
+
}
|
|
19
|
+
function readButtonOptions(element) {
|
|
20
|
+
const options = { block: element.hasAttribute("block") };
|
|
21
|
+
const mode = element.getAttribute("mode");
|
|
22
|
+
if (mode !== null) options.mode = mode;
|
|
23
|
+
const label = element.getAttribute("label");
|
|
24
|
+
if (label !== null) options.label = label;
|
|
25
|
+
const radius = numberAttribute(element.getAttribute("radius"));
|
|
26
|
+
if (radius !== void 0) options.radius = radius;
|
|
27
|
+
const height = numberAttribute(element.getAttribute("height"));
|
|
28
|
+
if (height !== void 0) options.height = height;
|
|
29
|
+
return options;
|
|
30
|
+
}
|
|
31
|
+
var MaytesCheckoutButtonElement = class extends BaseElement {
|
|
32
|
+
constructor() {
|
|
33
|
+
super(...arguments);
|
|
34
|
+
this.checkoutCallback = null;
|
|
35
|
+
this.maytes = null;
|
|
36
|
+
this.removeButton = null;
|
|
37
|
+
this.launching = false;
|
|
38
|
+
this.recreatePending = false;
|
|
39
|
+
this.listening = false;
|
|
40
|
+
this.forward = (event) => {
|
|
41
|
+
const maytes = this.maytes;
|
|
42
|
+
const name = FORWARDED_EVENTS.get(event.type);
|
|
43
|
+
if (maytes === null || name === void 0) return;
|
|
44
|
+
const detail = event.detail;
|
|
45
|
+
if (detail?.instanceId !== maytes.instanceId) return;
|
|
46
|
+
this.dispatchEvent(new CustomEvent(name, { detail, bubbles: true, composed: true }));
|
|
47
|
+
if (LAUNCH_ENDING_EVENTS.has(event.type)) this.launchEnded();
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
static get observedAttributes() {
|
|
51
|
+
return OBSERVED_ATTRIBUTES;
|
|
52
|
+
}
|
|
53
|
+
get createCheckout() {
|
|
54
|
+
return this.checkoutCallback;
|
|
55
|
+
}
|
|
56
|
+
set createCheckout(callback) {
|
|
57
|
+
this.checkoutCallback = typeof callback === "function" ? callback : null;
|
|
58
|
+
this.mount();
|
|
59
|
+
}
|
|
60
|
+
get instanceId() {
|
|
61
|
+
return this.maytes?.instanceId ?? null;
|
|
62
|
+
}
|
|
63
|
+
open(options) {
|
|
64
|
+
if (this.maytes === null) return Promise.resolve({ outcome: "ignored" });
|
|
65
|
+
return this.maytes.openCheckout(options);
|
|
66
|
+
}
|
|
67
|
+
connectedCallback() {
|
|
68
|
+
this.mount();
|
|
69
|
+
}
|
|
70
|
+
disconnectedCallback() {
|
|
71
|
+
queueMicrotask(() => {
|
|
72
|
+
if (!this.isConnected) this.unmount();
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
attributeChangedCallback(name, previous, next) {
|
|
76
|
+
if (previous === next) return;
|
|
77
|
+
if (this.maytes === null) {
|
|
78
|
+
this.mount();
|
|
79
|
+
return;
|
|
80
|
+
}
|
|
81
|
+
if (INSTANCE_ATTRIBUTES.has(name)) {
|
|
82
|
+
this.recreateWhenIdle();
|
|
83
|
+
return;
|
|
84
|
+
}
|
|
85
|
+
this.renderButton(this.maytes);
|
|
86
|
+
}
|
|
87
|
+
mount() {
|
|
88
|
+
if (!this.isConnected || this.checkoutCallback === null || this.maytes !== null) return;
|
|
89
|
+
const nonce = this.getAttribute("nonce");
|
|
90
|
+
let maytes;
|
|
91
|
+
try {
|
|
92
|
+
maytes = Maytes(
|
|
93
|
+
{
|
|
94
|
+
createCheckout: () => this.startCheckout(),
|
|
95
|
+
environment: this.getAttribute("environment")
|
|
96
|
+
},
|
|
97
|
+
nonce === null ? void 0 : { cspNonce: nonce }
|
|
98
|
+
);
|
|
99
|
+
} catch (error) {
|
|
100
|
+
this.reportConfigError(error);
|
|
101
|
+
return;
|
|
102
|
+
}
|
|
103
|
+
this.maytes = maytes;
|
|
104
|
+
this.listen();
|
|
105
|
+
this.renderButton(maytes);
|
|
106
|
+
}
|
|
107
|
+
renderButton(maytes) {
|
|
108
|
+
let removeButton;
|
|
109
|
+
try {
|
|
110
|
+
removeButton = maytes.renderButton(this, readButtonOptions(this));
|
|
111
|
+
} catch (error) {
|
|
112
|
+
this.reportConfigError(error);
|
|
113
|
+
return;
|
|
114
|
+
}
|
|
115
|
+
this.removeButton?.();
|
|
116
|
+
this.removeButton = removeButton;
|
|
117
|
+
}
|
|
118
|
+
recreateWhenIdle() {
|
|
119
|
+
if (this.launching) {
|
|
120
|
+
this.recreatePending = true;
|
|
121
|
+
return;
|
|
122
|
+
}
|
|
123
|
+
this.unmount();
|
|
124
|
+
this.mount();
|
|
125
|
+
}
|
|
126
|
+
launchEnded() {
|
|
127
|
+
this.launching = false;
|
|
128
|
+
if (!this.recreatePending) return;
|
|
129
|
+
this.recreatePending = false;
|
|
130
|
+
queueMicrotask(() => this.recreateWhenIdle());
|
|
131
|
+
}
|
|
132
|
+
listen() {
|
|
133
|
+
if (this.listening) return;
|
|
134
|
+
this.listening = true;
|
|
135
|
+
for (const type of FORWARDED_EVENTS.keys()) document.addEventListener(type, this.forward);
|
|
136
|
+
}
|
|
137
|
+
unlisten() {
|
|
138
|
+
if (!this.listening) return;
|
|
139
|
+
this.listening = false;
|
|
140
|
+
for (const type of FORWARDED_EVENTS.keys()) document.removeEventListener(type, this.forward);
|
|
141
|
+
}
|
|
142
|
+
reportConfigError(error) {
|
|
143
|
+
if (!(error instanceof MaytesError)) throw error;
|
|
144
|
+
console.error("[maytes/checkout-button] <maytes-checkout-button> configuration error:", error);
|
|
145
|
+
const detail = { reason: "config", cause: error };
|
|
146
|
+
this.dispatchEvent(new CustomEvent("maytes-failed", { detail, bubbles: true, composed: true }));
|
|
147
|
+
}
|
|
148
|
+
startCheckout() {
|
|
149
|
+
const callback = this.checkoutCallback;
|
|
150
|
+
if (callback === null) {
|
|
151
|
+
return Promise.reject(new MaytesError(MaytesErrorCode.Config, "<maytes-checkout-button> has no createCheckout"));
|
|
152
|
+
}
|
|
153
|
+
this.launching = true;
|
|
154
|
+
return callback();
|
|
155
|
+
}
|
|
156
|
+
unmount() {
|
|
157
|
+
this.unlisten();
|
|
158
|
+
this.removeButton?.();
|
|
159
|
+
this.removeButton = null;
|
|
160
|
+
this.maytes?.destroy();
|
|
161
|
+
this.maytes = null;
|
|
162
|
+
this.launching = false;
|
|
163
|
+
this.recreatePending = false;
|
|
164
|
+
}
|
|
165
|
+
};
|
|
166
|
+
function defineMaytesCheckoutButton() {
|
|
167
|
+
if (typeof customElements === "undefined") return false;
|
|
168
|
+
if (customElements.get(MAYTES_CHECKOUT_BUTTON_TAG) === void 0) {
|
|
169
|
+
customElements.define(MAYTES_CHECKOUT_BUTTON_TAG, MaytesCheckoutButtonElement);
|
|
170
|
+
}
|
|
171
|
+
return true;
|
|
172
|
+
}
|
|
173
|
+
defineMaytesCheckoutButton();
|
|
174
|
+
export {
|
|
175
|
+
MAYTES_CHECKOUT_BUTTON_TAG,
|
|
176
|
+
MaytesCheckoutButtonElement,
|
|
177
|
+
defineMaytesCheckoutButton,
|
|
178
|
+
readButtonOptions
|
|
179
|
+
};
|
|
180
|
+
//# sourceMappingURL=element.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/element.ts"],"sourcesContent":["import { Maytes, MaytesError, MaytesErrorCode } from '@maytes/checkout-button';\nimport type {\n CheckoutClosedDetail,\n CheckoutFailedDetail,\n CheckoutOpenedDetail,\n CheckoutRedirectedDetail,\n CreateCheckoutFn,\n MaytesEnvironment,\n MaytesSDK,\n OpenCheckoutOptions,\n OpenCheckoutResult,\n RenderButtonCleanup,\n RenderButtonMode,\n RenderButtonOptions,\n} from '@maytes/checkout-button';\n\nexport const MAYTES_CHECKOUT_BUTTON_TAG = 'maytes-checkout-button';\n\nexport interface ElementConfigFailedDetail {\n reason: 'config';\n cause: unknown;\n}\n\nexport type MaytesElementFailedDetail = CheckoutFailedDetail | ElementConfigFailedDetail;\n\nconst OBSERVED_ATTRIBUTES = ['environment', 'nonce', 'mode', 'label', 'block', 'radius', 'height'];\nconst INSTANCE_ATTRIBUTES = new Set(['environment', 'nonce']);\n\nconst FORWARDED_EVENTS = new Map<string, string>([\n ['maytes:checkout-opened', 'maytes-opened'],\n ['maytes:checkout-closed', 'maytes-closed'],\n ['maytes:checkout-redirected', 'maytes-redirected'],\n ['maytes:checkout-failed', 'maytes-failed'],\n]);\n\nconst LAUNCH_ENDING_EVENTS = new Set(['maytes:checkout-closed', 'maytes:checkout-redirected', 'maytes:checkout-failed']);\n\ntype SdkEventDetail = CheckoutOpenedDetail | CheckoutClosedDetail | CheckoutRedirectedDetail | CheckoutFailedDetail;\n\nconst BaseElement = (typeof HTMLElement === 'undefined' ? class {} : HTMLElement) as typeof HTMLElement;\n\nfunction numberAttribute(value: string | null): number | undefined {\n if (value === null || value.trim() === '') return undefined;\n return Number(value);\n}\n\nexport function readButtonOptions(element: Element): RenderButtonOptions {\n const options: RenderButtonOptions = { block: element.hasAttribute('block') };\n const mode = element.getAttribute('mode');\n if (mode !== null) options.mode = mode as RenderButtonMode;\n const label = element.getAttribute('label');\n if (label !== null) options.label = label;\n const radius = numberAttribute(element.getAttribute('radius'));\n if (radius !== undefined) options.radius = radius;\n const height = numberAttribute(element.getAttribute('height'));\n if (height !== undefined) options.height = height;\n return options;\n}\n\nexport class MaytesCheckoutButtonElement extends BaseElement {\n private checkoutCallback: CreateCheckoutFn | null = null;\n private maytes: MaytesSDK | null = null;\n private removeButton: RenderButtonCleanup | null = null;\n private launching = false;\n private recreatePending = false;\n private listening = false;\n\n static get observedAttributes(): string[] {\n return OBSERVED_ATTRIBUTES;\n }\n\n get createCheckout(): CreateCheckoutFn | null {\n return this.checkoutCallback;\n }\n\n set createCheckout(callback: CreateCheckoutFn | null) {\n this.checkoutCallback = typeof callback === 'function' ? callback : null;\n this.mount();\n }\n\n get instanceId(): string | null {\n return this.maytes?.instanceId ?? null;\n }\n\n open(options?: OpenCheckoutOptions): Promise<OpenCheckoutResult> {\n if (this.maytes === null) return Promise.resolve({ outcome: 'ignored' });\n return this.maytes.openCheckout(options);\n }\n\n connectedCallback(): void {\n this.mount();\n }\n\n disconnectedCallback(): void {\n queueMicrotask(() => {\n if (!this.isConnected) this.unmount();\n });\n }\n\n attributeChangedCallback(name: string, previous: string | null, next: string | null): void {\n if (previous === next) return;\n if (this.maytes === null) {\n this.mount();\n return;\n }\n if (INSTANCE_ATTRIBUTES.has(name)) {\n this.recreateWhenIdle();\n return;\n }\n this.renderButton(this.maytes);\n }\n\n private mount(): void {\n if (!this.isConnected || this.checkoutCallback === null || this.maytes !== null) return;\n const nonce = this.getAttribute('nonce');\n let maytes: MaytesSDK;\n try {\n maytes = Maytes(\n {\n createCheckout: () => this.startCheckout(),\n environment: this.getAttribute('environment') as MaytesEnvironment,\n },\n nonce === null ? undefined : { cspNonce: nonce },\n );\n } catch (error) {\n this.reportConfigError(error);\n return;\n }\n this.maytes = maytes;\n this.listen();\n this.renderButton(maytes);\n }\n\n private renderButton(maytes: MaytesSDK): void {\n let removeButton: RenderButtonCleanup;\n try {\n removeButton = maytes.renderButton(this, readButtonOptions(this));\n } catch (error) {\n this.reportConfigError(error);\n return;\n }\n this.removeButton?.();\n this.removeButton = removeButton;\n }\n\n private recreateWhenIdle(): void {\n if (this.launching) {\n this.recreatePending = true;\n return;\n }\n this.unmount();\n this.mount();\n }\n\n private readonly forward = (event: Event): void => {\n const maytes = this.maytes;\n const name = FORWARDED_EVENTS.get(event.type);\n if (maytes === null || name === undefined) return;\n const detail = (event as CustomEvent<SdkEventDetail | undefined>).detail;\n if (detail?.instanceId !== maytes.instanceId) return;\n this.dispatchEvent(new CustomEvent(name, { detail, bubbles: true, composed: true }));\n if (LAUNCH_ENDING_EVENTS.has(event.type)) this.launchEnded();\n };\n\n private launchEnded(): void {\n this.launching = false;\n if (!this.recreatePending) return;\n this.recreatePending = false;\n queueMicrotask(() => this.recreateWhenIdle());\n }\n\n private listen(): void {\n if (this.listening) return;\n this.listening = true;\n for (const type of FORWARDED_EVENTS.keys()) document.addEventListener(type, this.forward);\n }\n\n private unlisten(): void {\n if (!this.listening) return;\n this.listening = false;\n for (const type of FORWARDED_EVENTS.keys()) document.removeEventListener(type, this.forward);\n }\n\n private reportConfigError(error: unknown): void {\n if (!(error instanceof MaytesError)) throw error;\n console.error('[maytes/checkout-button] <maytes-checkout-button> configuration error:', error);\n const detail: ElementConfigFailedDetail = { reason: 'config', cause: error };\n this.dispatchEvent(new CustomEvent('maytes-failed', { detail, bubbles: true, composed: true }));\n }\n\n private startCheckout(): ReturnType<CreateCheckoutFn> {\n const callback = this.checkoutCallback;\n if (callback === null) {\n return Promise.reject(new MaytesError(MaytesErrorCode.Config, '<maytes-checkout-button> has no createCheckout'));\n }\n this.launching = true;\n return callback();\n }\n\n private unmount(): void {\n this.unlisten();\n this.removeButton?.();\n this.removeButton = null;\n this.maytes?.destroy();\n this.maytes = null;\n this.launching = false;\n this.recreatePending = false;\n }\n}\n\nexport function defineMaytesCheckoutButton(): boolean {\n if (typeof customElements === 'undefined') return false;\n if (customElements.get(MAYTES_CHECKOUT_BUTTON_TAG) === undefined) {\n customElements.define(MAYTES_CHECKOUT_BUTTON_TAG, MaytesCheckoutButtonElement);\n }\n return true;\n}\n\ndefineMaytesCheckoutButton();\n\ndeclare global {\n interface HTMLElementTagNameMap {\n 'maytes-checkout-button': MaytesCheckoutButtonElement;\n }\n}\n"],"mappings":";AAAA,SAAS,QAAQ,aAAa,uBAAuB;AAgB9C,IAAM,6BAA6B;AAS1C,IAAM,sBAAsB,CAAC,eAAe,SAAS,QAAQ,SAAS,SAAS,UAAU,QAAQ;AACjG,IAAM,sBAAsB,oBAAI,IAAI,CAAC,eAAe,OAAO,CAAC;AAE5D,IAAM,mBAAmB,oBAAI,IAAoB;AAAA,EAC/C,CAAC,0BAA0B,eAAe;AAAA,EAC1C,CAAC,0BAA0B,eAAe;AAAA,EAC1C,CAAC,8BAA8B,mBAAmB;AAAA,EAClD,CAAC,0BAA0B,eAAe;AAC5C,CAAC;AAED,IAAM,uBAAuB,oBAAI,IAAI,CAAC,0BAA0B,8BAA8B,wBAAwB,CAAC;AAIvH,IAAM,cAAe,OAAO,gBAAgB,cAAc,MAAM;AAAC,IAAI;AAErE,SAAS,gBAAgB,OAA0C;AACjE,MAAI,UAAU,QAAQ,MAAM,KAAK,MAAM,GAAI,QAAO;AAClD,SAAO,OAAO,KAAK;AACrB;AAEO,SAAS,kBAAkB,SAA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,MAAI,OAAO,mBAAmB,YAAa,QAAO;AAClD,MAAI,eAAe,IAAI,0BAA0B,MAAM,QAAW;AAChE,mBAAe,OAAO,4BAA4B,2BAA2B;AAAA,EAC/E;AACA,SAAO;AACT;AAEA,2BAA2B;","names":[]}
|
package/dist/integrity.json
CHANGED
|
@@ -1,39 +1,50 @@
|
|
|
1
1
|
{
|
|
2
|
-
"version": "1.
|
|
3
|
-
"builtAt": "2026-
|
|
2
|
+
"version": "1.2.0",
|
|
3
|
+
"builtAt": "2026-10-08T20:44:08.691Z",
|
|
4
4
|
"files": {
|
|
5
5
|
"checkout-button.js": {
|
|
6
6
|
"kind": "iife",
|
|
7
|
-
"hashedName": "checkout-button.
|
|
8
|
-
"semverPath": "v1.
|
|
9
|
-
"semverUrl": "https://js.maytes.co/v1.
|
|
10
|
-
"hashedUrl": "https://js.maytes.co/checkout-button.
|
|
11
|
-
"sri": "sha384
|
|
12
|
-
"sha256": "
|
|
13
|
-
"shortHash": "
|
|
14
|
-
"bytes":
|
|
7
|
+
"hashedName": "checkout-button.c640180e.js",
|
|
8
|
+
"semverPath": "v1.2.0/checkout-button.js",
|
|
9
|
+
"semverUrl": "https://js.maytes.co/v1.2.0/checkout-button.js",
|
|
10
|
+
"hashedUrl": "https://js.maytes.co/checkout-button.c640180e.js",
|
|
11
|
+
"sri": "sha384-gIQUOEPPO0H+Stb2Ffa40dFxBxGnB40p1+AHttEYor5oZiVKoM+aKagIL3GuNkG1",
|
|
12
|
+
"sha256": "c640180ead4d414a04c382dabe4ec426ea3db61b5096b2c664d4ce41ef8b7607",
|
|
13
|
+
"shortHash": "c640180e",
|
|
14
|
+
"bytes": 22679
|
|
15
15
|
},
|
|
16
16
|
"checkout-button.mjs": {
|
|
17
17
|
"kind": "esm",
|
|
18
|
-
"hashedName": "checkout-button.
|
|
19
|
-
"semverPath": "v1.
|
|
20
|
-
"semverUrl": "https://js.maytes.co/v1.
|
|
21
|
-
"hashedUrl": "https://js.maytes.co/checkout-button.
|
|
22
|
-
"sri": "sha384-
|
|
23
|
-
"sha256": "
|
|
24
|
-
"shortHash": "
|
|
25
|
-
"bytes":
|
|
18
|
+
"hashedName": "checkout-button.d6e444ee.mjs",
|
|
19
|
+
"semverPath": "v1.2.0/checkout-button.mjs",
|
|
20
|
+
"semverUrl": "https://js.maytes.co/v1.2.0/checkout-button.mjs",
|
|
21
|
+
"hashedUrl": "https://js.maytes.co/checkout-button.d6e444ee.mjs",
|
|
22
|
+
"sri": "sha384-sKFZzp1oSiijC45OFR04FOjFQfgvxawtUp+u/qXvRu5s5ZF8iMSTHaQo5g+VLJ79",
|
|
23
|
+
"sha256": "d6e444ee5c08457d63716884ca15d4e982c6e7f93775a90525d208194b5070af",
|
|
24
|
+
"shortHash": "d6e444ee",
|
|
25
|
+
"bytes": 34473
|
|
26
26
|
},
|
|
27
27
|
"checkout-button.cjs": {
|
|
28
28
|
"kind": "cjs",
|
|
29
|
-
"hashedName": "checkout-button.
|
|
30
|
-
"semverPath": "v1.
|
|
31
|
-
"semverUrl": "https://js.maytes.co/v1.
|
|
32
|
-
"hashedUrl": "https://js.maytes.co/checkout-button.
|
|
33
|
-
"sri": "sha384-
|
|
34
|
-
"sha256": "
|
|
35
|
-
"shortHash": "
|
|
36
|
-
"bytes":
|
|
29
|
+
"hashedName": "checkout-button.d7bc9aea.cjs",
|
|
30
|
+
"semverPath": "v1.2.0/checkout-button.cjs",
|
|
31
|
+
"semverUrl": "https://js.maytes.co/v1.2.0/checkout-button.cjs",
|
|
32
|
+
"hashedUrl": "https://js.maytes.co/checkout-button.d7bc9aea.cjs",
|
|
33
|
+
"sri": "sha384-fkm1nIhsGYEjDw3uc4cjdxui4K8H4HuPnIQ379GP1Y7vtwFC/Dw3M85KopqvjRl1",
|
|
34
|
+
"sha256": "d7bc9aeaaf3e0145619e7d362f7b56c97d5c901de5800e46dd93c275e4ec1b48",
|
|
35
|
+
"shortHash": "d7bc9aea",
|
|
36
|
+
"bytes": 35447
|
|
37
|
+
},
|
|
38
|
+
"checkout-button.element.js": {
|
|
39
|
+
"kind": "iife-element",
|
|
40
|
+
"hashedName": "checkout-button.element.6c530c24.js",
|
|
41
|
+
"semverPath": "v1.2.0/checkout-button.element.js",
|
|
42
|
+
"semverUrl": "https://js.maytes.co/v1.2.0/checkout-button.element.js",
|
|
43
|
+
"hashedUrl": "https://js.maytes.co/checkout-button.element.6c530c24.js",
|
|
44
|
+
"sri": "sha384-Y3aDrAucY25r7DzSkYJpsjoazd67oBy4WSxbTXBgpaWaT6TOuEOW3PidCw0PeVb9",
|
|
45
|
+
"sha256": "6c530c2483a4bf568d617832155a7bc0f6a30bc527815982b7feae20801e80ed",
|
|
46
|
+
"shortHash": "6c530c24",
|
|
47
|
+
"bytes": 26172
|
|
37
48
|
}
|
|
38
49
|
}
|
|
39
50
|
}
|
package/dist/react.cjs
ADDED
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
"use strict";
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
+
var __export = (target, all) => {
|
|
8
|
+
for (var name in all)
|
|
9
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
10
|
+
};
|
|
11
|
+
var __copyProps = (to, from, except, desc) => {
|
|
12
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
13
|
+
for (let key of __getOwnPropNames(from))
|
|
14
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
15
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
16
|
+
}
|
|
17
|
+
return to;
|
|
18
|
+
};
|
|
19
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
20
|
+
|
|
21
|
+
// src/react/index.ts
|
|
22
|
+
var react_exports = {};
|
|
23
|
+
__export(react_exports, {
|
|
24
|
+
MaytesButton: () => MaytesButton,
|
|
25
|
+
MaytesProvider: () => MaytesProvider,
|
|
26
|
+
useMaytes: () => useMaytes
|
|
27
|
+
});
|
|
28
|
+
module.exports = __toCommonJS(react_exports);
|
|
29
|
+
|
|
30
|
+
// src/react/MaytesProvider.tsx
|
|
31
|
+
var import_react = require("react");
|
|
32
|
+
var import_checkout_button = require("@maytes/checkout-button");
|
|
33
|
+
var import_jsx_runtime = require("react/jsx-runtime");
|
|
34
|
+
var MaytesContext = (0, import_react.createContext)(null);
|
|
35
|
+
function MaytesProvider({ environment, createCheckout, children }) {
|
|
36
|
+
const createCheckoutRef = (0, import_react.useRef)(createCheckout);
|
|
37
|
+
const [maytes, setMaytes] = (0, import_react.useState)(null);
|
|
38
|
+
const [busy, setBusy] = (0, import_react.useState)(false);
|
|
39
|
+
(0, import_react.useEffect)(() => {
|
|
40
|
+
createCheckoutRef.current = createCheckout;
|
|
41
|
+
});
|
|
42
|
+
(0, import_react.useEffect)(() => {
|
|
43
|
+
const instance = (0, import_checkout_button.Maytes)({ environment, createCheckout: () => createCheckoutRef.current() });
|
|
44
|
+
const stopWatchingBusy = instance.onBusyChange(setBusy);
|
|
45
|
+
setMaytes(instance);
|
|
46
|
+
setBusy(false);
|
|
47
|
+
return () => {
|
|
48
|
+
stopWatchingBusy();
|
|
49
|
+
instance.destroy();
|
|
50
|
+
};
|
|
51
|
+
}, [environment]);
|
|
52
|
+
const value = (0, import_react.useMemo)(() => ({ maytes, busy }), [maytes, busy]);
|
|
53
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(MaytesContext.Provider, { value, children });
|
|
54
|
+
}
|
|
55
|
+
var NOT_READY = { outcome: "ignored" };
|
|
56
|
+
function useMaytes() {
|
|
57
|
+
const context = (0, import_react.useContext)(MaytesContext);
|
|
58
|
+
if (context === null) throw new Error("useMaytes() must be used inside <MaytesProvider>.");
|
|
59
|
+
const { maytes, busy } = context;
|
|
60
|
+
const openCheckout = (0, import_react.useCallback)(
|
|
61
|
+
(options) => maytes === null ? Promise.resolve(NOT_READY) : maytes.openCheckout(options),
|
|
62
|
+
[maytes]
|
|
63
|
+
);
|
|
64
|
+
return { openCheckout, busy };
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
// src/react/MaytesButton.tsx
|
|
68
|
+
var import_react2 = require("react");
|
|
69
|
+
|
|
70
|
+
// src/react/events.ts
|
|
71
|
+
function isRecord(value) {
|
|
72
|
+
return typeof value === "object" && value !== null;
|
|
73
|
+
}
|
|
74
|
+
function listenToInstanceEvents(target, instanceId, source, handlers) {
|
|
75
|
+
const belongs = (event) => {
|
|
76
|
+
const detail = event.detail;
|
|
77
|
+
if (!isRecord(detail) || detail.instanceId !== instanceId || detail.source !== source) return null;
|
|
78
|
+
return detail;
|
|
79
|
+
};
|
|
80
|
+
const listeners = [
|
|
81
|
+
["maytes:checkout-opened", (event) => {
|
|
82
|
+
if (belongs(event)) handlers().onOpened?.();
|
|
83
|
+
}],
|
|
84
|
+
["maytes:checkout-closed", (event) => {
|
|
85
|
+
if (belongs(event)) handlers().onClosed?.();
|
|
86
|
+
}],
|
|
87
|
+
["maytes:checkout-redirected", (event) => {
|
|
88
|
+
const detail = belongs(event);
|
|
89
|
+
if (detail) handlers().onRedirected?.(detail);
|
|
90
|
+
}],
|
|
91
|
+
["maytes:checkout-failed", (event) => {
|
|
92
|
+
const detail = belongs(event);
|
|
93
|
+
if (detail) handlers().onFailed?.(detail);
|
|
94
|
+
}]
|
|
95
|
+
];
|
|
96
|
+
for (const [name, listener] of listeners) target.addEventListener(name, listener);
|
|
97
|
+
return () => {
|
|
98
|
+
for (const [name, listener] of listeners) target.removeEventListener(name, listener);
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
// src/react/MaytesButton.tsx
|
|
103
|
+
var import_jsx_runtime2 = require("react/jsx-runtime");
|
|
104
|
+
function renderOptions({ mode, label, block, radius, height, redirectOverlay }) {
|
|
105
|
+
const options = {};
|
|
106
|
+
if (mode !== void 0) options.mode = mode;
|
|
107
|
+
if (label !== void 0) options.label = label;
|
|
108
|
+
if (block !== void 0) options.block = block;
|
|
109
|
+
if (radius !== void 0) options.radius = radius;
|
|
110
|
+
if (height !== void 0) options.height = height;
|
|
111
|
+
if (redirectOverlay !== void 0) options.redirectOverlay = redirectOverlay;
|
|
112
|
+
return options;
|
|
113
|
+
}
|
|
114
|
+
function MaytesButton({ mode, label, block, radius, height, redirectOverlay, className, onOpened, onClosed, onRedirected, onFailed }) {
|
|
115
|
+
const context = (0, import_react2.useContext)(MaytesContext);
|
|
116
|
+
if (context === null) throw new Error("<MaytesButton> must be used inside <MaytesProvider>.");
|
|
117
|
+
const { maytes } = context;
|
|
118
|
+
const containerRef = (0, import_react2.useRef)(null);
|
|
119
|
+
const handlersRef = (0, import_react2.useRef)({});
|
|
120
|
+
(0, import_react2.useEffect)(() => {
|
|
121
|
+
const handlers = {};
|
|
122
|
+
if (onOpened !== void 0) handlers.onOpened = onOpened;
|
|
123
|
+
if (onClosed !== void 0) handlers.onClosed = onClosed;
|
|
124
|
+
if (onRedirected !== void 0) handlers.onRedirected = onRedirected;
|
|
125
|
+
if (onFailed !== void 0) handlers.onFailed = onFailed;
|
|
126
|
+
handlersRef.current = handlers;
|
|
127
|
+
});
|
|
128
|
+
(0, import_react2.useEffect)(() => {
|
|
129
|
+
const container = containerRef.current;
|
|
130
|
+
if (maytes === null || container === null) return void 0;
|
|
131
|
+
return maytes.renderButton(container, renderOptions({ mode, label, block, radius, height, redirectOverlay }));
|
|
132
|
+
}, [maytes, mode, label, block, radius, height, redirectOverlay]);
|
|
133
|
+
(0, import_react2.useEffect)(() => {
|
|
134
|
+
if (maytes === null) return void 0;
|
|
135
|
+
return listenToInstanceEvents(document, maytes.instanceId, "button", () => handlersRef.current);
|
|
136
|
+
}, [maytes]);
|
|
137
|
+
return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("div", { ref: containerRef, className });
|
|
138
|
+
}
|
|
139
|
+
//# sourceMappingURL=react.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/react/index.ts","../src/react/MaytesProvider.tsx","../src/react/MaytesButton.tsx","../src/react/events.ts"],"sourcesContent":["export { MaytesProvider, useMaytes } from './MaytesProvider.js';\nexport type { MaytesProviderProps, UseMaytesResult } from './MaytesProvider.js';\nexport { MaytesButton } from './MaytesButton.js';\nexport type { MaytesButtonProps } from './MaytesButton.js';\nexport type { MaytesEventHandlers } from './events.js';\n","import { createContext, useCallback, useContext, useEffect, useMemo, useRef, useState, type ReactNode } from 'react';\nimport { Maytes } from '@maytes/checkout-button';\nimport type {\n CreateCheckoutFn,\n MaytesEnvironment,\n MaytesSDK,\n OpenCheckoutOptions,\n OpenCheckoutResult,\n} from '@maytes/checkout-button';\n\nexport interface MaytesProviderProps {\n environment: MaytesEnvironment;\n createCheckout: CreateCheckoutFn;\n children?: ReactNode;\n}\n\nexport interface MaytesContextValue {\n maytes: MaytesSDK | null;\n busy: boolean;\n}\n\nexport const MaytesContext = createContext<MaytesContextValue | null>(null);\n\nexport function MaytesProvider({ environment, createCheckout, children }: MaytesProviderProps) {\n const createCheckoutRef = useRef(createCheckout);\n const [maytes, setMaytes] = useState<MaytesSDK | null>(null);\n const [busy, setBusy] = useState(false);\n\n useEffect(() => {\n createCheckoutRef.current = createCheckout;\n });\n\n useEffect(() => {\n const instance = Maytes({ environment, createCheckout: () => createCheckoutRef.current() });\n const stopWatchingBusy = instance.onBusyChange(setBusy);\n setMaytes(instance);\n setBusy(false);\n return () => {\n stopWatchingBusy();\n instance.destroy();\n };\n }, [environment]);\n\n const value = useMemo(() => ({ maytes, busy }), [maytes, busy]);\n return <MaytesContext.Provider value={value}>{children}</MaytesContext.Provider>;\n}\n\nexport interface UseMaytesResult {\n openCheckout: (options?: OpenCheckoutOptions) => Promise<OpenCheckoutResult>;\n busy: boolean;\n}\n\nconst NOT_READY: OpenCheckoutResult = { outcome: 'ignored' };\n\nexport function useMaytes(): UseMaytesResult {\n const context = useContext(MaytesContext);\n if (context === null) throw new Error('useMaytes() must be used inside <MaytesProvider>.');\n const { maytes, busy } = context;\n const openCheckout = useCallback(\n (options?: OpenCheckoutOptions) => (maytes === null ? Promise.resolve(NOT_READY) : maytes.openCheckout(options)),\n [maytes],\n );\n return { openCheckout, busy };\n}\n","import { useContext, useEffect, useRef } from 'react';\nimport type { RenderButtonOptions } from '@maytes/checkout-button';\nimport { MaytesContext } from './MaytesProvider.js';\nimport { listenToInstanceEvents, type MaytesEventHandlers } from './events.js';\n\ntype ButtonAppearance = Pick<RenderButtonOptions, 'mode' | 'label' | 'block' | 'radius' | 'height' | 'redirectOverlay'>;\n\ntype AppearanceValues = { [K in keyof ButtonAppearance]-?: ButtonAppearance[K] | undefined };\n\nexport type MaytesButtonProps = ButtonAppearance & MaytesEventHandlers & { className?: string };\n\nfunction renderOptions({ mode, label, block, radius, height, redirectOverlay }: AppearanceValues): RenderButtonOptions {\n const options: RenderButtonOptions = {};\n if (mode !== undefined) options.mode = mode;\n if (label !== undefined) options.label = label;\n if (block !== undefined) options.block = block;\n if (radius !== undefined) options.radius = radius;\n if (height !== undefined) options.height = height;\n if (redirectOverlay !== undefined) options.redirectOverlay = redirectOverlay;\n return options;\n}\n\nexport function MaytesButton({ mode, label, block, radius, height, redirectOverlay, className, onOpened, onClosed, onRedirected, onFailed }: MaytesButtonProps) {\n const context = useContext(MaytesContext);\n if (context === null) throw new Error('<MaytesButton> must be used inside <MaytesProvider>.');\n const { maytes } = context;\n const containerRef = useRef<HTMLDivElement | null>(null);\n const handlersRef = useRef<MaytesEventHandlers>({});\n\n useEffect(() => {\n const handlers: MaytesEventHandlers = {};\n if (onOpened !== undefined) handlers.onOpened = onOpened;\n if (onClosed !== undefined) handlers.onClosed = onClosed;\n if (onRedirected !== undefined) handlers.onRedirected = onRedirected;\n if (onFailed !== undefined) handlers.onFailed = onFailed;\n handlersRef.current = handlers;\n });\n\n useEffect(() => {\n const container = containerRef.current;\n if (maytes === null || container === null) return undefined;\n return maytes.renderButton(container, renderOptions({ mode, label, block, radius, height, redirectOverlay }));\n }, [maytes, mode, label, block, radius, height, redirectOverlay]);\n\n useEffect(() => {\n if (maytes === null) return undefined;\n return listenToInstanceEvents(document, maytes.instanceId, 'button', () => handlersRef.current);\n }, [maytes]);\n\n return <div ref={containerRef} className={className} />;\n}\n","import type { CheckoutFailedDetail, CheckoutRedirectedDetail, LaunchSource } from '@maytes/checkout-button';\n\nexport interface MaytesEventHandlers {\n onOpened?: () => void;\n onClosed?: () => void;\n onRedirected?: (detail: CheckoutRedirectedDetail) => void;\n onFailed?: (detail: CheckoutFailedDetail) => void;\n}\n\nfunction isRecord(value: unknown): value is Record<string, unknown> {\n return typeof value === 'object' && value !== null;\n}\n\nexport function listenToInstanceEvents(\n target: Document,\n instanceId: string,\n source: LaunchSource,\n handlers: () => MaytesEventHandlers,\n): () => void {\n const belongs = (event: Event): Record<string, unknown> | null => {\n const detail: unknown = (event as CustomEvent<unknown>).detail;\n if (!isRecord(detail) || detail.instanceId !== instanceId || detail.source !== source) return null;\n return detail;\n };\n\n const listeners: Array<[string, (event: Event) => void]> = [\n ['maytes:checkout-opened', (event) => { if (belongs(event)) handlers().onOpened?.(); }],\n ['maytes:checkout-closed', (event) => { if (belongs(event)) handlers().onClosed?.(); }],\n ['maytes:checkout-redirected', (event) => {\n const detail = belongs(event);\n if (detail) handlers().onRedirected?.(detail as unknown as CheckoutRedirectedDetail);\n }],\n ['maytes:checkout-failed', (event) => {\n const detail = belongs(event);\n if (detail) handlers().onFailed?.(detail as unknown as CheckoutFailedDetail);\n }],\n ];\n\n for (const [name, listener] of listeners) target.addEventListener(name, listener);\n return () => {\n for (const [name, listener] of listeners) target.removeEventListener(name, listener);\n };\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACA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AAM,OAAO,OAAO,QAAQ,QAAQ,iBAAiB,WAAW,UAAU,UAAU,cAAc,SAAS,GAAsB;AAC9J,QAAM,cAAU,0BAAW,aAAa;AACxC,MAAI,YAAY,KAAM,OAAM,IAAI,MAAM,sDAAsD;AAC5F,QAAM,EAAE,OAAO,IAAI;AACnB,QAAM,mBAAe,sBAA8B,IAAI;AACvD,QAAM,kBAAc,sBAA4B,CAAC,CAAC;AAElD,+BAAU,MAAM;AACd,UAAM,WAAgC,CAAC;AACvC,QAAI,aAAa,OAAW,UAAS,WAAW;AAChD,QAAI,aAAa,OAAW,UAAS,WAAW;AAChD,QAAI,iBAAiB,OAAW,UAAS,eAAe;AACxD,QAAI,aAAa,OAAW,UAAS,WAAW;AAChD,gBAAY,UAAU;AAAA,EACxB,CAAC;AAED,+BAAU,MAAM;AACd,UAAM,YAAY,aAAa;AAC/B,QAAI,WAAW,QAAQ,cAAc,KAAM,QAAO;AAClD,WAAO,OAAO,aAAa,WAAW,cAAc,EAAE,MAAM,OAAO,OAAO,QAAQ,QAAQ,gBAAgB,CAAC,CAAC;AAAA,EAC9G,GAAG,CAAC,QAAQ,MAAM,OAAO,OAAO,QAAQ,QAAQ,eAAe,CAAC;AAEhE,+BAAU,MAAM;AACd,QAAI,WAAW,KAAM,QAAO;AAC5B,WAAO,uBAAuB,UAAU,OAAO,YAAY,UAAU,MAAM,YAAY,OAAO;AAAA,EAChG,GAAG,CAAC,MAAM,CAAC;AAEX,SAAO,6CAAC,SAAI,KAAK,cAAc,WAAsB;AACvD;","names":["import_react","import_jsx_runtime"]}
|
package/dist/react.d.cts
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import * as react from 'react';
|
|
2
|
+
import { ReactNode } from 'react';
|
|
3
|
+
import { MaytesEnvironment, CreateCheckoutFn, OpenCheckoutOptions, OpenCheckoutResult, CheckoutRedirectedDetail, CheckoutFailedDetail, RenderButtonOptions } from '@maytes/checkout-button';
|
|
4
|
+
|
|
5
|
+
interface MaytesProviderProps {
|
|
6
|
+
environment: MaytesEnvironment;
|
|
7
|
+
createCheckout: CreateCheckoutFn;
|
|
8
|
+
children?: ReactNode;
|
|
9
|
+
}
|
|
10
|
+
declare function MaytesProvider({ environment, createCheckout, children }: MaytesProviderProps): react.JSX.Element;
|
|
11
|
+
interface UseMaytesResult {
|
|
12
|
+
openCheckout: (options?: OpenCheckoutOptions) => Promise<OpenCheckoutResult>;
|
|
13
|
+
busy: boolean;
|
|
14
|
+
}
|
|
15
|
+
declare function useMaytes(): UseMaytesResult;
|
|
16
|
+
|
|
17
|
+
interface MaytesEventHandlers {
|
|
18
|
+
onOpened?: () => void;
|
|
19
|
+
onClosed?: () => void;
|
|
20
|
+
onRedirected?: (detail: CheckoutRedirectedDetail) => void;
|
|
21
|
+
onFailed?: (detail: CheckoutFailedDetail) => void;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
type ButtonAppearance = Pick<RenderButtonOptions, 'mode' | 'label' | 'block' | 'radius' | 'height' | 'redirectOverlay'>;
|
|
25
|
+
type MaytesButtonProps = ButtonAppearance & MaytesEventHandlers & {
|
|
26
|
+
className?: string;
|
|
27
|
+
};
|
|
28
|
+
declare function MaytesButton({ mode, label, block, radius, height, redirectOverlay, className, onOpened, onClosed, onRedirected, onFailed }: MaytesButtonProps): react.JSX.Element;
|
|
29
|
+
|
|
30
|
+
export { MaytesButton, type MaytesButtonProps, type MaytesEventHandlers, MaytesProvider, type MaytesProviderProps, type UseMaytesResult, useMaytes };
|
package/dist/react.d.ts
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import * as react from 'react';
|
|
2
|
+
import { ReactNode } from 'react';
|
|
3
|
+
import { MaytesEnvironment, CreateCheckoutFn, OpenCheckoutOptions, OpenCheckoutResult, CheckoutRedirectedDetail, CheckoutFailedDetail, RenderButtonOptions } from '@maytes/checkout-button';
|
|
4
|
+
|
|
5
|
+
interface MaytesProviderProps {
|
|
6
|
+
environment: MaytesEnvironment;
|
|
7
|
+
createCheckout: CreateCheckoutFn;
|
|
8
|
+
children?: ReactNode;
|
|
9
|
+
}
|
|
10
|
+
declare function MaytesProvider({ environment, createCheckout, children }: MaytesProviderProps): react.JSX.Element;
|
|
11
|
+
interface UseMaytesResult {
|
|
12
|
+
openCheckout: (options?: OpenCheckoutOptions) => Promise<OpenCheckoutResult>;
|
|
13
|
+
busy: boolean;
|
|
14
|
+
}
|
|
15
|
+
declare function useMaytes(): UseMaytesResult;
|
|
16
|
+
|
|
17
|
+
interface MaytesEventHandlers {
|
|
18
|
+
onOpened?: () => void;
|
|
19
|
+
onClosed?: () => void;
|
|
20
|
+
onRedirected?: (detail: CheckoutRedirectedDetail) => void;
|
|
21
|
+
onFailed?: (detail: CheckoutFailedDetail) => void;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
type ButtonAppearance = Pick<RenderButtonOptions, 'mode' | 'label' | 'block' | 'radius' | 'height' | 'redirectOverlay'>;
|
|
25
|
+
type MaytesButtonProps = ButtonAppearance & MaytesEventHandlers & {
|
|
26
|
+
className?: string;
|
|
27
|
+
};
|
|
28
|
+
declare function MaytesButton({ mode, label, block, radius, height, redirectOverlay, className, onOpened, onClosed, onRedirected, onFailed }: MaytesButtonProps): react.JSX.Element;
|
|
29
|
+
|
|
30
|
+
export { MaytesButton, type MaytesButtonProps, type MaytesEventHandlers, MaytesProvider, type MaytesProviderProps, type UseMaytesResult, useMaytes };
|
package/dist/react.mjs
ADDED
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
// src/react/MaytesProvider.tsx
|
|
4
|
+
import { createContext, useCallback, useContext, useEffect, useMemo, useRef, useState } from "react";
|
|
5
|
+
import { Maytes } from "@maytes/checkout-button";
|
|
6
|
+
import { jsx } from "react/jsx-runtime";
|
|
7
|
+
var MaytesContext = createContext(null);
|
|
8
|
+
function MaytesProvider({ environment, createCheckout, children }) {
|
|
9
|
+
const createCheckoutRef = useRef(createCheckout);
|
|
10
|
+
const [maytes, setMaytes] = useState(null);
|
|
11
|
+
const [busy, setBusy] = useState(false);
|
|
12
|
+
useEffect(() => {
|
|
13
|
+
createCheckoutRef.current = createCheckout;
|
|
14
|
+
});
|
|
15
|
+
useEffect(() => {
|
|
16
|
+
const instance = Maytes({ environment, createCheckout: () => createCheckoutRef.current() });
|
|
17
|
+
const stopWatchingBusy = instance.onBusyChange(setBusy);
|
|
18
|
+
setMaytes(instance);
|
|
19
|
+
setBusy(false);
|
|
20
|
+
return () => {
|
|
21
|
+
stopWatchingBusy();
|
|
22
|
+
instance.destroy();
|
|
23
|
+
};
|
|
24
|
+
}, [environment]);
|
|
25
|
+
const value = useMemo(() => ({ maytes, busy }), [maytes, busy]);
|
|
26
|
+
return /* @__PURE__ */ jsx(MaytesContext.Provider, { value, children });
|
|
27
|
+
}
|
|
28
|
+
var NOT_READY = { outcome: "ignored" };
|
|
29
|
+
function useMaytes() {
|
|
30
|
+
const context = useContext(MaytesContext);
|
|
31
|
+
if (context === null) throw new Error("useMaytes() must be used inside <MaytesProvider>.");
|
|
32
|
+
const { maytes, busy } = context;
|
|
33
|
+
const openCheckout = useCallback(
|
|
34
|
+
(options) => maytes === null ? Promise.resolve(NOT_READY) : maytes.openCheckout(options),
|
|
35
|
+
[maytes]
|
|
36
|
+
);
|
|
37
|
+
return { openCheckout, busy };
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
// src/react/MaytesButton.tsx
|
|
41
|
+
import { useContext as useContext2, useEffect as useEffect2, useRef as useRef2 } from "react";
|
|
42
|
+
|
|
43
|
+
// src/react/events.ts
|
|
44
|
+
function isRecord(value) {
|
|
45
|
+
return typeof value === "object" && value !== null;
|
|
46
|
+
}
|
|
47
|
+
function listenToInstanceEvents(target, instanceId, source, handlers) {
|
|
48
|
+
const belongs = (event) => {
|
|
49
|
+
const detail = event.detail;
|
|
50
|
+
if (!isRecord(detail) || detail.instanceId !== instanceId || detail.source !== source) return null;
|
|
51
|
+
return detail;
|
|
52
|
+
};
|
|
53
|
+
const listeners = [
|
|
54
|
+
["maytes:checkout-opened", (event) => {
|
|
55
|
+
if (belongs(event)) handlers().onOpened?.();
|
|
56
|
+
}],
|
|
57
|
+
["maytes:checkout-closed", (event) => {
|
|
58
|
+
if (belongs(event)) handlers().onClosed?.();
|
|
59
|
+
}],
|
|
60
|
+
["maytes:checkout-redirected", (event) => {
|
|
61
|
+
const detail = belongs(event);
|
|
62
|
+
if (detail) handlers().onRedirected?.(detail);
|
|
63
|
+
}],
|
|
64
|
+
["maytes:checkout-failed", (event) => {
|
|
65
|
+
const detail = belongs(event);
|
|
66
|
+
if (detail) handlers().onFailed?.(detail);
|
|
67
|
+
}]
|
|
68
|
+
];
|
|
69
|
+
for (const [name, listener] of listeners) target.addEventListener(name, listener);
|
|
70
|
+
return () => {
|
|
71
|
+
for (const [name, listener] of listeners) target.removeEventListener(name, listener);
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
// src/react/MaytesButton.tsx
|
|
76
|
+
import { jsx as jsx2 } from "react/jsx-runtime";
|
|
77
|
+
function renderOptions({ mode, label, block, radius, height, redirectOverlay }) {
|
|
78
|
+
const options = {};
|
|
79
|
+
if (mode !== void 0) options.mode = mode;
|
|
80
|
+
if (label !== void 0) options.label = label;
|
|
81
|
+
if (block !== void 0) options.block = block;
|
|
82
|
+
if (radius !== void 0) options.radius = radius;
|
|
83
|
+
if (height !== void 0) options.height = height;
|
|
84
|
+
if (redirectOverlay !== void 0) options.redirectOverlay = redirectOverlay;
|
|
85
|
+
return options;
|
|
86
|
+
}
|
|
87
|
+
function MaytesButton({ mode, label, block, radius, height, redirectOverlay, className, onOpened, onClosed, onRedirected, onFailed }) {
|
|
88
|
+
const context = useContext2(MaytesContext);
|
|
89
|
+
if (context === null) throw new Error("<MaytesButton> must be used inside <MaytesProvider>.");
|
|
90
|
+
const { maytes } = context;
|
|
91
|
+
const containerRef = useRef2(null);
|
|
92
|
+
const handlersRef = useRef2({});
|
|
93
|
+
useEffect2(() => {
|
|
94
|
+
const handlers = {};
|
|
95
|
+
if (onOpened !== void 0) handlers.onOpened = onOpened;
|
|
96
|
+
if (onClosed !== void 0) handlers.onClosed = onClosed;
|
|
97
|
+
if (onRedirected !== void 0) handlers.onRedirected = onRedirected;
|
|
98
|
+
if (onFailed !== void 0) handlers.onFailed = onFailed;
|
|
99
|
+
handlersRef.current = handlers;
|
|
100
|
+
});
|
|
101
|
+
useEffect2(() => {
|
|
102
|
+
const container = containerRef.current;
|
|
103
|
+
if (maytes === null || container === null) return void 0;
|
|
104
|
+
return maytes.renderButton(container, renderOptions({ mode, label, block, radius, height, redirectOverlay }));
|
|
105
|
+
}, [maytes, mode, label, block, radius, height, redirectOverlay]);
|
|
106
|
+
useEffect2(() => {
|
|
107
|
+
if (maytes === null) return void 0;
|
|
108
|
+
return listenToInstanceEvents(document, maytes.instanceId, "button", () => handlersRef.current);
|
|
109
|
+
}, [maytes]);
|
|
110
|
+
return /* @__PURE__ */ jsx2("div", { ref: containerRef, className });
|
|
111
|
+
}
|
|
112
|
+
export {
|
|
113
|
+
MaytesButton,
|
|
114
|
+
MaytesProvider,
|
|
115
|
+
useMaytes
|
|
116
|
+
};
|
|
117
|
+
//# sourceMappingURL=react.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/react/MaytesProvider.tsx","../src/react/MaytesButton.tsx","../src/react/events.ts"],"sourcesContent":["import { createContext, useCallback, useContext, useEffect, useMemo, useRef, useState, type ReactNode } from 'react';\nimport { Maytes } from '@maytes/checkout-button';\nimport type {\n CreateCheckoutFn,\n MaytesEnvironment,\n MaytesSDK,\n OpenCheckoutOptions,\n OpenCheckoutResult,\n} from '@maytes/checkout-button';\n\nexport interface MaytesProviderProps {\n environment: MaytesEnvironment;\n createCheckout: CreateCheckoutFn;\n children?: ReactNode;\n}\n\nexport interface MaytesContextValue {\n maytes: MaytesSDK | null;\n busy: boolean;\n}\n\nexport const MaytesContext = createContext<MaytesContextValue | null>(null);\n\nexport function MaytesProvider({ environment, createCheckout, children }: MaytesProviderProps) {\n const createCheckoutRef = useRef(createCheckout);\n const [maytes, setMaytes] = useState<MaytesSDK | null>(null);\n const [busy, setBusy] = useState(false);\n\n useEffect(() => {\n createCheckoutRef.current = createCheckout;\n });\n\n useEffect(() => {\n const instance = Maytes({ environment, createCheckout: () => createCheckoutRef.current() });\n const stopWatchingBusy = instance.onBusyChange(setBusy);\n setMaytes(instance);\n setBusy(false);\n return () => {\n stopWatchingBusy();\n instance.destroy();\n };\n }, [environment]);\n\n const value = useMemo(() => ({ maytes, busy }), [maytes, busy]);\n return <MaytesContext.Provider value={value}>{children}</MaytesContext.Provider>;\n}\n\nexport interface UseMaytesResult {\n openCheckout: (options?: OpenCheckoutOptions) => Promise<OpenCheckoutResult>;\n busy: boolean;\n}\n\nconst NOT_READY: OpenCheckoutResult = { outcome: 'ignored' };\n\nexport function useMaytes(): UseMaytesResult {\n const context = useContext(MaytesContext);\n if (context === null) throw new Error('useMaytes() must be used inside <MaytesProvider>.');\n const { maytes, busy } = context;\n const openCheckout = useCallback(\n (options?: OpenCheckoutOptions) => (maytes === null ? Promise.resolve(NOT_READY) : maytes.openCheckout(options)),\n [maytes],\n );\n return { openCheckout, busy };\n}\n","import { useContext, useEffect, useRef } from 'react';\nimport type { RenderButtonOptions } from '@maytes/checkout-button';\nimport { MaytesContext } from './MaytesProvider.js';\nimport { listenToInstanceEvents, type MaytesEventHandlers } from './events.js';\n\ntype ButtonAppearance = Pick<RenderButtonOptions, 'mode' | 'label' | 'block' | 'radius' | 'height' | 'redirectOverlay'>;\n\ntype AppearanceValues = { [K in keyof ButtonAppearance]-?: ButtonAppearance[K] | undefined };\n\nexport type MaytesButtonProps = ButtonAppearance & MaytesEventHandlers & { className?: string };\n\nfunction renderOptions({ mode, label, block, radius, height, redirectOverlay }: AppearanceValues): RenderButtonOptions {\n const options: RenderButtonOptions = {};\n if (mode !== undefined) options.mode = mode;\n if (label !== undefined) options.label = label;\n if (block !== undefined) options.block = block;\n if (radius !== undefined) options.radius = radius;\n if (height !== undefined) options.height = height;\n if (redirectOverlay !== undefined) options.redirectOverlay = redirectOverlay;\n return options;\n}\n\nexport function MaytesButton({ mode, label, block, radius, height, redirectOverlay, className, onOpened, onClosed, onRedirected, onFailed }: MaytesButtonProps) {\n const context = useContext(MaytesContext);\n if (context === null) throw new Error('<MaytesButton> must be used inside <MaytesProvider>.');\n const { maytes } = context;\n const containerRef = useRef<HTMLDivElement | null>(null);\n const handlersRef = useRef<MaytesEventHandlers>({});\n\n useEffect(() => {\n const handlers: MaytesEventHandlers = {};\n if (onOpened !== undefined) handlers.onOpened = onOpened;\n if (onClosed !== undefined) handlers.onClosed = onClosed;\n if (onRedirected !== undefined) handlers.onRedirected = onRedirected;\n if (onFailed !== undefined) handlers.onFailed = onFailed;\n handlersRef.current = handlers;\n });\n\n useEffect(() => {\n const container = containerRef.current;\n if (maytes === null || container === null) return undefined;\n return maytes.renderButton(container, renderOptions({ mode, label, block, radius, height, redirectOverlay }));\n }, [maytes, mode, label, block, radius, height, redirectOverlay]);\n\n useEffect(() => {\n if (maytes === null) return undefined;\n return listenToInstanceEvents(document, maytes.instanceId, 'button', () => handlersRef.current);\n }, [maytes]);\n\n return <div ref={containerRef} className={className} />;\n}\n","import type { CheckoutFailedDetail, CheckoutRedirectedDetail, LaunchSource } from '@maytes/checkout-button';\n\nexport interface MaytesEventHandlers {\n onOpened?: () => void;\n onClosed?: () => void;\n onRedirected?: (detail: CheckoutRedirectedDetail) => void;\n onFailed?: (detail: CheckoutFailedDetail) => void;\n}\n\nfunction isRecord(value: unknown): value is Record<string, unknown> {\n return typeof value === 'object' && value !== null;\n}\n\nexport function listenToInstanceEvents(\n target: Document,\n instanceId: string,\n source: LaunchSource,\n handlers: () => MaytesEventHandlers,\n): () => void {\n const belongs = (event: Event): Record<string, unknown> | null => {\n const detail: unknown = (event as CustomEvent<unknown>).detail;\n if (!isRecord(detail) || detail.instanceId !== instanceId || detail.source !== source) return null;\n return detail;\n };\n\n const listeners: Array<[string, (event: Event) => void]> = [\n ['maytes:checkout-opened', (event) => { if (belongs(event)) handlers().onOpened?.(); }],\n ['maytes:checkout-closed', (event) => { if (belongs(event)) handlers().onClosed?.(); }],\n ['maytes:checkout-redirected', (event) => {\n const detail = belongs(event);\n if (detail) handlers().onRedirected?.(detail as unknown as CheckoutRedirectedDetail);\n }],\n ['maytes:checkout-failed', (event) => {\n const detail = belongs(event);\n if (detail) handlers().onFailed?.(detail as unknown as CheckoutFailedDetail);\n }],\n ];\n\n for (const [name, listener] of listeners) target.addEventListener(name, listener);\n return () => {\n for (const [name, listener] of listeners) target.removeEventListener(name, listener);\n };\n}\n"],"mappings":";;;AAAA,SAAS,eAAe,aAAa,YAAY,WAAW,SAAS,QAAQ,gBAAgC;AAC7G,SAAS,cAAc;AA2Cd;AAvBF,IAAM,gBAAgB,cAAyC,IAAI;AAEnE,SAAS,eAAe,EAAE,aAAa,gBAAgB,SAAS,GAAwB;AAC7F,QAAM,oBAAoB,OAAO,cAAc;AAC/C,QAAM,CAAC,QAAQ,SAAS,IAAI,SAA2B,IAAI;AAC3D,QAAM,CAAC,MAAM,OAAO,IAAI,SAAS,KAAK;AAEtC,YAAU,MAAM;AACd,sBAAkB,UAAU;AAAA,EAC9B,CAAC;AAED,YAAU,MAAM;AACd,UAAM,WAAW,OAAO,EAAE,aAAa,gBAAgB,MAAM,kBAAkB,QAAQ,EAAE,CAAC;AAC1F,UAAM,mBAAmB,SAAS,aAAa,OAAO;AACtD,cAAU,QAAQ;AAClB,YAAQ,KAAK;AACb,WAAO,MAAM;AACX,uBAAiB;AACjB,eAAS,QAAQ;AAAA,IACnB;AAAA,EACF,GAAG,CAAC,WAAW,CAAC;AAEhB,QAAM,QAAQ,QAAQ,OAAO,EAAE,QAAQ,KAAK,IAAI,CAAC,QAAQ,IAAI,CAAC;AAC9D,SAAO,oBAAC,cAAc,UAAd,EAAuB,OAAe,UAAS;AACzD;AAOA,I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