@cowprotocol/widget-react 3.2.11 → 3.3.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.
Files changed (3) hide show
  1. package/index.cjs +1 -1
  2. package/index.js +81 -77
  3. package/package.json +4 -4
package/index.cjs CHANGED
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const O=require("@cowprotocol/widget-lib"),j=require("react/jsx-runtime"),n=require("react");function m({params:e,provider:c,listeners:r,onReady:i,onLoadingError:f,enableSafeSdkBridge:t=!0}){const[a,d]=n.useState(null),y=n.useRef(null),l=n.useRef(c),p=n.useRef(r),R=n.useRef(t),h=n.useRef(null),o=n.useRef(null),u=n.useCallback((s,E)=>{try{console.log(`[WIDGET] ${s}`),E()}catch(g){const w=`Error ${s.toLowerCase()}`,W=g instanceof Error?g:new Error("Unknown CowSwapWidget error",{cause:g});console.error(`[WIDGET] ${w}`,W),d({message:w,error:W})}},[]);return x({refs:{paramsRef:y,providerRef:l,listenersRef:p,enableSafeSdkBridgeRef:R,widgetHandlerRef:o},tryOrHandleError:u}),n.useEffect(()=>{const s=!!y.current&&D(y.current,e),E=R.current!==t;if(!h.current||!s&&!E&&JSON.stringify(y.current)===JSON.stringify(e))return;const g=h.current,w=o.current;y.current=e,R.current=t,w===null?u("Creating a new widget",()=>{o.current=v({container:g,params:e,provider:l.current,listeners:r,onReady:i,onLoadingError:f,enableSafeSdkBridge:t}),p.current=r}):E?u("Recreating the widget",()=>{w.destroy(),o.current=v({container:g,params:e,provider:l.current,listeners:r,onReady:i,onLoadingError:f,enableSafeSdkBridge:t}),p.current=r}):u("Updating the widget",()=>w.updateParams(e))},[e,t,u]),n.useEffect(()=>{if(!o.current||l.current===c)return;l.current=c;const s=h.current;s&&u("Updating the provider",()=>{o.current?.destroy(),o.current=v({container:s,params:e,provider:l.current,listeners:r,onReady:i,enableSafeSdkBridge:t}),R.current=t})},[i,c,u]),n.useEffect(()=>{if(!o.current||p.current===r)return;const s=o.current;u("Updating the listeners",()=>s.updateListeners(r))},[r,u]),a?j.jsx(C,{error:a}):j.jsx("div",{ref:h,id:O.WIDGET_CONTAINER_ID,style:{width:"100%",flex:"1 0 auto"}})}function D(e,c){const r=c.hooks??{},i=Object.keys(r),f=e.hooks??{},t=Object.keys(f);return i.some(a=>{const d=a;return r[d]!==f[d]})||t.some(a=>{const d=a;return r[d]!==f[d]})}function v({container:e,params:c,provider:r,listeners:i,onReady:f,onLoadingError:t,enableSafeSdkBridge:a}){return O.createCowSwapWidget(e,{params:c,provider:r,listeners:i,onReady:f,enableSafeSdkBridge:a,onLoadingError:t})}function x({refs:e,tryOrHandleError:c}){n.useEffect(()=>()=>{e.paramsRef.current=null,e.providerRef.current=void 0,e.listenersRef.current=void 0,e.enableSafeSdkBridgeRef.current=!0;const r=e.widgetHandlerRef.current;r&&(c("Destroy widget",()=>r.destroy()),e.widgetHandlerRef.current=null)},[])}function C({error:e}){return j.jsxs("div",{style:{color:"#ff3a3a"},children:[e.message,e.error.message&&j.jsx("pre",{style:{whiteSpace:"pre-wrap",fontSize:"0.75em"},children:e.error.message})]})}exports.CowSwapWidget=m;Object.keys(O).forEach(e=>{e!=="default"&&!Object.prototype.hasOwnProperty.call(exports,e)&&Object.defineProperty(exports,e,{enumerable:!0,get:()=>O[e]})});
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const O=require("@cowprotocol/widget-lib"),j=require("react/jsx-runtime"),t=require("react");function D({params:e,provider:u,listeners:r,onReady:f,onLoadingError:c,enableSafeSdkBridge:n=!0}){const[l,a]=t.useState(null),y=t.useRef(null),d=t.useRef(u),R=t.useRef(r),p=t.useRef(n),h=t.useRef(null),o=t.useRef(null),W=t.useRef(c);t.useEffect(()=>{W.current=c},[c]);const s=t.useCallback((i,E)=>{try{console.log(`[WIDGET] ${i}`),E()}catch(g){const w=`Error ${i.toLowerCase()}`,m=g instanceof Error?g:new Error("Unknown CowSwapWidget error",{cause:g});console.error(`[WIDGET] ${w}`,m),a({message:w,error:m}),W.current?.()}},[]);return C({refs:{paramsRef:y,providerRef:d,listenersRef:R,enableSafeSdkBridgeRef:p,widgetHandlerRef:o},tryOrHandleError:s}),t.useEffect(()=>{const i=!!y.current&&x(y.current,e),E=p.current!==n;if(!h.current||!i&&!E&&JSON.stringify(y.current)===JSON.stringify(e))return;const g=h.current,w=o.current;y.current=e,p.current=n,w===null?s("Creating a new widget",()=>{o.current=v({container:g,params:e,provider:d.current,listeners:r,onReady:f,onLoadingError:c,enableSafeSdkBridge:n}),R.current=r}):E?s("Recreating the widget",()=>{w.destroy(),o.current=v({container:g,params:e,provider:d.current,listeners:r,onReady:f,onLoadingError:c,enableSafeSdkBridge:n}),R.current=r}):s("Updating the widget",()=>w.updateParams(e))},[e,n,s]),t.useEffect(()=>{if(!o.current||d.current===u)return;d.current=u;const i=h.current;i&&s("Updating the provider",()=>{o.current?.destroy(),o.current=v({container:i,params:e,provider:d.current,listeners:r,onReady:f,enableSafeSdkBridge:n}),p.current=n})},[f,u,s]),t.useEffect(()=>{if(!o.current||R.current===r)return;const i=o.current;s("Updating the listeners",()=>i.updateListeners(r))},[r,s]),l?j.jsx(H,{error:l}):j.jsx("div",{ref:h,id:O.WIDGET_CONTAINER_ID,style:{width:"100%",flex:"1 0 auto"}})}function x(e,u){const r=u.hooks??{},f=Object.keys(r),c=e.hooks??{},n=Object.keys(c);return f.some(l=>{const a=l;return r[a]!==c[a]})||n.some(l=>{const a=l;return r[a]!==c[a]})}function v({container:e,params:u,provider:r,listeners:f,onReady:c,onLoadingError:n,enableSafeSdkBridge:l}){return O.createCowSwapWidget(e,{params:u,provider:r,listeners:f,onReady:c,enableSafeSdkBridge:l,onLoadingError:n})}function C({refs:e,tryOrHandleError:u}){t.useEffect(()=>()=>{e.paramsRef.current=null,e.providerRef.current=void 0,e.listenersRef.current=void 0,e.enableSafeSdkBridgeRef.current=!0;const r=e.widgetHandlerRef.current;r&&(u("Destroy widget",()=>r.destroy()),e.widgetHandlerRef.current=null)},[])}function H({error:e}){return j.jsxs("div",{style:{color:"#ff3a3a"},children:[e.message,e.error.message&&j.jsx("pre",{style:{whiteSpace:"pre-wrap",fontSize:"0.75em"},children:e.error.message})]})}exports.CowSwapWidget=D;Object.keys(O).forEach(e=>{e!=="default"&&!Object.prototype.hasOwnProperty.call(exports,e)&&Object.defineProperty(exports,e,{enumerable:!0,get:()=>O[e]})});
package/index.js CHANGED
@@ -1,138 +1,142 @@
1
- import { WIDGET_CONTAINER_ID as k, createCowSwapWidget as H } from "@cowprotocol/widget-lib";
1
+ import { WIDGET_CONTAINER_ID as H, createCowSwapWidget as x } from "@cowprotocol/widget-lib";
2
2
  export * from "@cowprotocol/widget-lib";
3
- import { jsx as D, jsxs as x } from "react/jsx-runtime";
4
- import { useState as C, useRef as w, useCallback as O, useEffect as E } from "react";
5
- function _({
3
+ import { jsx as W, jsxs as C } from "react/jsx-runtime";
4
+ import { useState as O, useRef as d, useEffect as y, useCallback as I } from "react";
5
+ function $({
6
6
  params: r,
7
- provider: n,
7
+ provider: c,
8
8
  listeners: e,
9
- onReady: s,
10
- onLoadingError: i,
9
+ onReady: i,
10
+ onLoadingError: n,
11
11
  enableSafeSdkBridge: t = !0
12
12
  }) {
13
- const [f, d] = C(null), p = w(null), l = w(n), y = w(e), h = w(t), m = w(null), o = w(null), c = O((u, R) => {
13
+ const [f, l] = O(null), p = d(null), a = d(c), h = d(e), m = d(t), R = d(null), o = d(null), k = d(n);
14
+ y(() => {
15
+ k.current = n;
16
+ }, [n]);
17
+ const u = I((s, v) => {
14
18
  try {
15
- console.log(`[WIDGET] ${u}`), R();
16
- } catch (a) {
17
- const g = `Error ${u.toLowerCase()}`, W = a instanceof Error ? a : new Error("Unknown CowSwapWidget error", {
18
- cause: a
19
+ console.log(`[WIDGET] ${s}`), v();
20
+ } catch (w) {
21
+ const g = `Error ${s.toLowerCase()}`, E = w instanceof Error ? w : new Error("Unknown CowSwapWidget error", {
22
+ cause: w
19
23
  });
20
- console.error(`[WIDGET] ${g}`, W), d({
24
+ console.error(`[WIDGET] ${g}`, E), l({
21
25
  message: g,
22
- error: W
23
- });
26
+ error: E
27
+ }), k.current?.();
24
28
  }
25
29
  }, []);
26
- return U({
30
+ return j({
27
31
  refs: {
28
32
  paramsRef: p,
29
- providerRef: l,
30
- listenersRef: y,
31
- enableSafeSdkBridgeRef: h,
33
+ providerRef: a,
34
+ listenersRef: h,
35
+ enableSafeSdkBridgeRef: m,
32
36
  widgetHandlerRef: o
33
37
  },
34
- tryOrHandleError: c
35
- }), E(() => {
36
- const u = !!p.current && I(p.current, r), R = h.current !== t;
37
- if (!m.current || !u && !R && JSON.stringify(p.current) === JSON.stringify(r))
38
+ tryOrHandleError: u
39
+ }), y(() => {
40
+ const s = !!p.current && U(p.current, r), v = m.current !== t;
41
+ if (!R.current || !s && !v && JSON.stringify(p.current) === JSON.stringify(r))
38
42
  return;
39
- const a = m.current, g = o.current;
40
- p.current = r, h.current = t, g === null ? c("Creating a new widget", () => {
41
- o.current = v({
42
- container: a,
43
+ const w = R.current, g = o.current;
44
+ p.current = r, m.current = t, g === null ? u("Creating a new widget", () => {
45
+ o.current = D({
46
+ container: w,
43
47
  params: r,
44
- provider: l.current,
48
+ provider: a.current,
45
49
  listeners: e,
46
- onReady: s,
47
- onLoadingError: i,
50
+ onReady: i,
51
+ onLoadingError: n,
48
52
  enableSafeSdkBridge: t
49
- }), y.current = e;
50
- }) : R ? c("Recreating the widget", () => {
51
- g.destroy(), o.current = v({
52
- container: a,
53
+ }), h.current = e;
54
+ }) : v ? u("Recreating the widget", () => {
55
+ g.destroy(), o.current = D({
56
+ container: w,
53
57
  params: r,
54
- provider: l.current,
58
+ provider: a.current,
55
59
  listeners: e,
56
- onReady: s,
57
- onLoadingError: i,
60
+ onReady: i,
61
+ onLoadingError: n,
58
62
  enableSafeSdkBridge: t
59
- }), y.current = e;
60
- }) : c("Updating the widget", () => g.updateParams(r));
61
- }, [r, t, c]), E(() => {
62
- if (!o.current || l.current === n)
63
+ }), h.current = e;
64
+ }) : u("Updating the widget", () => g.updateParams(r));
65
+ }, [r, t, u]), y(() => {
66
+ if (!o.current || a.current === c)
63
67
  return;
64
- l.current = n;
65
- const u = m.current;
66
- u && c("Updating the provider", () => {
67
- o.current?.destroy(), o.current = v({
68
- container: u,
68
+ a.current = c;
69
+ const s = R.current;
70
+ s && u("Updating the provider", () => {
71
+ o.current?.destroy(), o.current = D({
72
+ container: s,
69
73
  params: r,
70
- provider: l.current,
74
+ provider: a.current,
71
75
  listeners: e,
72
- onReady: s,
76
+ onReady: i,
73
77
  enableSafeSdkBridge: t
74
- }), h.current = t;
78
+ }), m.current = t;
75
79
  });
76
- }, [s, n, c]), E(() => {
77
- if (!o.current || y.current === e) return;
78
- const u = o.current;
79
- c("Updating the listeners", () => u.updateListeners(e));
80
- }, [e, c]), f ? /* @__PURE__ */ D(j, { error: f }) : /* @__PURE__ */ D("div", { ref: m, id: k, style: {
80
+ }, [i, c, u]), y(() => {
81
+ if (!o.current || h.current === e) return;
82
+ const s = o.current;
83
+ u("Updating the listeners", () => s.updateListeners(e));
84
+ }, [e, u]), f ? /* @__PURE__ */ W(N, { error: f }) : /* @__PURE__ */ W("div", { ref: R, id: H, style: {
81
85
  width: "100%",
82
86
  flex: "1 0 auto"
83
87
  } });
84
88
  }
85
- function I(r, n) {
86
- const e = n.hooks ?? {}, s = Object.keys(e), i = r.hooks ?? {}, t = Object.keys(i);
87
- return s.some((f) => {
88
- const d = f;
89
- return e[d] !== i[d];
89
+ function U(r, c) {
90
+ const e = c.hooks ?? {}, i = Object.keys(e), n = r.hooks ?? {}, t = Object.keys(n);
91
+ return i.some((f) => {
92
+ const l = f;
93
+ return e[l] !== n[l];
90
94
  }) || t.some((f) => {
91
- const d = f;
92
- return e[d] !== i[d];
95
+ const l = f;
96
+ return e[l] !== n[l];
93
97
  });
94
98
  }
95
- function v({
99
+ function D({
96
100
  container: r,
97
- params: n,
101
+ params: c,
98
102
  provider: e,
99
- listeners: s,
100
- onReady: i,
103
+ listeners: i,
104
+ onReady: n,
101
105
  onLoadingError: t,
102
106
  enableSafeSdkBridge: f
103
107
  }) {
104
- return H(r, {
105
- params: n,
108
+ return x(r, {
109
+ params: c,
106
110
  provider: e,
107
- listeners: s,
108
- onReady: i,
111
+ listeners: i,
112
+ onReady: n,
109
113
  enableSafeSdkBridge: f,
110
114
  onLoadingError: t
111
115
  });
112
116
  }
113
- function U({
117
+ function j({
114
118
  refs: r,
115
- tryOrHandleError: n
119
+ tryOrHandleError: c
116
120
  }) {
117
- E(() => () => {
121
+ y(() => () => {
118
122
  r.paramsRef.current = null, r.providerRef.current = void 0, r.listenersRef.current = void 0, r.enableSafeSdkBridgeRef.current = !0;
119
123
  const e = r.widgetHandlerRef.current;
120
- e && (n("Destroy widget", () => e.destroy()), r.widgetHandlerRef.current = null);
124
+ e && (c("Destroy widget", () => e.destroy()), r.widgetHandlerRef.current = null);
121
125
  }, []);
122
126
  }
123
- function j({
127
+ function N({
124
128
  error: r
125
129
  }) {
126
- return /* @__PURE__ */ x("div", { style: {
130
+ return /* @__PURE__ */ C("div", { style: {
127
131
  color: "#ff3a3a"
128
132
  }, children: [
129
133
  r.message,
130
- r.error.message && /* @__PURE__ */ D("pre", { style: {
134
+ r.error.message && /* @__PURE__ */ W("pre", { style: {
131
135
  whiteSpace: "pre-wrap",
132
136
  fontSize: "0.75em"
133
137
  }, children: r.error.message })
134
138
  ] });
135
139
  }
136
140
  export {
137
- _ as CowSwapWidget
141
+ $ as CowSwapWidget
138
142
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cowprotocol/widget-react",
3
- "version": "3.2.11",
3
+ "version": "3.3.0",
4
4
  "description": "CoW Swap Widget Library. Allows you to easily embed a CoW Swap widget on your React application.",
5
5
  "repository": {
6
6
  "type": "git",
@@ -15,9 +15,9 @@
15
15
  "access": "public"
16
16
  },
17
17
  "dependencies": {
18
- "@cowprotocol/events": "^4.16.0",
19
- "@cowprotocol/types": "^4.13.0",
20
- "@cowprotocol/widget-lib": "^4.10.0"
18
+ "@cowprotocol/events": "^4.17.0",
19
+ "@cowprotocol/types": "^4.14.0",
20
+ "@cowprotocol/widget-lib": "^4.11.0"
21
21
  },
22
22
  "devDependencies": {
23
23
  "@testing-library/dom": "10.4.1",