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