UNPKG

@apideck/vault-react

Version:

React hook for the Apideck Vault component.

60 lines (59 loc) 2.13 kB
function C(d, a) { if (d == null) return {}; var l = {}, n = Object.keys(d), o, e; for (e = 0; e < n.length; e++) o = n[e], !(a.indexOf(o) >= 0) && (l[o] = d[o]); return l; } var g = ["onClose", "onReady", "onConnectionChange", "onConnectionDelete"], b = function() { var a = "https://vaultjs.apideck.com", l = function() { var e = document.createElement("iframe"); return e.style.display = "none", e.style.position = "fixed", e.style.top = "0", e.style.left = "0", e.style.width = "100%", e.style.height = "100%", e.style.border = "none", e.style.zIndex = "9999", e.src = a, e; }, n = null; return { open: function(e) { var i = e.onClose, r = e.onReady, c = e.onConnectionChange, s = e.onConnectionDelete, p = C(e, g); n = l(), document.body.appendChild(n); var m = function h(t) { var u; if (n !== null) { if (t.data === "on-ready") { var v; n.style.display = "block", (v = n.contentWindow) == null || v.postMessage(p, a), r == null || r(); } if (t.data === "on-close" && (window.removeEventListener("message", h), i == null || i(), setTimeout(function() { n !== null && (document.body.removeChild(n), n = null); }, 300)), typeof t.data == "object" && (u = t.data) != null && u.data) { var y; switch ((y = t.data) == null ? void 0 : y.type) { case "on-connection-change": c == null || c(t.data.data); break; case "on-connection-delete": s == null || s(t.data.data); break; } } } }; window.addEventListener("message", m); }, close: function() { var e, i; (e = n) == null || (i = e.contentWindow) == null || i.postMessage({ type: "close" }, a), setTimeout(function() { n !== null && (document.body.removeChild(n), n = null); }, 500); } }; }, f = /* @__PURE__ */ b(); const k = () => ({ open: (l) => { f.open(l); }, close: () => { f.close(); } }); export { k as useVault };