@open-iframe-resizer/core 1.2.1 → 1.3.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.
package/README.md CHANGED
@@ -8,7 +8,7 @@
8
8
 
9
9
  A modern, lightweight alternative for resizing iframes dynamically. It is shipped under the MIT license, making it usable in commercial projects.
10
10
 
11
- If you found this plugin helpful, please consider starring the repository!
11
+ If you found this project helpful, please consider starring the repository!
12
12
 
13
13
  ## Getting Started
14
14
 
@@ -41,39 +41,10 @@ A React component is also available:
41
41
  npm install @open-iframe-resizer/react
42
42
  ```
43
43
 
44
- ## Notes
44
+ ## Full documentation
45
45
 
46
- ### Performing actions after a resize
46
+ Explore the [full documentation](https://lemick.github.io/open-iframe-resizer/guides/getting-started/#_top)
47
47
 
48
- You can execute a custom function after an iframe has been resized. Also, you can use built-in functions
49
- like `updateParentScrollOnResize` to help keep the iframe within the viewport after resizing:
50
-
51
- ```javascript
52
- import { initialize, updateParentScrollOnResize } from "https://cdn.jsdelivr.net/npm/@open-iframe-resizer/core@latest/dist/index.min.js";
53
-
54
- initialize({ onIframeResize: updateParentScrollOnResize }, "#myIframe");
55
- ```
56
-
57
- ### Resize iframes from a different origin
58
-
59
- - If you have control over the embedded page, you need to load the script on your child page to enable messaging between the two windows (you do not need to call the initialize function in the child;
60
- loading the module is sufficient).
61
-
62
- Here is an example of the [parent page](https://codesandbox.io/p/sandbox/xj24pg) and the [child](https://codesandbox.io/p/sandbox/growing-iframe-msv4hr).
63
-
64
-
65
- - If you have no control over the child iframe domain, and, by chance, the child page loads the legacy *iframe-resizer* script, you can initialize the library with the compatibility mode; it will try
66
- to connect to the child iframe:
67
- ```javascript
68
- initialize({ enableLegacyLibSupport: true }, "#my-iframe");
69
- ```
70
-
71
- ### Comparison with iframe-resizer
72
-
73
- This library is very good, but it has changed its license, so it is no longer usable in closed-source projects for free.
74
- I decided to replicate some parts of the API, as it may facilitate migration to this project.
75
-
76
- Some features from this library are missing, but they could be implemented in future versions.
77
48
 
78
49
  ## Browser support
79
50
 
package/dist/index.d.ts CHANGED
@@ -1,3 +1,10 @@
1
+ export declare type IframeChildInitEventData = {
2
+ type: "iframe-child-init";
3
+ targetElementSelector?: string;
4
+ bodyPadding?: string;
5
+ bodyMargin?: string;
6
+ };
7
+
1
8
  export declare type IframeResizeEvent = MessageEvent<IframeResizeEventData>;
2
9
 
3
10
  export declare type IframeResizeEventData = {
@@ -24,6 +31,13 @@ export declare type InteractionState = {
24
31
  isHovered: boolean;
25
32
  };
26
33
 
34
+ export declare type RegisteredElement = {
35
+ iframe: HTMLIFrameElement;
36
+ settings: Settings;
37
+ interactionState: InteractionState;
38
+ initContext: { isInitialized: boolean; retryAttempts: number; retryTimeoutId?: number };
39
+ };
40
+
27
41
  export declare type ResizeContext = {
28
42
  iframe: HTMLIFrameElement;
29
43
  settings: Settings;
@@ -57,6 +71,28 @@ export declare type Settings = {
57
71
  * Default: `false`
58
72
  */
59
73
  enableLegacyLibSupport: boolean;
74
+ /**
75
+ * By default, the root element observed for resizing is the <html> document.
76
+ * In more complex layouts, the scrolling container may be elsewhere.
77
+ * This setting allows you to customize the root element that should be observed for resize events.
78
+ *
79
+ * Default: `undefined`
80
+ */
81
+ targetElementSelector?: string;
82
+
83
+ /**
84
+ * Customize the padding style of the iframe body.
85
+ *
86
+ * Default: `undefined`
87
+ */
88
+ bodyPadding?: string;
89
+
90
+ /**
91
+ * Customize the margin style of the iframe body.
92
+ *
93
+ * Default: `undefined`
94
+ */
95
+ bodyMargin?: string;
60
96
  /**
61
97
  * Listener that is called after the iframe has been resized.
62
98
  * You can use a predefined handler like `updateParentScrollOnResize` or create your own custom handler.
@@ -69,9 +105,9 @@ export declare type Settings = {
69
105
  /**
70
106
  * Resize handler that scrolls to restore the iframe's position in the viewport as it was before the resize.
71
107
  *
72
- * *Note:* This behavior only triggers if the iframe is currently being hovered by the user,
108
+ * *Note:* This behavior only triggers if the iframe currently has focus,
73
109
  * in order to try to limit the number of scroll as it can affect the user experience.
74
110
  */
75
- export declare const updateParentScrollOnResize: ({ previousRenderState, nextRenderState, interactionState }: ResizeContext) => void;
111
+ export declare const updateParentScrollOnResize: ({ previousRenderState, nextRenderState, iframe }: ResizeContext) => void;
76
112
 
77
113
  export { }
package/dist/index.js CHANGED
@@ -1,18 +1,18 @@
1
- const g = () => typeof window < "u", L = () => window.self !== window.top, m = (e) => e instanceof HTMLIFrameElement, w = (e, t) => {
2
- e.document.readyState === "complete" ? t() : e.addEventListener("load", t);
3
- }, y = (e, t) => {
1
+ const p = () => typeof window < "u", R = () => window.self !== window.top, b = (e) => e instanceof HTMLIFrameElement, M = (e) => {
2
+ window.document.readyState === "complete" ? e() : window.addEventListener("load", e);
3
+ }, T = (e, t) => {
4
4
  t(), e.addEventListener("load", t);
5
- }, p = (e, t) => {
6
- var i, o;
7
- const n = ((i = e.contentWindow) == null ? void 0 : i.document.readyState) === "complete";
8
- return e.src !== "about:blank" && ((o = e.contentWindow) == null ? void 0 : o.location.href) !== "about:blank" && n ? t() : e.addEventListener("load", t);
9
- }, v = () => ({ offsetSize: 0, checkOrigin: !0, enableLegacyLibSupport: !1 }), b = (e) => {
5
+ }, C = (e, t) => {
6
+ var o, r;
7
+ const n = ((o = e.contentWindow) == null ? void 0 : o.document.readyState) === "complete";
8
+ return e.src !== "about:blank" && ((r = e.contentWindow) == null ? void 0 : r.location.href) !== "about:blank" && n ? t() : e.addEventListener("load", t);
9
+ }, k = () => ({ offsetSize: 0, checkOrigin: !0, enableLegacyLibSupport: !1 }), B = (e) => {
10
10
  try {
11
11
  return new URL(e.src).origin === window.location.origin;
12
12
  } catch {
13
13
  return !1;
14
14
  }
15
- }, I = (e) => {
15
+ }, D = (e) => {
16
16
  try {
17
17
  const t = new URL(e.src).origin;
18
18
  if (t !== "about:blank")
@@ -20,150 +20,185 @@ const g = () => typeof window < "u", L = () => window.self !== window.top, m = (
20
20
  } catch {
21
21
  }
22
22
  return null;
23
- }, R = (e) => (Object.keys(e).forEach((t) => e[t] === void 0 && delete e[t]), e), h = (e) => {
24
- const { height: t } = e.documentElement.getBoundingClientRect();
25
- return Math.ceil(t);
26
- };
27
- function z(e) {
28
- y(
29
- e,
30
- () => {
31
- var t;
32
- return (t = e.contentWindow) == null ? void 0 : t.postMessage("[iFrameSizer]ID:0:false:false:32:true:true::auto:::0:false:child:auto:true:::true:::false", "*");
33
- }
34
- );
35
- }
36
- function O(e) {
23
+ }, A = (e) => (Object.keys(e).forEach((t) => e[t] === void 0 && delete e[t]), e), L = (e) => {
24
+ const { height: t, width: n } = e.getBoundingClientRect();
25
+ return { height: Math.ceil(t), width: Math.ceil(n) };
26
+ }, f = (e, t) => e ? t ? e.querySelector(t) : e.documentElement : null, I = (e, t) => {
27
+ e && (t.bodyPadding && (e.body.style.padding = t.bodyPadding), t.bodyMargin && (e.body.style.margin = t.bodyMargin));
28
+ }, m = (e) => e <= 100 ? 100 : e <= 120 ? 1e3 : 1e4, H = () => "[iFrameSizer]ID:0:false:false:32:true:true::auto:::0:false:child:auto:true:::true:::false";
29
+ function P(e) {
37
30
  if (typeof e.data == "string" && e.data.startsWith("[iFrameSizer]")) {
38
- const [t, n] = e.data.split(":"), r = +n;
39
- return r > 0 ? r : null;
31
+ const [t, n] = e.data.split(":"), i = +n;
32
+ return i > 0 ? i : null;
40
33
  }
41
34
  return null;
42
35
  }
43
- const a = g() ? M() : null;
36
+ const v = q();
44
37
  let l = [];
45
- const D = (e, t) => {
46
- if (!g())
38
+ const K = (e, t) => {
39
+ if (!p())
47
40
  return [];
48
- const n = { ...v(), ...R(e ?? {}) }, r = S(t), i = E(n, r);
49
- return r.map((o) => {
50
- const s = { iframe: o, settings: n, interactionState: { isHovered: !1 } }, d = C(s, i);
41
+ const n = { ...k(), ...A(e ?? {}) }, i = W(t), o = x(n, i);
42
+ return i.map((r) => {
43
+ const s = {
44
+ iframe: r,
45
+ settings: n,
46
+ interactionState: { isHovered: !1 },
47
+ initContext: { isInitialized: !1, retryAttempts: 0 }
48
+ }, a = F(s, o);
51
49
  return l.push(s), {
52
50
  unsubscribe: () => {
53
- d(), l = l.filter((u) => u.iframe !== o);
51
+ a(), l = l.filter((d) => d.iframe !== r);
54
52
  }
55
53
  };
56
54
  });
57
55
  };
58
- function S(e) {
59
- return typeof e == "string" ? Array.from(document.querySelectorAll(e)).filter(m) : e ? m(e) ? [e] : [] : Array.from(document.getElementsByTagName("iframe"));
56
+ function W(e) {
57
+ return typeof e == "string" ? Array.from(document.querySelectorAll(e)).filter(b) : e ? b(e) ? [e] : [] : Array.from(document.getElementsByTagName("iframe"));
60
58
  }
61
- function E(e, t) {
59
+ function x(e, t) {
62
60
  if (Array.isArray(e.checkOrigin))
63
61
  return e.checkOrigin;
64
62
  if (!e.checkOrigin)
65
63
  return [];
66
64
  const n = [];
67
- for (const r of t) {
68
- const i = I(r);
69
- i && n.push(i);
65
+ for (const i of t) {
66
+ const o = D(i);
67
+ o && n.push(o);
70
68
  }
71
69
  return n;
72
70
  }
73
- function C(e, t) {
74
- const n = b(e.iframe) ? B(e) : k(e, t), r = H(e);
71
+ function F(e, t) {
72
+ const n = B(e.iframe) ? U(e) : N(e, t), i = $(e);
75
73
  return () => {
76
- n(), r();
74
+ n(), i();
77
75
  };
78
76
  }
79
- function k(e, t) {
80
- const { iframe: n, settings: r } = e, i = (o) => {
81
- var u;
82
- const s = !r.checkOrigin || t.includes(o.origin);
83
- if (!(!(n.contentWindow === o.source) || !s)) {
84
- if (((u = o.data) == null ? void 0 : u.type) === "iframe-resized") {
85
- const { height: c } = o.data;
86
- c && f({ newHeight: c, registeredElement: e });
77
+ function N(e, t) {
78
+ const {
79
+ iframe: n,
80
+ initContext: i,
81
+ settings: { checkOrigin: o, enableLegacyLibSupport: r, targetElementSelector: s, bodyPadding: a, bodyMargin: d }
82
+ } = e, h = (c) => {
83
+ var y;
84
+ const E = !o || t.includes(c.origin);
85
+ if (!(!(n.contentWindow === c.source) || !E)) {
86
+ if (((y = c.data) == null ? void 0 : y.type) === "iframe-resized") {
87
+ const { height: u } = c.data;
88
+ u && g({ newHeight: u, registeredElement: e });
87
89
  return;
88
90
  }
89
- if (r.enableLegacyLibSupport) {
90
- const c = O(o);
91
- c !== null && f({ newHeight: c, registeredElement: e });
91
+ if (r) {
92
+ const u = P(c);
93
+ u !== null && g({ newHeight: u, registeredElement: e });
92
94
  return;
93
95
  }
94
96
  }
95
97
  };
96
- return window.addEventListener("message", i), r.enableLegacyLibSupport && z(n), () => window.removeEventListener("message", i);
98
+ window.addEventListener("message", h);
99
+ const S = r ? H() : { type: "iframe-child-init", targetElementSelector: s, bodyPadding: a, bodyMargin: d }, w = () => {
100
+ T(n, () => {
101
+ var c;
102
+ return (c = n.contentWindow) == null ? void 0 : c.postMessage(S, "*");
103
+ }), i.retryAttempts++, i.retryTimeoutId = window.setTimeout(w, m(i.retryAttempts));
104
+ };
105
+ return w(), () => window.removeEventListener("message", h);
97
106
  }
98
- function B({ iframe: e }) {
99
- const t = () => {
100
- var r;
101
- const n = (r = e.contentDocument) == null ? void 0 : r.body;
102
- n && (a == null || a.observe(n));
107
+ function U(e) {
108
+ const { iframe: t, settings: n } = e, { targetElementSelector: i } = n;
109
+ let o = 0;
110
+ const r = () => {
111
+ const s = f(t.contentDocument, i);
112
+ if (!t.contentDocument || !s)
113
+ return o++, setTimeout(r, m(o));
114
+ I(t.contentDocument, n), v().observe(s);
103
115
  };
104
- return p(e, t), () => {
105
- var n;
106
- (n = e.contentDocument) != null && n.body && (a == null || a.unobserve(e.contentDocument.body)), e.removeEventListener("load", t);
116
+ return C(t, r), () => {
117
+ const s = f(t.contentDocument, i);
118
+ s && v().unobserve(s), t.removeEventListener("load", r);
107
119
  };
108
120
  }
109
- function H({ iframe: e, interactionState: t }) {
121
+ function $({ iframe: e, interactionState: t }) {
110
122
  const n = () => {
111
123
  t.isHovered = !0;
112
- }, r = () => {
124
+ }, i = () => {
113
125
  t.isHovered = !1;
114
126
  };
115
- return e.addEventListener("mouseenter", n), e.addEventListener("mouseleave", r), () => {
116
- e.removeEventListener("mouseenter", n), e.removeEventListener("mouseleave", r);
127
+ return e.addEventListener("mouseenter", n), e.addEventListener("mouseleave", i), () => {
128
+ e.removeEventListener("mouseenter", n), e.removeEventListener("mouseleave", i);
117
129
  };
118
130
  }
119
- function M() {
120
- const e = ({ target: t }) => {
121
- const n = l.find((o) => {
122
- var s;
123
- return ((s = o.iframe.contentDocument) == null ? void 0 : s.body) === t;
124
- });
125
- if (!n)
126
- return;
127
- const { iframe: r } = n;
128
- if (!r.contentDocument)
129
- return;
130
- const i = h(r.contentDocument);
131
- i && f({ newHeight: i, registeredElement: n });
131
+ function q() {
132
+ let e = null;
133
+ return () => {
134
+ if (!e) {
135
+ const t = ({ target: n }) => {
136
+ const i = l.find(({ iframe: d }) => d.contentDocument === n.ownerDocument);
137
+ if (!i)
138
+ return;
139
+ const { iframe: o, settings: r } = i, s = f(o.contentDocument, r.targetElementSelector);
140
+ if (!s)
141
+ return;
142
+ const { height: a } = L(s);
143
+ a && g({ newHeight: a, registeredElement: i });
144
+ };
145
+ e = new ResizeObserver((n) => n.forEach(t));
146
+ }
147
+ return e;
132
148
  };
133
- return new ResizeObserver((t) => t.forEach(e));
134
149
  }
135
- function f({ registeredElement: e, newHeight: t }) {
136
- const { iframe: n, settings: r, interactionState: i } = e, o = n.getBoundingClientRect(), s = t + r.offsetSize;
137
- if (n.style.height = `${s}px`, !r.onIframeResize)
150
+ function g({ registeredElement: e, newHeight: t }) {
151
+ const { iframe: n, settings: i, interactionState: o, initContext: r } = e;
152
+ r.isInitialized || (r.isInitialized = !0, clearTimeout(r.retryTimeoutId));
153
+ const s = n.getBoundingClientRect(), a = t + i.offsetSize;
154
+ if (n.style.height = `${a}px`, !i.onIframeResize)
138
155
  return;
139
156
  const d = {
140
157
  iframe: n,
141
- settings: { ...r },
142
- interactionState: { ...i },
143
- previousRenderState: { rect: o },
158
+ settings: { ...i },
159
+ interactionState: { ...o },
160
+ previousRenderState: { rect: s },
144
161
  nextRenderState: { rect: n.getBoundingClientRect() }
145
162
  };
146
- r.onIframeResize(d);
163
+ i.onIframeResize(d);
147
164
  }
148
- W();
149
- function W() {
150
- !g() || !L() || w(window, () => {
151
- const e = () => {
152
- const n = {
153
- type: "iframe-resized",
154
- width: document.documentElement.scrollWidth,
155
- height: h(document)
156
- };
157
- window.parent.postMessage(n, "*");
158
- };
159
- new ResizeObserver(e).observe(document.body);
165
+ const V = G();
166
+ let z = !1;
167
+ _();
168
+ function _() {
169
+ !p() || !R() || window.addEventListener("message", (e) => {
170
+ var t;
171
+ ((t = e.data) == null ? void 0 : t.type) === "iframe-child-init" && M(() => O(e));
160
172
  });
161
173
  }
162
- const A = ({ previousRenderState: e, nextRenderState: t, interactionState: n }) => {
163
- n.isHovered && window.scrollBy(0, t.rect.bottom - e.rect.bottom);
174
+ function O(e, t = 0) {
175
+ const { targetElementSelector: n, bodyPadding: i, bodyMargin: o } = e.data, r = f(document, n);
176
+ if (z || window.parent !== e.source)
177
+ return;
178
+ if (!r)
179
+ return setTimeout(() => O(e, t + 1), m(t));
180
+ I(document, { bodyMargin: o, bodyPadding: i });
181
+ const s = V();
182
+ s.disconnect(), s.observe(r), z = !0;
183
+ }
184
+ function G() {
185
+ let e = null;
186
+ return () => (e || (e = new ResizeObserver((t) => {
187
+ if (!t[0].target)
188
+ return;
189
+ const { height: n, width: i } = L(t[0].target), o = {
190
+ type: "iframe-resized",
191
+ width: i,
192
+ height: n
193
+ };
194
+ window.parent.postMessage(o, "*");
195
+ })), e);
196
+ }
197
+ const Q = ({ previousRenderState: e, nextRenderState: t, iframe: n }) => {
198
+ document.activeElement === n && window.scrollBy(0, t.rect.bottom - e.rect.bottom);
164
199
  };
165
200
  export {
166
- D as initialize,
167
- W as initializeChildListener,
168
- A as updateParentScrollOnResize
201
+ K as initialize,
202
+ _ as initializeChildListener,
203
+ Q as updateParentScrollOnResize
169
204
  };
@@ -1 +1 @@
1
- (function(a,c){typeof exports=="object"&&typeof module<"u"?c(exports):typeof define=="function"&&define.amd?define(["exports"],c):(a=typeof globalThis<"u"?globalThis:a||self,c(a.iframeResizer={}))})(this,function(a){"use strict";const c=()=>typeof window<"u",p=()=>window.self!==window.top,h=e=>e instanceof HTMLIFrameElement,y=(e,t)=>{e.document.readyState==="complete"?t():e.addEventListener("load",t)},b=(e,t)=>{t(),e.addEventListener("load",t)},v=(e,t)=>{var r,o;const n=((r=e.contentWindow)==null?void 0:r.document.readyState)==="complete";return e.src!=="about:blank"&&((o=e.contentWindow)==null?void 0:o.location.href)!=="about:blank"&&n?t():e.addEventListener("load",t)},z=()=>({offsetSize:0,checkOrigin:!0,enableLegacyLibSupport:!1}),R=e=>{try{return new URL(e.src).origin===window.location.origin}catch{return!1}},S=e=>{try{const t=new URL(e.src).origin;if(t!=="about:blank")return t}catch{}return null},I=e=>(Object.keys(e).forEach(t=>e[t]===void 0&&delete e[t]),e),L=e=>{const{height:t}=e.documentElement.getBoundingClientRect();return Math.ceil(t)};function O(e){b(e,()=>{var t;return(t=e.contentWindow)==null?void 0:t.postMessage("[iFrameSizer]ID:0:false:false:32:true:true::auto:::0:false:child:auto:true:::true:::false","*")})}function E(e){if(typeof e.data=="string"&&e.data.startsWith("[iFrameSizer]")){const[t,n]=e.data.split(":"),i=+n;return i>0?i:null}return null}const d=c()?D():null;let f=[];const C=(e,t)=>{if(!c())return[];const n={...z(),...I(e??{})},i=k(t),r=B(n,i);return i.map(o=>{const s={iframe:o,settings:n,interactionState:{isHovered:!1}},l=H(s,r);return f.push(s),{unsubscribe:()=>{l(),f=f.filter(g=>g.iframe!==o)}}})};function k(e){return typeof e=="string"?Array.from(document.querySelectorAll(e)).filter(h):e?h(e)?[e]:[]:Array.from(document.getElementsByTagName("iframe"))}function B(e,t){if(Array.isArray(e.checkOrigin))return e.checkOrigin;if(!e.checkOrigin)return[];const n=[];for(const i of t){const r=S(i);r&&n.push(r)}return n}function H(e,t){const n=R(e.iframe)?T(e):M(e,t),i=W(e);return()=>{n(),i()}}function M(e,t){const{iframe:n,settings:i}=e,r=o=>{var g;const s=!i.checkOrigin||t.includes(o.origin);if(!(!(n.contentWindow===o.source)||!s)){if(((g=o.data)==null?void 0:g.type)==="iframe-resized"){const{height:u}=o.data;u&&m({newHeight:u,registeredElement:e});return}if(i.enableLegacyLibSupport){const u=E(o);u!==null&&m({newHeight:u,registeredElement:e});return}}};return window.addEventListener("message",r),i.enableLegacyLibSupport&&O(n),()=>window.removeEventListener("message",r)}function T({iframe:e}){const t=()=>{var i;const n=(i=e.contentDocument)==null?void 0:i.body;n&&(d==null||d.observe(n))};return v(e,t),()=>{var n;(n=e.contentDocument)!=null&&n.body&&(d==null||d.unobserve(e.contentDocument.body)),e.removeEventListener("load",t)}}function W({iframe:e,interactionState:t}){const n=()=>{t.isHovered=!0},i=()=>{t.isHovered=!1};return e.addEventListener("mouseenter",n),e.addEventListener("mouseleave",i),()=>{e.removeEventListener("mouseenter",n),e.removeEventListener("mouseleave",i)}}function D(){const e=({target:t})=>{const n=f.find(o=>{var s;return((s=o.iframe.contentDocument)==null?void 0:s.body)===t});if(!n)return;const{iframe:i}=n;if(!i.contentDocument)return;const r=L(i.contentDocument);r&&m({newHeight:r,registeredElement:n})};return new ResizeObserver(t=>t.forEach(e))}function m({registeredElement:e,newHeight:t}){const{iframe:n,settings:i,interactionState:r}=e,o=n.getBoundingClientRect(),s=t+i.offsetSize;if(n.style.height=`${s}px`,!i.onIframeResize)return;const l={iframe:n,settings:{...i},interactionState:{...r},previousRenderState:{rect:o},nextRenderState:{rect:n.getBoundingClientRect()}};i.onIframeResize(l)}w();function w(){!c()||!p()||y(window,()=>{const e=()=>{const n={type:"iframe-resized",width:document.documentElement.scrollWidth,height:L(document)};window.parent.postMessage(n,"*")};new ResizeObserver(e).observe(document.body)})}const P=({previousRenderState:e,nextRenderState:t,interactionState:n})=>{n.isHovered&&window.scrollBy(0,t.rect.bottom-e.rect.bottom)};a.initialize=C,a.initializeChildListener=w,a.updateParentScrollOnResize=P,Object.defineProperty(a,Symbol.toStringTag,{value:"Module"})});
1
+ (function(a,l){typeof exports=="object"&&typeof module<"u"?l(exports):typeof define=="function"&&define.amd?define(["exports"],l):(a=typeof globalThis<"u"?globalThis:a||self,l(a.iframeResizer={}))})(this,function(a){"use strict";const l=()=>typeof window<"u",E=()=>window.self!==window.top,w=e=>e instanceof HTMLIFrameElement,T=e=>{window.document.readyState==="complete"?e():window.addEventListener("load",e)},M=(e,t)=>{t(),e.addEventListener("load",t)},C=(e,t)=>{var o,r;const n=((o=e.contentWindow)==null?void 0:o.document.readyState)==="complete";return e.src!=="about:blank"&&((r=e.contentWindow)==null?void 0:r.location.href)!=="about:blank"&&n?t():e.addEventListener("load",t)},k=()=>({offsetSize:0,checkOrigin:!0,enableLegacyLibSupport:!1}),B=e=>{try{return new URL(e.src).origin===window.location.origin}catch{return!1}},D=e=>{try{const t=new URL(e.src).origin;if(t!=="about:blank")return t}catch{}return null},P=e=>(Object.keys(e).forEach(t=>e[t]===void 0&&delete e[t]),e),b=e=>{const{height:t,width:n}=e.getBoundingClientRect();return{height:Math.ceil(t),width:Math.ceil(n)}},g=(e,t)=>e?t?e.querySelector(t):e.documentElement:null,p=(e,t)=>{e&&(t.bodyPadding&&(e.body.style.padding=t.bodyPadding),t.bodyMargin&&(e.body.style.margin=t.bodyMargin))},h=e=>e<=100?100:e<=120?1e3:1e4,A=()=>"[iFrameSizer]ID:0:false:false:32:true:true::auto:::0:false:child:auto:true:::true:::false";function H(e){if(typeof e.data=="string"&&e.data.startsWith("[iFrameSizer]")){const[t,n]=e.data.split(":"),i=+n;return i>0?i:null}return null}const z=V();let m=[];const W=(e,t)=>{if(!l())return[];const n={...k(),...P(e??{})},i=x(t),o=F(n,i);return i.map(r=>{const s={iframe:r,settings:n,interactionState:{isHovered:!1},initContext:{isInitialized:!1,retryAttempts:0}},c=N(s,o);return m.push(s),{unsubscribe:()=>{c(),m=m.filter(u=>u.iframe!==r)}}})};function x(e){return typeof e=="string"?Array.from(document.querySelectorAll(e)).filter(w):e?w(e)?[e]:[]:Array.from(document.getElementsByTagName("iframe"))}function F(e,t){if(Array.isArray(e.checkOrigin))return e.checkOrigin;if(!e.checkOrigin)return[];const n=[];for(const i of t){const o=D(i);o&&n.push(o)}return n}function N(e,t){const n=B(e.iframe)?$(e):U(e,t),i=q(e);return()=>{n(),i()}}function U(e,t){const{iframe:n,initContext:i,settings:{checkOrigin:o,enableLegacyLibSupport:r,targetElementSelector:s,bodyPadding:c,bodyMargin:u}}=e,I=d=>{var R;const K=!o||t.includes(d.origin);if(!(!(n.contentWindow===d.source)||!K)){if(((R=d.data)==null?void 0:R.type)==="iframe-resized"){const{height:f}=d.data;f&&y({newHeight:f,registeredElement:e});return}if(r){const f=H(d);f!==null&&y({newHeight:f,registeredElement:e});return}}};window.addEventListener("message",I);const J=r?A():{type:"iframe-child-init",targetElementSelector:s,bodyPadding:c,bodyMargin:u},S=()=>{M(n,()=>{var d;return(d=n.contentWindow)==null?void 0:d.postMessage(J,"*")}),i.retryAttempts++,i.retryTimeoutId=window.setTimeout(S,h(i.retryAttempts))};return S(),()=>window.removeEventListener("message",I)}function $(e){const{iframe:t,settings:n}=e,{targetElementSelector:i}=n;let o=0;const r=()=>{const s=g(t.contentDocument,i);if(!t.contentDocument||!s)return o++,setTimeout(r,h(o));p(t.contentDocument,n),z().observe(s)};return C(t,r),()=>{const s=g(t.contentDocument,i);s&&z().unobserve(s),t.removeEventListener("load",r)}}function q({iframe:e,interactionState:t}){const n=()=>{t.isHovered=!0},i=()=>{t.isHovered=!1};return e.addEventListener("mouseenter",n),e.addEventListener("mouseleave",i),()=>{e.removeEventListener("mouseenter",n),e.removeEventListener("mouseleave",i)}}function V(){let e=null;return()=>{if(!e){const t=({target:n})=>{const i=m.find(({iframe:u})=>u.contentDocument===n.ownerDocument);if(!i)return;const{iframe:o,settings:r}=i,s=g(o.contentDocument,r.targetElementSelector);if(!s)return;const{height:c}=b(s);c&&y({newHeight:c,registeredElement:i})};e=new ResizeObserver(n=>n.forEach(t))}return e}}function y({registeredElement:e,newHeight:t}){const{iframe:n,settings:i,interactionState:o,initContext:r}=e;r.isInitialized||(r.isInitialized=!0,clearTimeout(r.retryTimeoutId));const s=n.getBoundingClientRect(),c=t+i.offsetSize;if(n.style.height=`${c}px`,!i.onIframeResize)return;const u={iframe:n,settings:{...i},interactionState:{...o},previousRenderState:{rect:s},nextRenderState:{rect:n.getBoundingClientRect()}};i.onIframeResize(u)}const _=j();let v=!1;L();function L(){!l()||!E()||window.addEventListener("message",e=>{var t;((t=e.data)==null?void 0:t.type)==="iframe-child-init"&&T(()=>O(e))})}function O(e,t=0){const{targetElementSelector:n,bodyPadding:i,bodyMargin:o}=e.data,r=g(document,n);if(v||window.parent!==e.source)return;if(!r)return setTimeout(()=>O(e,t+1),h(t));p(document,{bodyMargin:o,bodyPadding:i});const s=_();s.disconnect(),s.observe(r),v=!0}function j(){let e=null;return()=>(e||(e=new ResizeObserver(t=>{if(!t[0].target)return;const{height:n,width:i}=b(t[0].target),o={type:"iframe-resized",width:i,height:n};window.parent.postMessage(o,"*")})),e)}const G=({previousRenderState:e,nextRenderState:t,iframe:n})=>{document.activeElement===n&&window.scrollBy(0,t.rect.bottom-e.rect.bottom)};a.initialize=W,a.initializeChildListener=L,a.updateParentScrollOnResize=G,Object.defineProperty(a,Symbol.toStringTag,{value:"Module"})});
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@open-iframe-resizer/core",
3
3
  "private": false,
4
- "version": "1.2.1",
4
+ "version": "1.3.1",
5
5
  "description": "Open-source modern iframe resizer",
6
6
  "license": "MIT",
7
7
  "repository": {
@@ -21,14 +21,14 @@
21
21
  "responsive"
22
22
  ],
23
23
  "type": "module",
24
- "main": "./dist/index.cjs",
24
+ "main": "./dist/index.umd.cjs",
25
25
  "module": "./dist/index.js",
26
26
  "typings": "./dist/index.d.ts",
27
27
  "exports": {
28
28
  ".": {
29
29
  "types": "./dist/index.d.ts",
30
30
  "import": "./dist/index.js",
31
- "require": "./dist/index.cjs"
31
+ "require": "./dist/index.umd.cjs"
32
32
  }
33
33
  },
34
34
  "files": [
@@ -46,8 +46,5 @@
46
46
  "test:e2e": "npx playwright install --with-deps && npx playwright test",
47
47
  "test:unit": "vitest run",
48
48
  "prepare": "copyfiles -V ../../README.md packages/core"
49
- },
50
- "devDependencies": {
51
- "@microsoft/tsdoc": "^0.15.0"
52
49
  }
53
50
  }