@cowprotocol/widget-react 1.2.1 → 1.3.3

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/README.md CHANGED
@@ -30,7 +30,7 @@ const cowSwapWidgetParams: CowSwapWidgetParams = {
30
30
  Render the component:
31
31
 
32
32
  ```jsx
33
- <CowSwapWidget params={cowSwapWidgetParams} />
33
+ <CowSwapWidget params={cowSwapWidgetParams} onReady={() => console.log('Widget ready')} />
34
34
  ```
35
35
 
36
36
  ## Developers
package/index.cjs CHANGED
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const p=require("@cowprotocol/widget-lib"),h=require("react/jsx-runtime"),t=require("react");function E(u){const{params:n,provider:i,listeners:s}=u,[a,g]=t.useState(null),o=t.useRef(null),r=t.useRef(i),w=t.useRef(s),d=t.useRef(null),c=t.useRef(null),f=t.useCallback((e,y)=>{try{console.log(`[WIDGET] ${e}`),y()}catch(l){const m=`Error ${e.toLowerCase()}`,v=l instanceof Error?l:new Error("Unknown CowSwapWidget error",{cause:l});console.error(`[WIDGET] ${m}`,v),g({message:m,error:v})}},[]);return t.useEffect(()=>()=>{o.current=null,r.current=void 0,w.current=void 0;const e=c.current;e&&(f("💥 Destroy widget",()=>e.destroy()),c.current=null)},[]),t.useEffect(()=>{const e=!!o.current&&S(o.current,n);if(!d.current||JSON.stringify(o.current)===JSON.stringify(n)&&!e)return;const y=d.current,l=c.current;o.current=n,l===null?f("Creating a new widget",()=>{c.current=p.createCowSwapWidget(y,{params:n,provider:r.current,listeners:s}),w.current=s}):f("Updating the widget",()=>l.updateParams(n))},[n,f]),t.useEffect(()=>{if(!c.current||r.current===i)return;r.current=i;const e=d.current;e&&f("Updating the provider",()=>{c.current?.destroy(),c.current=p.createCowSwapWidget(e,{params:n,provider:r.current,listeners:s})})},[i,f]),t.useEffect(()=>{if(!c.current||w.current===s)return;const e=c.current;f("Updating the listeners",()=>e.updateListeners(s))},[s,f]),a?h.jsxs("div",{style:{color:"#ff3a3a"},children:[a.message,a.error.message&&h.jsx("pre",{style:{whiteSpace:"pre-wrap",fontSize:"0.75em"},children:a.error.message})]}):h.jsx("div",{ref:d,style:{width:"100%"}})}function S(u,n){const i=n.hooks??{},s=Object.keys(i),a=u.hooks??{},g=Object.keys(a);return s.some(o=>{const r=o;return i[r]!==a[r]})||g.some(o=>{const r=o;return i[r]!==a[r]})}exports.CowSwapWidget=E;Object.keys(p).forEach(u=>{u!=="default"&&!Object.prototype.hasOwnProperty.call(exports,u)&&Object.defineProperty(exports,u,{enumerable:!0,get:()=>p[u]})});
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]})});
package/index.js CHANGED
@@ -1,79 +1,82 @@
1
- import { createCowSwapWidget as k } from "@cowprotocol/widget-lib";
1
+ import { createCowSwapWidget as v } from "@cowprotocol/widget-lib";
2
2
  export * from "@cowprotocol/widget-lib";
3
- import { jsxs as E, jsx as v } from "react/jsx-runtime";
4
- import { useState as S, useRef as l, useCallback as x, useEffect as d } from "react";
5
- function O(p) {
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) {
6
6
  const {
7
7
  params: t,
8
- provider: c,
9
- listeners: n
10
- } = p, [i, g] = S(null), o = l(null), e = l(c), m = l(n), f = l(null), s = l(null), u = x((r, w) => {
8
+ provider: s,
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) => {
11
12
  try {
12
- console.log(`[WIDGET] ${r}`), w();
13
+ console.log(`[WIDGET] ${r}`), y();
13
14
  } catch (a) {
14
- const y = `Error ${r.toLowerCase()}`, h = a instanceof Error ? a : new Error("Unknown CowSwapWidget error", {
15
+ const h = `Error ${r.toLowerCase()}`, k = a instanceof Error ? a : new Error("Unknown CowSwapWidget error", {
15
16
  cause: a
16
17
  });
17
- console.error(`[WIDGET] ${y}`, h), g({
18
- message: y,
19
- error: h
18
+ console.error(`[WIDGET] ${h}`, k), l({
19
+ message: h,
20
+ error: k
20
21
  });
21
22
  }
22
23
  }, []);
23
- return d(() => () => {
24
- o.current = null, e.current = void 0, m.current = void 0;
25
- const r = s.current;
26
- r && (u("💥 Destroy widget", () => r.destroy()), s.current = null);
27
- }, []), d(() => {
28
- const r = !!o.current && C(o.current, t);
29
- if (!f.current || JSON.stringify(o.current) === JSON.stringify(t) && !r)
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)
30
31
  return;
31
- const w = f.current, a = s.current;
32
- o.current = t, a === null ? u("Creating a new widget", () => {
33
- s.current = k(w, {
32
+ const y = p.current, a = o.current;
33
+ e.current = t, a === null ? c("Creating a new widget", () => {
34
+ o.current = v(y, {
34
35
  params: t,
35
- provider: e.current,
36
- listeners: n
37
- }), m.current = n;
38
- }) : u("Updating the widget", () => a.updateParams(t));
39
- }, [t, u]), d(() => {
40
- if (!s.current || e.current === c)
36
+ provider: d.current,
37
+ listeners: n,
38
+ onReady: i
39
+ }), w.current = n;
40
+ }) : c("Updating the widget", () => a.updateParams(t));
41
+ }, [t, c]), g(() => {
42
+ if (!o.current || d.current === s)
41
43
  return;
42
- e.current = c;
43
- const r = f.current;
44
- r && u("Updating the provider", () => {
45
- s.current?.destroy(), s.current = k(r, {
44
+ d.current = s;
45
+ const r = p.current;
46
+ r && c("Updating the provider", () => {
47
+ o.current?.destroy(), o.current = v(r, {
46
48
  params: t,
47
- provider: e.current,
48
- listeners: n
49
+ provider: d.current,
50
+ listeners: n,
51
+ onReady: i
49
52
  });
50
53
  });
51
- }, [c, u]), d(() => {
52
- if (!s.current || m.current === n) return;
53
- const r = s.current;
54
- u("Updating the listeners", () => r.updateListeners(n));
55
- }, [n, u]), i ? /* @__PURE__ */ E("div", { style: {
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: {
56
59
  color: "#ff3a3a"
57
60
  }, children: [
58
- i.message,
59
- i.error.message && /* @__PURE__ */ v("pre", { style: {
61
+ u.message,
62
+ u.error.message && /* @__PURE__ */ E("pre", { style: {
60
63
  whiteSpace: "pre-wrap",
61
64
  fontSize: "0.75em"
62
- }, children: i.error.message })
63
- ] }) : /* @__PURE__ */ v("div", { ref: f, style: {
65
+ }, children: u.error.message })
66
+ ] }) : /* @__PURE__ */ E("div", { ref: p, style: {
64
67
  width: "100%"
65
68
  } });
66
69
  }
67
- function C(p, t) {
68
- const c = t.hooks ?? {}, n = Object.keys(c), i = p.hooks ?? {}, g = Object.keys(i);
69
- return n.some((o) => {
70
- const e = o;
71
- return c[e] !== i[e];
72
- }) || g.some((o) => {
73
- const e = o;
74
- return c[e] !== i[e];
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];
75
78
  });
76
79
  }
77
80
  export {
78
- O as CowSwapWidget
81
+ W as CowSwapWidget
79
82
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cowprotocol/widget-react",
3
- "version": "1.2.1",
3
+ "version": "1.3.3",
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.1.0",
19
- "@cowprotocol/types": "^4.1.0",
20
- "@cowprotocol/widget-lib": "^2.1.1"
18
+ "@cowprotocol/events": "^4.1.1",
19
+ "@cowprotocol/types": "^4.1.1",
20
+ "@cowprotocol/widget-lib": "^2.2.3"
21
21
  },
22
22
  "devDependencies": {
23
23
  "@types/react": "19.1.3"