@cowprotocol/widget-react 0.2.0 → 0.2.1
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/index.js +1 -1
- package/index.mjs +30 -31
- package/package.json +19 -5
package/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const l=require("react/jsx-runtime"),
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const l=require("react/jsx-runtime"),i=require("react"),w="2.0",g="*",m=["connect","disconnect","close","chainChanged","accountsChanged"];class E{constructor(e){this.contentWindow=e,window.addEventListener("message",this.processEvent)}ethereumProvider=null;requests={};disconnect(){this.ethereumProvider=null,window.removeEventListener("message",this.processEvent)}onConnect(e){this.ethereumProvider=e,Object.keys(this.requests).forEach(s=>{this.processRequest(this.requests[s])}),this.requests={},m.forEach(s=>{e.on(s,n=>{this.postMessage({method:s,params:[n]})})})}processRequest(e){if(!this.ethereumProvider)return;(e.method==="enable"?this.ethereumProvider.enable():this.ethereumProvider.request(e)).then(n=>{this.postMessage({id:e.id,result:n})}).catch(n=>{this.postMessage({id:e.id,error:n})})}processEvent=e=>{e.data.jsonrpc==="2.0"&&(this.ethereumProvider?this.processRequest(e.data):this.requests[e.data.id]=e.data)};postMessage(e){this.contentWindow.postMessage({jsonrpc:w,...e},g)}}function y(){return typeof window>"u"||!window?"":`https://swap-dev-git-${window.location.hostname.replace("widget-configurator-git-","").replace("-cowswap.vercel.app","")}-cowswap.vercel.app`}const R={local:"http://localhost:3000",prod:"https://swap.cow.fi",dev:"https://dev.swap.cow.fi",pr:y()};var u=(t=>(t.SWAP="swap",t.LIMIT="limit",t.ADVANCED="advanced",t))(u||{});const a="_";function v(t){const e=R[t.env||"prod"],s=h(t),n=p(t);return e+"/#"+s+"?"+n}function h(t){const{chainId:e=1,sell:s,buy:n,tradeType:o=u.SWAP}=t,r=[s?.asset||a,n?.asset||a].map(encodeURIComponent).join("/");return`/${e}/widget/${o}/${r}`}function p(t){const{sell:e,buy:s,theme:n}=t,o=new URLSearchParams;return e?.amount&&o.append("sellAmount",e.amount),s?.amount&&o.append("buyAmount",s.amount),n&&o.append("theme",typeof n=="string"?n:n.baseTheme),o}const c="cowSwapWidget",f="640px",P="450px",W=20;function S(t,e={}){const{provider:s}=e,n=T(e);t.innerHTML="",t.appendChild(n);const{contentWindow:o}=n;if(!o)throw new Error("Iframe does not contain a window!");return C(o,e.appCode),M(n,e.height),s&&new E(o).onConnect(s),n.addEventListener("load",()=>{d(e,o)}),r=>d(r,o)}function T(t){const{width:e=P,height:s=f}=t,n=document.createElement("iframe");return n.src=v(t),n.width=e,n.height=s,n.style.border="0",n}function d(t,e){const s=h(t),n=p(t).toString();e.postMessage({key:c,method:"update",urlParams:{pathname:s,search:n},appParams:{...t,provider:void 0}},"*")}function C(t,e){window.addEventListener("message",s=>{s.data.key!==c||s.data.method!=="activate"||t.postMessage({key:c,method:"metaData",metaData:e?{appCode:e}:void 0},"*")})}function M(t,e=f){window.addEventListener("message",s=>{s.data.key!==c||s.data.method!=="iframeHeight"||(t.style.height=s.data.height?`${s.data.height+W}px`:e)})}function b({params:t,provider:e}){const s=i.useRef(),n=i.useRef(null),o=i.useRef(null);return i.useEffect(()=>{n.current&&(e&&s.current!==e&&(o.current=null),o.current?o.current(t):o.current=S(n.current,t))},[e,t]),i.useEffect(()=>{s.current=e},[e]),l.jsx("div",{ref:n})}exports.CowSwapWidget=b;
|
package/index.mjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { jsx as w } from "react/jsx-runtime";
|
|
2
|
-
import { useRef as
|
|
2
|
+
import { useRef as c, useEffect as a } from "react";
|
|
3
3
|
const m = "2.0", g = "*", E = ["connect", "disconnect", "close", "chainChanged", "accountsChanged"];
|
|
4
4
|
class v {
|
|
5
5
|
/**
|
|
@@ -64,44 +64,43 @@ class v {
|
|
|
64
64
|
function y() {
|
|
65
65
|
return typeof window > "u" || !window ? "" : `https://swap-dev-git-${window.location.hostname.replace("widget-configurator-git-", "").replace("-cowswap.vercel.app", "")}-cowswap.vercel.app`;
|
|
66
66
|
}
|
|
67
|
-
const
|
|
67
|
+
const P = {
|
|
68
68
|
local: "http://localhost:3000",
|
|
69
69
|
prod: "https://swap.cow.fi",
|
|
70
70
|
dev: "https://dev.swap.cow.fi",
|
|
71
71
|
pr: y()
|
|
72
72
|
};
|
|
73
|
-
var
|
|
74
|
-
|
|
75
|
-
|
|
73
|
+
var h = /* @__PURE__ */ ((t) => (t.SWAP = "swap", t.LIMIT = "limit", t.ADVANCED = "advanced", t))(h || {});
|
|
74
|
+
const d = "_";
|
|
75
|
+
function W(t) {
|
|
76
|
+
const e = P[t.env || "prod"], s = p(t), n = f(t);
|
|
76
77
|
return e + "/#" + s + "?" + n;
|
|
77
78
|
}
|
|
78
|
-
function h(t) {
|
|
79
|
-
const { chainId: e = 1, sell: s, buy: n, tradeType: o = u.SWAP } = t, i = [];
|
|
80
|
-
s?.asset && i.push(s.asset), n?.asset && i.push(n.asset);
|
|
81
|
-
const l = i.map(encodeURIComponent).join("/");
|
|
82
|
-
return `/${e}/widget/${o}/${l}`;
|
|
83
|
-
}
|
|
84
79
|
function p(t) {
|
|
80
|
+
const { chainId: e = 1, sell: s, buy: n, tradeType: o = h.SWAP } = t, i = [s?.asset || d, n?.asset || d].map(encodeURIComponent).join("/");
|
|
81
|
+
return `/${e}/widget/${o}/${i}`;
|
|
82
|
+
}
|
|
83
|
+
function f(t) {
|
|
85
84
|
const { sell: e, buy: s, theme: n } = t, o = new URLSearchParams();
|
|
86
85
|
return e?.amount && o.append("sellAmount", e.amount), s?.amount && o.append("buyAmount", s.amount), n && o.append("theme", typeof n == "string" ? n : n.baseTheme), o;
|
|
87
86
|
}
|
|
88
|
-
const r = "cowSwapWidget",
|
|
89
|
-
function
|
|
90
|
-
const { provider: s } = e, n =
|
|
87
|
+
const r = "cowSwapWidget", l = "640px", R = "450px", T = 20;
|
|
88
|
+
function A(t, e = {}) {
|
|
89
|
+
const { provider: s } = e, n = C(e);
|
|
91
90
|
t.innerHTML = "", t.appendChild(n);
|
|
92
91
|
const { contentWindow: o } = n;
|
|
93
92
|
if (!o)
|
|
94
93
|
throw new Error("Iframe does not contain a window!");
|
|
95
|
-
return
|
|
96
|
-
|
|
97
|
-
}), (i) =>
|
|
94
|
+
return M(o, e.appCode), S(n, e.height), s && new v(o).onConnect(s), n.addEventListener("load", () => {
|
|
95
|
+
u(e, o);
|
|
96
|
+
}), (i) => u(i, o);
|
|
98
97
|
}
|
|
99
|
-
function
|
|
100
|
-
const { width: e = R, height: s =
|
|
101
|
-
return n.src =
|
|
98
|
+
function C(t) {
|
|
99
|
+
const { width: e = R, height: s = l } = t, n = document.createElement("iframe");
|
|
100
|
+
return n.src = W(t), n.width = e, n.height = s, n.style.border = "0", n;
|
|
102
101
|
}
|
|
103
|
-
function
|
|
104
|
-
const s =
|
|
102
|
+
function u(t, e) {
|
|
103
|
+
const s = p(t), n = f(t).toString();
|
|
105
104
|
e.postMessage(
|
|
106
105
|
{
|
|
107
106
|
key: r,
|
|
@@ -118,7 +117,7 @@ function d(t, e) {
|
|
|
118
117
|
"*"
|
|
119
118
|
);
|
|
120
119
|
}
|
|
121
|
-
function
|
|
120
|
+
function M(t, e) {
|
|
122
121
|
window.addEventListener("message", (s) => {
|
|
123
122
|
s.data.key !== r || s.data.method !== "activate" || t.postMessage(
|
|
124
123
|
{
|
|
@@ -130,19 +129,19 @@ function I(t, e) {
|
|
|
130
129
|
);
|
|
131
130
|
});
|
|
132
131
|
}
|
|
133
|
-
function
|
|
132
|
+
function S(t, e = l) {
|
|
134
133
|
window.addEventListener("message", (s) => {
|
|
135
|
-
s.data.key !== r || s.data.method !== "iframeHeight" || (t.style.height = s.data.height ? `${s.data.height +
|
|
134
|
+
s.data.key !== r || s.data.method !== "iframeHeight" || (t.style.height = s.data.height ? `${s.data.height + T}px` : e);
|
|
136
135
|
});
|
|
137
136
|
}
|
|
138
|
-
function
|
|
139
|
-
const s =
|
|
140
|
-
return
|
|
141
|
-
n.current && (e && s.current !== e && (o.current = null), o.current ? o.current(t) : o.current =
|
|
142
|
-
}, [e, t]),
|
|
137
|
+
function L({ params: t, provider: e }) {
|
|
138
|
+
const s = c(), n = c(null), o = c(null);
|
|
139
|
+
return a(() => {
|
|
140
|
+
n.current && (e && s.current !== e && (o.current = null), o.current ? o.current(t) : o.current = A(n.current, t));
|
|
141
|
+
}, [e, t]), a(() => {
|
|
143
142
|
s.current = e;
|
|
144
143
|
}, [e]), /* @__PURE__ */ w("div", { ref: n });
|
|
145
144
|
}
|
|
146
145
|
export {
|
|
147
|
-
|
|
146
|
+
L as CowSwapWidget
|
|
148
147
|
};
|
package/package.json
CHANGED
|
@@ -1,12 +1,26 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cowprotocol/widget-react",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.1",
|
|
4
4
|
"main": "./index.js",
|
|
5
5
|
"types": "./index.d.ts",
|
|
6
6
|
"exports": {
|
|
7
|
-
"
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
7
|
+
"import": "./index.mjs",
|
|
8
|
+
"require": "./index.js"
|
|
9
|
+
},
|
|
10
|
+
"license": "ISC",
|
|
11
|
+
"repository": {
|
|
12
|
+
"type": "git",
|
|
13
|
+
"url": "https://github.com/cowprotocol/cowswap"
|
|
14
|
+
},
|
|
15
|
+
"keywords": [
|
|
16
|
+
"cowprotocol",
|
|
17
|
+
"widget",
|
|
18
|
+
"cowswap",
|
|
19
|
+
"dex",
|
|
20
|
+
"widget-lib",
|
|
21
|
+
"react"
|
|
22
|
+
],
|
|
23
|
+
"dependencies": {
|
|
24
|
+
"@cowprotocol/widget-lib": "^0.2.1"
|
|
11
25
|
}
|
|
12
26
|
}
|