@cowprotocol/widget-react 0.2.0 → 0.2.2

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.
Files changed (3) hide show
  1. package/index.js +1 -1
  2. package/index.mjs +30 -31
  3. package/package.json +22 -7
package/index.js CHANGED
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const l=require("react/jsx-runtime"),r=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 d=(t=>(t.SWAP="swap",t.LIMIT="limit",t.ADVANCED="advanced",t))(d||{});function v(t){const e=R[t.env||"prod"],s=u(t),n=h(t);return e+"/#"+s+"?"+n}function u(t){const{chainId:e=1,sell:s,buy:n,tradeType:o=d.SWAP}=t,i=[];s?.asset&&i.push(s.asset),n?.asset&&i.push(n.asset);const f=i.map(encodeURIComponent).join("/");return`/${e}/widget/${o}/${f}`}function h(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 a="cowSwapWidget",p="640px",W="450px",P=20;function S(t,e={}){const{provider:s}=e,n=C(e);t.innerHTML="",t.appendChild(n);const{contentWindow:o}=n;if(!o)throw new Error("Iframe does not contain a window!");return b(o,e.appCode),A(n,e.height),s&&new E(o).onConnect(s),n.addEventListener("load",()=>{c(e,o)}),i=>c(i,o)}function C(t){const{width:e=W,height:s=p}=t,n=document.createElement("iframe");return n.src=v(t),n.width=e,n.height=s,n.style.border="0",n}function c(t,e){const s=u(t),n=h(t).toString();e.postMessage({key:a,method:"update",urlParams:{pathname:s,search:n},appParams:{...t,provider:void 0}},"*")}function b(t,e){window.addEventListener("message",s=>{s.data.key!==a||s.data.method!=="activate"||t.postMessage({key:a,method:"metaData",metaData:e?{appCode:e}:void 0},"*")})}function A(t,e=p){window.addEventListener("message",s=>{s.data.key!==a||s.data.method!=="iframeHeight"||(t.style.height=s.data.height?`${s.data.height+P}px`:e)})}function M({params:t,provider:e}){const s=r.useRef(),n=r.useRef(null),o=r.useRef(null);return r.useEffect(()=>{n.current&&(e&&s.current!==e&&(o.current=null),o.current?o.current(t):o.current=S(n.current,t))},[e,t]),r.useEffect(()=>{s.current=e},[e]),l.jsx("div",{ref:n})}exports.CowSwapWidget=M;
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 a, useEffect as c } from "react";
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 W = {
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 u = /* @__PURE__ */ ((t) => (t.SWAP = "swap", t.LIMIT = "limit", t.ADVANCED = "advanced", t))(u || {});
74
- function P(t) {
75
- const e = W[t.env || "prod"], s = h(t), n = p(t);
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", f = "640px", R = "450px", A = 20;
89
- function C(t, e = {}) {
90
- const { provider: s } = e, n = S(e);
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 I(o, e.appCode), L(n, e.height), s && new v(o).onConnect(s), n.addEventListener("load", () => {
96
- d(e, o);
97
- }), (i) => d(i, o);
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 S(t) {
100
- const { width: e = R, height: s = f } = t, n = document.createElement("iframe");
101
- return n.src = P(t), n.width = e, n.height = s, n.style.border = "0", n;
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 d(t, e) {
104
- const s = h(t), n = p(t).toString();
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 I(t, e) {
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 L(t, e = f) {
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 + A}px` : e);
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 b({ params: t, provider: e }) {
139
- const s = a(), n = a(null), o = a(null);
140
- return c(() => {
141
- n.current && (e && s.current !== e && (o.current = null), o.current ? o.current(t) : o.current = C(n.current, t));
142
- }, [e, t]), c(() => {
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
- b as CowSwapWidget
146
+ L as CowSwapWidget
148
147
  };
package/package.json CHANGED
@@ -1,12 +1,27 @@
1
1
  {
2
2
  "name": "@cowprotocol/widget-react",
3
- "version": "0.2.0",
4
- "main": "./index.js",
5
- "types": "./index.d.ts",
3
+ "version": "0.2.2",
4
+ "type": "commonjs",
5
+ "description": "CoW Swap Widget Library. Allows you to easily embed a CoW Swap widget on your React application.",
6
+ "main": "index.js",
6
7
  "exports": {
7
- ".": {
8
- "import": "./index.mjs",
9
- "require": "./index.js"
10
- }
8
+ "import": "./index.js",
9
+ "require": "./index.mjs"
10
+ },
11
+ "license": "ISC",
12
+ "repository": {
13
+ "type": "git",
14
+ "url": "https://github.com/cowprotocol/cowswap"
15
+ },
16
+ "keywords": [
17
+ "cowprotocol",
18
+ "widget",
19
+ "cowswap",
20
+ "dex",
21
+ "widget-lib",
22
+ "react"
23
+ ],
24
+ "dependencies": {
25
+ "@cowprotocol/widget-lib": "^0.2.1"
11
26
  }
12
27
  }