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