@machinemetrics/mm-react-embeddable 0.10.2 → 0.11.0-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.
@@ -1,6 +1,6 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const t=require("react"),y=require("prop-types"),x=require("styled-components"),R=t.createContext(null),P=({children:r})=>{const[a,c]=t.useState({}),s=t.useRef({}),l=t.useCallback((e,o,n)=>{c(i=>({...i,[e]:{origin:o,frameRef:n}}))},[]),f=t.useCallback(e=>{c(o=>{const n={...o};return delete n[e],n})},[]);t.useEffect(()=>{const e=(n=[],i)=>{n.forEach(u=>u(i))},o=n=>{var M,E,w,b,p;if(!((M=n==null?void 0:n.data)!=null&&M.instanceId))return;const{instanceId:i,type:u}=n.data,g=s.current;e((E=g[i])==null?void 0:E[u],n.data),e((w=g["*"])==null?void 0:w[u],n.data),e((b=g[i])==null?void 0:b["*"],n.data),e((p=g["*"])==null?void 0:p["*"],n.data)};return window.addEventListener("message",o),()=>{window.removeEventListener("message",o)}},[a]);const d=t.useCallback((e,o,n)=>{const i=a[e];if(!i){console.warn(`Origin not set for id: ${e}. Unable to send message.`);return}i.frameRef.current.contentWindow.postMessage({type:o,...n,instanceId:e},i.origin)},[a]),m=t.useCallback((e,o)=>{d(e,"load-options",o)},[d]),I=t.useCallback((e,o)=>{d(e,"validate",o)},[d]),C={registerFrame:l,unregisterFrame:f,sendMessage:d,loadOptions:m,validate:I,addListener:(e,o,n)=>(s.current[e]||(s.current[e]={}),s.current[e][o]||(s.current[e][o]=[]),s.current[e][o].push(n),()=>{s.current[e][o]=s.current[e][o].filter(i=>i!==n)})};return t.createElement(R.Provider,{value:C},r)},h={},$=async r=>{try{const a=new URL(r).origin;if(h[a]!==void 0)return h[a];const s=await(await fetch(`${a}/mm-app-manifest.json`)).json();if(!s.version)throw new Error("Invalid manifest");return h[a]=s,s}catch{}return h[origin]=!1,!1},L=async r=>!!await $(r),S=(r,a,c)=>{const s=t.useContext(R);if(s===null)throw new Error("useEmbeddedEffect must be used within a MMEmbeddableZoneProvider");const l=t.useRef(c);return t.useEffect(()=>{l.current=c},[c]),t.useEffect(()=>{if(!(!r||!a))return s.addListener(r,a,(...f)=>l.current(...f))},[s,r,a]),t.useCallback((f,d)=>s.sendMessage(r,f,d),[s,r])};P.propTypes={children:y.node.isRequired};const v=x.default||x,F=v.iframe`
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const t=require("react"),M=require("prop-types"),S=require("styled-components"),R=t.createContext(null),F=({children:r})=>{const[o,u]=t.useState({}),i=t.useRef({}),l=t.useCallback((e,s,n)=>{u(a=>({...a,[e]:{origin:s,frameRef:n}}))},[]),d=t.useCallback(e=>{u(s=>{const n={...s};return delete n[e],n})},[]);t.useEffect(()=>{const e=(n=[],a)=>{n.forEach(c=>c(a))},s=n=>{var h,E,y,g,p;if(!((h=n==null?void 0:n.data)!=null&&h.instanceId))return;const{instanceId:a,type:c}=n.data,f=i.current;e((E=f[a])==null?void 0:E[c],n.data),e((y=f["*"])==null?void 0:y[c],n.data),e((g=f[a])==null?void 0:g["*"],n.data),e((p=f["*"])==null?void 0:p["*"],n.data)};return window.addEventListener("message",s),()=>{window.removeEventListener("message",s)}},[o]);const b=t.useCallback((e,s,n)=>{var c,f;const a=o[e];if(!a){console.warn(`Origin not set for id: ${e}. Unable to send message.`);return}(f=(c=a==null?void 0:a.frameRef)==null?void 0:c.current)!=null&&f.contentWindow&&a.frameRef.current.contentWindow.postMessage({type:s,...n,instanceId:e},a.origin)},[o]),m=t.useCallback((e,s)=>{b(e,"load-options",s)},[b]),C=t.useCallback((e,s)=>{b(e,"validate",s)},[b]),I={registerFrame:l,unregisterFrame:d,sendMessage:b,loadOptions:m,validate:C,addListener:(e,s,n)=>(i.current[e]||(i.current[e]={}),i.current[e][s]||(i.current[e][s]=[]),i.current[e][s].push(n),()=>{i.current[e][s]=i.current[e][s].filter(a=>a!==n)})};return t.createElement(R.Provider,{value:I},r)},w={},Z=async r=>{try{const o=new URL(r).origin;if(w[o]!==void 0)return w[o];const i=await(await fetch(`${o}/mm-app-manifest.json`)).json();if(!i.version)throw new Error("Invalid manifest");return w[o]=i,i}catch{}return w[origin]=!1,!1},v=async r=>!!await Z(r),x=(r,o,u)=>{const i=t.useContext(R);if(i===null)throw new Error("useEmbeddedEffect must be used within a MMEmbeddableZoneProvider");const l=t.useRef(u);t.useEffect(()=>{l.current=u},[u]),t.useEffect(()=>{if(!(!r||!o))return i.addListener(r,o,(...d)=>l.current(...d))},[i,r,o])};F.propTypes={children:M.node.isRequired};const $=S.default||S,L=$.iframe`
2
2
  width: 100%;
3
3
  height: 100%;
4
4
  border: none;
5
5
  visibility: ${r=>r.$isReady?"visible":"hidden"};
6
- `,Z=({id:r,src:a,path:c,onInit:s=()=>{},context:l,isConfiguring:f,...d})=>{const m=t.useRef(),[I,C]=t.useState(),[e,o]=t.useState(0),{registerFrame:n,unregisterFrame:i,sendMessage:u}=t.useContext(R),[g,M]=t.useState(!0),[E,w]=t.useState();return t.useEffect(()=>{e&&u(r,"set-context",{context:l})},[l,r,e]),t.useEffect(()=>{c!==void 0&&w(c)},[c]),t.useEffect(()=>{E===void 0||!e||u(r,"navigate",{path:E})},[E,e,r,u]),t.useEffect(()=>{e&&u(r,"set-configuring",{isConfiguring:f})},[e,f]),t.useEffect(()=>{if(!(!a||!r||!m))return(async()=>{const b=await L(a);if(M(b),b){const p=new URL(a);n(r,p.origin,m),s(),C(`${p.origin}/mm-embedded-interop?id=${r}&origin=${window.location.origin}`);return}})(),()=>{i(r)}},[a,r,n,m]),S(r,"ready",()=>o(b=>b+1)),g?t.createElement(F,{$isReady:e,ref:m,src:I,...d}):t.createElement("div",{style:{height:"100%",display:"flex",alignItems:"center",justifyContent:"center"}},t.createElement("div",{style:{textAlign:"center"}},"Application must adhere to the MM App Manifest specification to be embedded."))};Z.propTypes={id:y.string,src:y.string.isRequired,path:y.string,onInit:y.func};exports.MMEmbeddableZone=Z;exports.MMEmbeddableZoneContext=R;exports.MMEmbeddableZoneProvider=P;exports.getAppInfo=$;exports.testIsEmbeddable=L;exports.useEmbeddedEffect=S;
6
+ `,P=({id:r,src:o,path:u,onInit:i=()=>{},context:l,params:d,...b})=>{if("isConfiguring"in b)return console.error("isConfiguring must be passed via the params prop to MMEmbeddableZone"),null;if("colorMode"in l)return console.error("colorMode must be passed via the params prop to MMEmbeddableZone"),null;if("isFullScreen"in l)return console.error("isFullScreen must be passed via the params prop to MMEmbeddableZone"),null;const m=t.useRef(),[C,I]=t.useState(),[e,s]=t.useState(0),{registerFrame:n,unregisterFrame:a,sendMessage:c}=t.useContext(R),[f,h]=t.useState(!0),[E,y]=t.useState();return t.useEffect(()=>{e&&c(r,"set-context",{context:{...l,colorMode:d.colorMode,isFullScreen:d.isFullScreen}})},[l,d.colorMode,d.isFullScreen,r,e]),t.useEffect(()=>{e&&(c(r,"set-params",{params:d}),c(r,"set-configuring",{isConfiguring:d.isConfiguring}))},[d,r,e]),t.useEffect(()=>{u!==void 0&&y(u)},[u]),t.useEffect(()=>{E===void 0||!e||c(r,"navigate",{path:E})},[E,e,r,c]),t.useEffect(()=>{if(!(!o||!r||!m))return(async()=>{const g=await v(o);if(h(g),g){const p=new URL(o);n(r,p.origin,m),i(),I(`${p.origin}/mm-embedded-interop?id=${r}&origin=${window.location.origin}`);return}})(),()=>{a(r)}},[o,r,n,m]),x(r,"ready",()=>s(g=>g+1)),f?t.createElement(L,{$isReady:e,ref:m,src:C,...b}):t.createElement("div",{style:{height:"100%",display:"flex",alignItems:"center",justifyContent:"center"}},t.createElement("div",{style:{textAlign:"center"}},"Application must adhere to the MM App Manifest specification to be embedded."))};P.propTypes={id:M.string,src:M.string.isRequired,path:M.string,onInit:M.func};exports.MMEmbeddableZone=P;exports.MMEmbeddableZoneContext=R;exports.MMEmbeddableZoneProvider=F;exports.getAppInfo=Z;exports.testIsEmbeddable=v;exports.useEmbeddedEffect=x;
@@ -1,140 +1,157 @@
1
- import x, { createContext as C, useState as y, useRef as F, useCallback as E, useEffect as f, useContext as A } from "react";
2
- import h from "prop-types";
3
- import v from "styled-components";
4
- const P = C(null), U = ({ children: t }) => {
5
- const [o, a] = y({}), n = F({}), l = E((e, s, r) => {
6
- a((i) => ({ ...i, [e]: { origin: s, frameRef: r } }));
7
- }, []), u = E((e) => {
8
- a((s) => {
9
- const r = { ...s };
10
- return delete r[e], r;
1
+ import I, { createContext as P, useState as h, useRef as v, useCallback as E, useEffect as m, useContext as Z } from "react";
2
+ import w from "prop-types";
3
+ import L from "styled-components";
4
+ const x = P(null), S = ({ children: r }) => {
5
+ const [s, c] = h({}), o = v({}), u = E((e, n, t) => {
6
+ c((i) => ({ ...i, [e]: { origin: n, frameRef: t } }));
7
+ }, []), d = E((e) => {
8
+ c((n) => {
9
+ const t = { ...n };
10
+ return delete t[e], t;
11
11
  });
12
12
  }, []);
13
- f(() => {
14
- const e = (r = [], i) => {
15
- r.forEach((d) => d(i));
16
- }, s = (r) => {
17
- var M, b, R, m, w;
18
- if (!((M = r == null ? void 0 : r.data) != null && M.instanceId))
13
+ m(() => {
14
+ const e = (t = [], i) => {
15
+ t.forEach((a) => a(i));
16
+ }, n = (t) => {
17
+ var y, p, R, g, M;
18
+ if (!((y = t == null ? void 0 : t.data) != null && y.instanceId))
19
19
  return;
20
- const { instanceId: i, type: d } = r.data, p = n.current;
21
- e((b = p[i]) == null ? void 0 : b[d], r.data), e((R = p["*"]) == null ? void 0 : R[d], r.data), e((m = p[i]) == null ? void 0 : m["*"], r.data), e((w = p["*"]) == null ? void 0 : w["*"], r.data);
20
+ const { instanceId: i, type: a } = t.data, l = o.current;
21
+ e((p = l[i]) == null ? void 0 : p[a], t.data), e((R = l["*"]) == null ? void 0 : R[a], t.data), e((g = l[i]) == null ? void 0 : g["*"], t.data), e((M = l["*"]) == null ? void 0 : M["*"], t.data);
22
22
  };
23
- return window.addEventListener("message", s), () => {
24
- window.removeEventListener("message", s);
23
+ return window.addEventListener("message", n), () => {
24
+ window.removeEventListener("message", n);
25
25
  };
26
- }, [o]);
27
- const c = E(
28
- (e, s, r) => {
29
- const i = o[e];
26
+ }, [s]);
27
+ const f = E(
28
+ (e, n, t) => {
29
+ var a, l;
30
+ const i = s[e];
30
31
  if (!i) {
31
32
  console.warn(`Origin not set for id: ${e}. Unable to send message.`);
32
33
  return;
33
34
  }
34
- i.frameRef.current.contentWindow.postMessage(
35
- { type: s, ...r, instanceId: e },
35
+ (l = (a = i == null ? void 0 : i.frameRef) == null ? void 0 : a.current) != null && l.contentWindow && i.frameRef.current.contentWindow.postMessage(
36
+ { type: n, ...t, instanceId: e },
36
37
  i.origin
37
38
  );
38
39
  },
39
- [o]
40
- ), g = E(
41
- (e, s) => {
42
- c(e, "load-options", s);
40
+ [s]
41
+ ), b = E(
42
+ (e, n) => {
43
+ f(e, "load-options", n);
43
44
  },
44
- [c]
45
- ), $ = E(
46
- (e, s) => {
47
- c(e, "validate", s);
45
+ [f]
46
+ ), C = E(
47
+ (e, n) => {
48
+ f(e, "validate", n);
48
49
  },
49
- [c]
50
- ), L = {
51
- registerFrame: l,
52
- unregisterFrame: u,
53
- sendMessage: c,
54
- loadOptions: g,
55
- validate: $,
56
- addListener: (e, s, r) => (n.current[e] || (n.current[e] = {}), n.current[e][s] || (n.current[e][s] = []), n.current[e][s].push(r), () => {
57
- n.current[e][s] = n.current[e][s].filter((i) => i !== r);
50
+ [f]
51
+ ), $ = {
52
+ registerFrame: u,
53
+ unregisterFrame: d,
54
+ sendMessage: f,
55
+ loadOptions: b,
56
+ validate: C,
57
+ addListener: (e, n, t) => (o.current[e] || (o.current[e] = {}), o.current[e][n] || (o.current[e][n] = []), o.current[e][n].push(t), () => {
58
+ o.current[e][n] = o.current[e][n].filter((i) => i !== t);
58
59
  })
59
60
  };
60
- return /* @__PURE__ */ x.createElement(P.Provider, { value: L }, t);
61
- }, I = {}, Z = async (t) => {
61
+ return /* @__PURE__ */ I.createElement(x.Provider, { value: $ }, r);
62
+ }, F = {}, A = async (r) => {
62
63
  try {
63
- const o = new URL(t).origin;
64
- if (I[o] !== void 0)
65
- return I[o];
66
- const n = await (await fetch(`${o}/mm-app-manifest.json`)).json();
67
- if (!n.version)
64
+ const s = new URL(r).origin;
65
+ if (F[s] !== void 0)
66
+ return F[s];
67
+ const o = await (await fetch(`${s}/mm-app-manifest.json`)).json();
68
+ if (!o.version)
68
69
  throw new Error("Invalid manifest");
69
- return I[o] = n, n;
70
+ return F[s] = o, o;
70
71
  } catch {
71
72
  }
72
- return I[origin] = !1, !1;
73
- }, j = async (t) => !!await Z(t), O = (t, o, a) => {
74
- const n = A(P);
75
- if (n === null)
73
+ return F[origin] = !1, !1;
74
+ }, U = async (r) => !!await A(r), j = (r, s, c) => {
75
+ const o = Z(x);
76
+ if (o === null)
76
77
  throw new Error(
77
78
  "useEmbeddedEffect must be used within a MMEmbeddableZoneProvider"
78
79
  );
79
- const l = F(a);
80
- return f(() => {
81
- l.current = a;
82
- }, [a]), f(() => {
83
- if (!(!t || !o))
84
- return n.addListener(
85
- t,
86
- o,
87
- (...u) => l.current(...u)
80
+ const u = v(c);
81
+ m(() => {
82
+ u.current = c;
83
+ }, [c]), m(() => {
84
+ if (!(!r || !s))
85
+ return o.addListener(
86
+ r,
87
+ s,
88
+ (...d) => u.current(...d)
88
89
  );
89
- }, [n, t, o]), E(
90
- (u, c) => n.sendMessage(t, u, c),
91
- [n, t]
92
- );
90
+ }, [o, r, s]);
93
91
  };
94
- U.propTypes = {
95
- children: h.node.isRequired
92
+ S.propTypes = {
93
+ children: w.node.isRequired
96
94
  };
97
- const T = v.default || v, q = T.iframe`
95
+ const O = L.default || L, T = O.iframe`
98
96
  width: 100%;
99
97
  height: 100%;
100
98
  border: none;
101
- visibility: ${(t) => t.$isReady ? "visible" : "hidden"};
102
- `, S = ({
103
- id: t,
104
- src: o,
105
- path: a,
106
- onInit: n = () => {
99
+ visibility: ${(r) => r.$isReady ? "visible" : "hidden"};
100
+ `, q = ({
101
+ id: r,
102
+ src: s,
103
+ path: c,
104
+ onInit: o = () => {
107
105
  },
108
- context: l,
109
- isConfiguring: u,
110
- ...c
106
+ context: u,
107
+ params: d,
108
+ ...f
111
109
  }) => {
112
- const g = F(), [$, L] = y(), [e, s] = y(0), { registerFrame: r, unregisterFrame: i, sendMessage: d } = A(
113
- P
114
- ), [p, M] = y(!0), [b, R] = y();
115
- return f(() => {
116
- e && d(t, "set-context", { context: l });
117
- }, [l, t, e]), f(() => {
118
- a !== void 0 && R(a);
119
- }, [a]), f(() => {
120
- b === void 0 || !e || d(t, "navigate", { path: b });
121
- }, [b, e, t, d]), f(() => {
122
- e && d(t, "set-configuring", { isConfiguring: u });
123
- }, [e, u]), f(() => {
124
- if (!(!o || !t || !g))
110
+ if ("isConfiguring" in f)
111
+ return console.error(
112
+ "isConfiguring must be passed via the params prop to MMEmbeddableZone"
113
+ ), null;
114
+ if ("colorMode" in u)
115
+ return console.error(
116
+ "colorMode must be passed via the params prop to MMEmbeddableZone"
117
+ ), null;
118
+ if ("isFullScreen" in u)
119
+ return console.error(
120
+ "isFullScreen must be passed via the params prop to MMEmbeddableZone"
121
+ ), null;
122
+ const b = v(), [C, $] = h(), [e, n] = h(0), { registerFrame: t, unregisterFrame: i, sendMessage: a } = Z(
123
+ x
124
+ ), [l, y] = h(!0), [p, R] = h();
125
+ return m(() => {
126
+ e && a(r, "set-context", {
127
+ context: {
128
+ ...u,
129
+ // TODO: Remove this once everything is migrated to params
130
+ colorMode: d.colorMode,
131
+ isFullScreen: d.isFullScreen
132
+ }
133
+ });
134
+ }, [u, d.colorMode, d.isFullScreen, r, e]), m(() => {
135
+ e && (a(r, "set-params", { params: d }), a(r, "set-configuring", { isConfiguring: d.isConfiguring }));
136
+ }, [d, r, e]), m(() => {
137
+ c !== void 0 && R(c);
138
+ }, [c]), m(() => {
139
+ p === void 0 || !e || a(r, "navigate", { path: p });
140
+ }, [p, e, r, a]), m(() => {
141
+ if (!(!s || !r || !b))
125
142
  return (async () => {
126
- const m = await j(o);
127
- if (M(m), m) {
128
- const w = new URL(o);
129
- r(t, w.origin, g), n(), L(
130
- `${w.origin}/mm-embedded-interop?id=${t}&origin=${window.location.origin}`
143
+ const g = await U(s);
144
+ if (y(g), g) {
145
+ const M = new URL(s);
146
+ t(r, M.origin, b), o(), $(
147
+ `${M.origin}/mm-embedded-interop?id=${r}&origin=${window.location.origin}`
131
148
  );
132
149
  return;
133
150
  }
134
151
  })(), () => {
135
- i(t);
152
+ i(r);
136
153
  };
137
- }, [o, t, r, g]), O(t, "ready", () => s((m) => m + 1)), p ? /* @__PURE__ */ x.createElement(q, { $isReady: e, ref: g, src: $, ...c }) : /* @__PURE__ */ x.createElement(
154
+ }, [s, r, t, b]), j(r, "ready", () => n((g) => g + 1)), l ? /* @__PURE__ */ I.createElement(T, { $isReady: e, ref: b, src: C, ...f }) : /* @__PURE__ */ I.createElement(
138
155
  "div",
139
156
  {
140
157
  style: {
@@ -144,20 +161,20 @@ const T = v.default || v, q = T.iframe`
144
161
  justifyContent: "center"
145
162
  }
146
163
  },
147
- /* @__PURE__ */ x.createElement("div", { style: { textAlign: "center" } }, "Application must adhere to the MM App Manifest specification to be embedded.")
164
+ /* @__PURE__ */ I.createElement("div", { style: { textAlign: "center" } }, "Application must adhere to the MM App Manifest specification to be embedded.")
148
165
  );
149
166
  };
150
- S.propTypes = {
151
- id: h.string,
152
- src: h.string.isRequired,
153
- path: h.string,
154
- onInit: h.func
167
+ q.propTypes = {
168
+ id: w.string,
169
+ src: w.string.isRequired,
170
+ path: w.string,
171
+ onInit: w.func
155
172
  };
156
173
  export {
157
- S as MMEmbeddableZone,
158
- P as MMEmbeddableZoneContext,
159
- U as MMEmbeddableZoneProvider,
160
- Z as getAppInfo,
161
- j as testIsEmbeddable,
162
- O as useEmbeddedEffect
174
+ q as MMEmbeddableZone,
175
+ x as MMEmbeddableZoneContext,
176
+ S as MMEmbeddableZoneProvider,
177
+ A as getAppInfo,
178
+ U as testIsEmbeddable,
179
+ j as useEmbeddedEffect
163
180
  };
@@ -1,6 +1,6 @@
1
- (function(d,e){typeof exports=="object"&&typeof module<"u"?e(exports,require("react"),require("prop-types"),require("styled-components")):typeof define=="function"&&define.amd?define(["exports","react","prop-types","styled-components"],e):(d=typeof globalThis<"u"?globalThis:d||self,e(d["mm-react-embeddable"]={},d.React,d.PropTypes,d.styled))})(this,function(d,e,y,S){"use strict";const M=e.createContext(null),Z=({children:n})=>{const[o,a]=e.useState({}),s=e.useRef({}),m=e.useCallback((t,i,r)=>{a(u=>({...u,[t]:{origin:i,frameRef:r}}))},[]),l=e.useCallback(t=>{a(i=>{const r={...i};return delete r[t],r})},[]);e.useEffect(()=>{const t=(r=[],u)=>{r.forEach(c=>c(u))},i=r=>{var I,p,C,b,h;if(!((I=r==null?void 0:r.data)!=null&&I.instanceId))return;const{instanceId:u,type:c}=r.data,g=s.current;t((p=g[u])==null?void 0:p[c],r.data),t((C=g["*"])==null?void 0:C[c],r.data),t((b=g[u])==null?void 0:b["*"],r.data),t((h=g["*"])==null?void 0:h["*"],r.data)};return window.addEventListener("message",i),()=>{window.removeEventListener("message",i)}},[o]);const f=e.useCallback((t,i,r)=>{const u=o[t];if(!u){console.warn(`Origin not set for id: ${t}. Unable to send message.`);return}u.frameRef.current.contentWindow.postMessage({type:i,...r,instanceId:t},u.origin)},[o]),E=e.useCallback((t,i)=>{f(t,"load-options",i)},[f]),$=e.useCallback((t,i)=>{f(t,"validate",i)},[f]),L={registerFrame:m,unregisterFrame:l,sendMessage:f,loadOptions:E,validate:$,addListener:(t,i,r)=>(s.current[t]||(s.current[t]={}),s.current[t][i]||(s.current[t][i]=[]),s.current[t][i].push(r),()=>{s.current[t][i]=s.current[t][i].filter(u=>u!==r)})};return e.createElement(M.Provider,{value:L},n)},w={},v=async n=>{try{const o=new URL(n).origin;if(w[o]!==void 0)return w[o];const s=await(await fetch(`${o}/mm-app-manifest.json`)).json();if(!s.version)throw new Error("Invalid manifest");return w[o]=s,s}catch{}return w[origin]=!1,!1},x=async n=>!!await v(n),F=(n,o,a)=>{const s=e.useContext(M);if(s===null)throw new Error("useEmbeddedEffect must be used within a MMEmbeddableZoneProvider");const m=e.useRef(a);return e.useEffect(()=>{m.current=a},[a]),e.useEffect(()=>{if(!(!n||!o))return s.addListener(n,o,(...l)=>m.current(...l))},[s,n,o]),e.useCallback((l,f)=>s.sendMessage(n,l,f),[s,n])};Z.propTypes={children:y.node.isRequired};const q=(S.default||S).iframe`
1
+ (function(u,e){typeof exports=="object"&&typeof module<"u"?e(exports,require("react"),require("prop-types"),require("styled-components")):typeof define=="function"&&define.amd?define(["exports","react","prop-types","styled-components"],e):(u=typeof globalThis<"u"?globalThis:u||self,e(u["mm-react-embeddable"]={},u.React,u.PropTypes,u.styled))})(this,function(u,e,M,Z){"use strict";const y=e.createContext(null),v=({children:t})=>{const[o,f]=e.useState({}),i=e.useRef({}),c=e.useCallback((n,s,r)=>{f(d=>({...d,[n]:{origin:s,frameRef:r}}))},[]),l=e.useCallback(n=>{f(s=>{const r={...s};return delete r[n],r})},[]);e.useEffect(()=>{const n=(r=[],d)=>{r.forEach(a=>a(d))},s=r=>{var C,p,I,g,h;if(!((C=r==null?void 0:r.data)!=null&&C.instanceId))return;const{instanceId:d,type:a}=r.data,b=i.current;n((p=b[d])==null?void 0:p[a],r.data),n((I=b["*"])==null?void 0:I[a],r.data),n((g=b[d])==null?void 0:g["*"],r.data),n((h=b["*"])==null?void 0:h["*"],r.data)};return window.addEventListener("message",s),()=>{window.removeEventListener("message",s)}},[o]);const m=e.useCallback((n,s,r)=>{var a,b;const d=o[n];if(!d){console.warn(`Origin not set for id: ${n}. Unable to send message.`);return}(b=(a=d==null?void 0:d.frameRef)==null?void 0:a.current)!=null&&b.contentWindow&&d.frameRef.current.contentWindow.postMessage({type:s,...r,instanceId:n},d.origin)},[o]),E=e.useCallback((n,s)=>{m(n,"load-options",s)},[m]),S=e.useCallback((n,s)=>{m(n,"validate",s)},[m]),F={registerFrame:c,unregisterFrame:l,sendMessage:m,loadOptions:E,validate:S,addListener:(n,s,r)=>(i.current[n]||(i.current[n]={}),i.current[n][s]||(i.current[n][s]=[]),i.current[n][s].push(r),()=>{i.current[n][s]=i.current[n][s].filter(d=>d!==r)})};return e.createElement(y.Provider,{value:F},t)},w={},$=async t=>{try{const o=new URL(t).origin;if(w[o]!==void 0)return w[o];const i=await(await fetch(`${o}/mm-app-manifest.json`)).json();if(!i.version)throw new Error("Invalid manifest");return w[o]=i,i}catch{}return w[origin]=!1,!1},L=async t=>!!await $(t),j=(t,o,f)=>{const i=e.useContext(y);if(i===null)throw new Error("useEmbeddedEffect must be used within a MMEmbeddableZoneProvider");const c=e.useRef(f);e.useEffect(()=>{c.current=f},[f]),e.useEffect(()=>{if(!(!t||!o))return i.addListener(t,o,(...l)=>c.current(...l))},[i,t,o])};v.propTypes={children:M.node.isRequired};const x=(Z.default||Z).iframe`
2
2
  width: 100%;
3
3
  height: 100%;
4
4
  border: none;
5
- visibility: ${n=>n.$isReady?"visible":"hidden"};
6
- `,j=({id:n,src:o,path:a,onInit:s=()=>{},context:m,isConfiguring:l,...f})=>{const E=e.useRef(),[$,L]=e.useState(),[t,i]=e.useState(0),{registerFrame:r,unregisterFrame:u,sendMessage:c}=e.useContext(M),[g,I]=e.useState(!0),[p,C]=e.useState();return e.useEffect(()=>{t&&c(n,"set-context",{context:m})},[m,n,t]),e.useEffect(()=>{a!==void 0&&C(a)},[a]),e.useEffect(()=>{p===void 0||!t||c(n,"navigate",{path:p})},[p,t,n,c]),e.useEffect(()=>{t&&c(n,"set-configuring",{isConfiguring:l})},[t,l]),e.useEffect(()=>{if(!(!o||!n||!E))return(async()=>{const b=await x(o);if(I(b),b){const h=new URL(o);r(n,h.origin,E),s(),L(`${h.origin}/mm-embedded-interop?id=${n}&origin=${window.location.origin}`);return}})(),()=>{u(n)}},[o,n,r,E]),F(n,"ready",()=>i(b=>b+1)),g?e.createElement(q,{$isReady:t,ref:E,src:$,...f}):e.createElement("div",{style:{height:"100%",display:"flex",alignItems:"center",justifyContent:"center"}},e.createElement("div",{style:{textAlign:"center"}},"Application must adhere to the MM App Manifest specification to be embedded."))};j.propTypes={id:y.string,src:y.string.isRequired,path:y.string,onInit:y.func},d.MMEmbeddableZone=j,d.MMEmbeddableZoneContext=M,d.MMEmbeddableZoneProvider=Z,d.getAppInfo=v,d.testIsEmbeddable=x,d.useEmbeddedEffect=F,Object.defineProperty(d,Symbol.toStringTag,{value:"Module"})});
5
+ visibility: ${t=>t.$isReady?"visible":"hidden"};
6
+ `,q=({id:t,src:o,path:f,onInit:i=()=>{},context:c,params:l,...m})=>{if("isConfiguring"in m)return console.error("isConfiguring must be passed via the params prop to MMEmbeddableZone"),null;if("colorMode"in c)return console.error("colorMode must be passed via the params prop to MMEmbeddableZone"),null;if("isFullScreen"in c)return console.error("isFullScreen must be passed via the params prop to MMEmbeddableZone"),null;const E=e.useRef(),[S,F]=e.useState(),[n,s]=e.useState(0),{registerFrame:r,unregisterFrame:d,sendMessage:a}=e.useContext(y),[b,C]=e.useState(!0),[p,I]=e.useState();return e.useEffect(()=>{n&&a(t,"set-context",{context:{...c,colorMode:l.colorMode,isFullScreen:l.isFullScreen}})},[c,l.colorMode,l.isFullScreen,t,n]),e.useEffect(()=>{n&&(a(t,"set-params",{params:l}),a(t,"set-configuring",{isConfiguring:l.isConfiguring}))},[l,t,n]),e.useEffect(()=>{f!==void 0&&I(f)},[f]),e.useEffect(()=>{p===void 0||!n||a(t,"navigate",{path:p})},[p,n,t,a]),e.useEffect(()=>{if(!(!o||!t||!E))return(async()=>{const g=await L(o);if(C(g),g){const h=new URL(o);r(t,h.origin,E),i(),F(`${h.origin}/mm-embedded-interop?id=${t}&origin=${window.location.origin}`);return}})(),()=>{d(t)}},[o,t,r,E]),j(t,"ready",()=>s(g=>g+1)),b?e.createElement(x,{$isReady:n,ref:E,src:S,...m}):e.createElement("div",{style:{height:"100%",display:"flex",alignItems:"center",justifyContent:"center"}},e.createElement("div",{style:{textAlign:"center"}},"Application must adhere to the MM App Manifest specification to be embedded."))};q.propTypes={id:M.string,src:M.string.isRequired,path:M.string,onInit:M.func},u.MMEmbeddableZone=q,u.MMEmbeddableZoneContext=y,u.MMEmbeddableZoneProvider=v,u.getAppInfo=$,u.testIsEmbeddable=L,u.useEmbeddedEffect=j,Object.defineProperty(u,Symbol.toStringTag,{value:"Module"})});
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@machinemetrics/mm-react-embeddable",
3
- "version": "0.10.2",
3
+ "version": "0.11.0-0",
4
4
  "license": "MIT",
5
5
  "module": "dist/mm-react-embeddable.es.js",
6
6
  "main": "dist/mm-react-embeddable.cjs.js",