@open-iframe-resizer/core 1.2.0 → 1.3.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.
package/README.md CHANGED
@@ -1,10 +1,14 @@
1
1
  # Open Iframe Resizer
2
2
 
3
+ ![NPM Version](https://img.shields.io/npm/v/%40open-iframe-resizer%2Fcore)
4
+ ![NPM License](https://img.shields.io/npm/l/%40open-iframe-resizer%2Fcore)
5
+ [![](https://data.jsdelivr.com/v1/package/npm/@open-iframe-resizer/core/badge)](https://www.jsdelivr.com/package/npm/@open-iframe-resizer/core)
6
+
3
7
  ## Overview
4
8
 
5
9
  A modern, lightweight alternative for resizing iframes dynamically. It is shipped under the MIT license, making it usable in commercial projects.
6
10
 
7
- If you found this plugin helpful, please consider starring the repository!
11
+ If you found this project helpful, please consider starring the repository!
8
12
 
9
13
  ## Getting Started
10
14
 
@@ -13,7 +17,7 @@ If you found this plugin helpful, please consider starring the repository!
13
17
  ```html
14
18
 
15
19
  <script type="module">
16
- import { initialize } from "https://cdn.jsdelivr.net/npm/@open-iframe-resizer/core@latest/dist/index.js";
20
+ import { initialize } from "https://cdn.jsdelivr.net/npm/@open-iframe-resizer/core@latest/dist/index.min.js";
17
21
 
18
22
  initialize({}, "#my-iframe");
19
23
  </script>
@@ -45,7 +49,7 @@ You can execute a custom function after an iframe has been resized. Also, you ca
45
49
  like `updateParentScrollOnResize` to help keep the iframe within the viewport after resizing:
46
50
 
47
51
  ```javascript
48
- import { initialize, updateParentScrollOnResize } from "https://cdn.jsdelivr.net/npm/@open-iframe-resizer/core@latest/dist/index.js";
52
+ import { initialize, updateParentScrollOnResize } from "https://cdn.jsdelivr.net/npm/@open-iframe-resizer/core@latest/dist/index.min.js";
49
53
 
50
54
  initialize({ onIframeResize: updateParentScrollOnResize }, "#myIframe");
51
55
  ```
package/dist/index.d.ts CHANGED
@@ -1,4 +1,113 @@
1
- export * from './parent';
2
- export * from './child';
3
- export * from './resize-handlers';
4
- export type * from './type';
1
+ export declare type IframeChildInitEventData = {
2
+ type: "iframe-child-init";
3
+ targetElementSelector?: string;
4
+ bodyPadding?: string;
5
+ bodyMargin?: string;
6
+ };
7
+
8
+ export declare type IframeResizeEvent = MessageEvent<IframeResizeEventData>;
9
+
10
+ export declare type IframeResizeEventData = {
11
+ type: "iframe-resized";
12
+ width: number;
13
+ height?: number;
14
+ };
15
+
16
+ export declare const initialize: InitializeFunction;
17
+
18
+ export declare function initializeChildListener(): void;
19
+
20
+ /**
21
+ * Automatically resize the selected iframes when their inner content grows.
22
+ * @param settings The settings for the selected iframes. The default settings properties are picked if empty.
23
+ * @param selector The selector for the iframe(s) or the HTMLIFrameElement to be resized. If empty, all document iframe elements will be selected.
24
+ * @returns A result array, which can be used to clean up the listeners if you often remove iframes from the document.
25
+ */
26
+ export declare type InitializeFunction = (settings?: Partial<Settings>, selector?: string | HTMLIFrameElement) => InitializeResult[];
27
+
28
+ export declare type InitializeResult = { unsubscribe: () => void };
29
+
30
+ export declare type InteractionState = {
31
+ isHovered: boolean;
32
+ };
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
+
41
+ export declare type ResizeContext = {
42
+ iframe: HTMLIFrameElement;
43
+ settings: Settings;
44
+ interactionState: InteractionState;
45
+ previousRenderState: ResizeRenderState;
46
+ nextRenderState: ResizeRenderState;
47
+ };
48
+
49
+ export declare type ResizeRenderState = { rect: DOMRect };
50
+
51
+ export declare type Settings = {
52
+ /**
53
+ * Offset added to the resize size (in pixels).
54
+ *
55
+ * Default: `0`
56
+ */
57
+ offsetSize: number;
58
+ /**
59
+ * Specifies whether to check the origin of incoming messages.
60
+ * Accepts an array of allowed origins or a boolean.
61
+ * If `true`, incoming messages are allowed from the origins of the registered iframes.
62
+ *
63
+ * Default: `true`
64
+ */
65
+ checkOrigin: string[] | boolean;
66
+ /**
67
+ * Allows the library to communicate with a cross-origin child iframe
68
+ * containing the original "iframe-resizer" script.
69
+ * Useful if you do not control the child domain.
70
+ *
71
+ * Default: `false`
72
+ */
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;
96
+ /**
97
+ * Listener that is called after the iframe has been resized.
98
+ * You can use a predefined handler like `updateParentScrollOnResize` or create your own custom handler.
99
+ *
100
+ * Default: `undefined`
101
+ */
102
+ onIframeResize?: (context: ResizeContext) => void;
103
+ };
104
+
105
+ /**
106
+ * Resize handler that scrolls to restore the iframe's position in the viewport as it was before the resize.
107
+ *
108
+ * *Note:* This behavior only triggers if the iframe currently has focus,
109
+ * in order to try to limit the number of scroll as it can affect the user experience.
110
+ */
111
+ export declare const updateParentScrollOnResize: ({ previousRenderState, nextRenderState, iframe }: ResizeContext) => void;
112
+
113
+ export { }
package/dist/index.js CHANGED
@@ -1,18 +1,18 @@
1
- const h = () => window && window.self !== window.top, l = (e) => e instanceof HTMLIFrameElement, L = (e, t) => {
2
- e.document.readyState === "complete" ? t() : e.addEventListener("load", t);
3
- }, b = (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
- }, v = (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
- }, w = () => ({ offsetSize: 0, checkOrigin: !0, enableLegacyLibSupport: !1 }), y = (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
- }, p = (e) => {
15
+ }, D = (e) => {
16
16
  try {
17
17
  const t = new URL(e.src).origin;
18
18
  if (t !== "about:blank")
@@ -20,152 +20,185 @@ const h = () => window && window.self !== window.top, l = (e) => e instanceof HT
20
20
  } catch {
21
21
  }
22
22
  return null;
23
- }, z = (e) => (Object.keys(e).forEach((t) => e[t] === void 0 && delete e[t]), e), g = (e) => {
24
- const { height: t } = e.documentElement.getBoundingClientRect();
25
- return Math.ceil(t);
26
- };
27
- function O(e) {
28
- b(
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 I(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 m = B();
44
- let u = [];
45
- const W = (e, t) => {
46
- const n = { ...w(), ...z(e ?? {}) }, r = R(t), i = S(n, r);
47
- return r.map((o) => {
48
- const s = { iframe: o, settings: n, interactionState: { isHovered: !1 } }, c = E(s, i);
49
- return u.push(s), {
36
+ const v = q();
37
+ let l = [];
38
+ const K = (e, t) => {
39
+ if (!p())
40
+ return [];
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);
49
+ return l.push(s), {
50
50
  unsubscribe: () => {
51
- c(), u = u.filter((d) => d.iframe !== o);
51
+ a(), l = l.filter((d) => d.iframe !== r);
52
52
  }
53
53
  };
54
54
  });
55
55
  };
56
- function R(e) {
57
- return typeof e == "string" ? Array.from(document.querySelectorAll(e)).filter(l) : e ? l(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"));
58
58
  }
59
- function S(e, t) {
59
+ function x(e, t) {
60
60
  if (Array.isArray(e.checkOrigin))
61
61
  return e.checkOrigin;
62
62
  if (!e.checkOrigin)
63
63
  return [];
64
64
  const n = [];
65
- for (const r of t) {
66
- const i = p(r);
67
- i && n.push(i);
65
+ for (const i of t) {
66
+ const o = D(i);
67
+ o && n.push(o);
68
68
  }
69
69
  return n;
70
70
  }
71
- function E(e, t) {
72
- const n = y(e.iframe) ? k(e) : C(e, t), r = H(e);
71
+ function F(e, t) {
72
+ const n = B(e.iframe) ? U(e) : N(e, t), i = $(e);
73
73
  return () => {
74
- n(), r();
74
+ n(), i();
75
75
  };
76
76
  }
77
- function C(e, t) {
78
- const { iframe: n, settings: r } = e, i = (o) => {
79
- var d;
80
- const s = !r.checkOrigin || t.includes(o.origin);
81
- if (!(!(n.contentWindow === o.source) || !s)) {
82
- if (((d = o.data) == null ? void 0 : d.type) === "iframe-resized") {
83
- const { height: a } = o.data;
84
- a && f({ newHeight: a, 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 });
85
89
  return;
86
90
  }
87
- if (r.enableLegacyLibSupport) {
88
- const a = I(o);
89
- a !== null && f({ newHeight: a, registeredElement: e });
91
+ if (r) {
92
+ const u = P(c);
93
+ u !== null && g({ newHeight: u, registeredElement: e });
90
94
  return;
91
95
  }
92
96
  }
93
97
  };
94
- return window.addEventListener("message", i), r.enableLegacyLibSupport && O(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);
95
106
  }
96
- function k(e) {
97
- const { iframe: t } = e, n = () => {
98
- var i;
99
- const r = (i = t.contentDocument) == null ? void 0 : i.body;
100
- if (!r) {
101
- console.error("Unable to observe the iframe content document body");
102
- return;
103
- }
104
- m.observe(r);
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);
105
115
  };
106
- return v(t, n), () => {
107
- var r;
108
- (r = t.contentDocument) != null && r.body && m.unobserve(t.contentDocument.body), t.removeEventListener("load", n);
116
+ return C(t, r), () => {
117
+ const s = f(t.contentDocument, i);
118
+ s && v().unobserve(s), t.removeEventListener("load", r);
109
119
  };
110
120
  }
111
- function H(e) {
112
- const { iframe: t, interactionState: n } = e, r = () => {
113
- n.isHovered = !0;
121
+ function $({ iframe: e, interactionState: t }) {
122
+ const n = () => {
123
+ t.isHovered = !0;
114
124
  }, i = () => {
115
- n.isHovered = !1;
125
+ t.isHovered = !1;
116
126
  };
117
- return t.addEventListener("mouseenter", r), t.addEventListener("mouseleave", i), () => {
118
- t.removeEventListener("mouseenter", r), t.removeEventListener("mouseleave", i);
127
+ return e.addEventListener("mouseenter", n), e.addEventListener("mouseleave", i), () => {
128
+ e.removeEventListener("mouseenter", n), e.removeEventListener("mouseleave", i);
119
129
  };
120
130
  }
121
- function B() {
122
- const e = ({ target: t }) => {
123
- const n = u.find((o) => {
124
- var s;
125
- return ((s = o.iframe.contentDocument) == null ? void 0 : s.body) === t;
126
- });
127
- if (!n)
128
- return;
129
- const { iframe: r } = n;
130
- if (!r.contentDocument)
131
- return;
132
- const i = g(r.contentDocument);
133
- 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;
134
148
  };
135
- return new ResizeObserver((t) => t.forEach(e));
136
149
  }
137
- function f({ registeredElement: e, newHeight: t }) {
138
- const { iframe: n, settings: r, interactionState: i } = e, o = n.getBoundingClientRect(), s = t + r.offsetSize;
139
- 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)
140
155
  return;
141
- const c = {
156
+ const d = {
142
157
  iframe: n,
143
- settings: { ...r },
144
- interactionState: { ...i },
145
- previousRenderState: { rect: o },
158
+ settings: { ...i },
159
+ interactionState: { ...o },
160
+ previousRenderState: { rect: s },
146
161
  nextRenderState: { rect: n.getBoundingClientRect() }
147
162
  };
148
- r.onIframeResize(c);
163
+ i.onIframeResize(d);
149
164
  }
150
- h() && M();
151
- function M() {
152
- L(window, () => {
153
- const e = () => {
154
- const n = {
155
- type: "iframe-resized",
156
- width: document.documentElement.scrollWidth,
157
- height: g(document)
158
- };
159
- window.parent.postMessage(n, "*");
160
- };
161
- 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));
162
172
  });
163
173
  }
164
- const D = ({ previousRenderState: e, nextRenderState: t, interactionState: n }) => {
165
- 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);
166
199
  };
167
200
  export {
168
- W as initialize,
169
- M as initializeChildListener,
170
- D as updateParentScrollOnResize
201
+ K as initialize,
202
+ _ as initializeChildListener,
203
+ Q as updateParentScrollOnResize
171
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=()=>window&&window.self!==window.top,g=e=>e instanceof HTMLIFrameElement,v=(e,t)=>{e.document.readyState==="complete"?t():e.addEventListener("load",t)},w=(e,t)=>{t(),e.addEventListener("load",t)},y=(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)},p=()=>({offsetSize:0,checkOrigin:!0,enableLegacyLibSupport:!1}),z=e=>{try{return new URL(e.src).origin===window.location.origin}catch{return!1}},O=e=>{try{const t=new URL(e.src).origin;if(t!=="about:blank")return t}catch{}return null},S=e=>(Object.keys(e).forEach(t=>e[t]===void 0&&delete e[t]),e),h=e=>{const{height:t}=e.documentElement.getBoundingClientRect();return Math.ceil(t)};function R(e){w(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 I(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 b=W();let u=[];const E=(e,t)=>{const n={...p(),...S(e??{})},i=C(t),r=k(n,i);return i.map(o=>{const s={iframe:o,settings:n,interactionState:{isHovered:!1}},f=H(s,r);return u.push(s),{unsubscribe:()=>{f(),u=u.filter(l=>l.iframe!==o)}}})};function C(e){return typeof e=="string"?Array.from(document.querySelectorAll(e)).filter(g):e?g(e)?[e]:[]:Array.from(document.getElementsByTagName("iframe"))}function k(e,t){if(Array.isArray(e.checkOrigin))return e.checkOrigin;if(!e.checkOrigin)return[];const n=[];for(const i of t){const r=O(i);r&&n.push(r)}return n}function H(e,t){const n=z(e.iframe)?B(e):M(e,t),i=T(e);return()=>{n(),i()}}function M(e,t){const{iframe:n,settings:i}=e,r=o=>{var l;const s=!i.checkOrigin||t.includes(o.origin);if(!(!(n.contentWindow===o.source)||!s)){if(((l=o.data)==null?void 0:l.type)==="iframe-resized"){const{height:d}=o.data;d&&m({newHeight:d,registeredElement:e});return}if(i.enableLegacyLibSupport){const d=I(o);d!==null&&m({newHeight:d,registeredElement:e});return}}};return window.addEventListener("message",r),i.enableLegacyLibSupport&&R(n),()=>window.removeEventListener("message",r)}function B(e){const{iframe:t}=e,n=()=>{var r;const i=(r=t.contentDocument)==null?void 0:r.body;if(!i){console.error("Unable to observe the iframe content document body");return}b.observe(i)};return y(t,n),()=>{var i;(i=t.contentDocument)!=null&&i.body&&b.unobserve(t.contentDocument.body),t.removeEventListener("load",n)}}function T(e){const{iframe:t,interactionState:n}=e,i=()=>{n.isHovered=!0},r=()=>{n.isHovered=!1};return t.addEventListener("mouseenter",i),t.addEventListener("mouseleave",r),()=>{t.removeEventListener("mouseenter",i),t.removeEventListener("mouseleave",r)}}function W(){const e=({target:t})=>{const n=u.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=h(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 f={iframe:n,settings:{...i},interactionState:{...r},previousRenderState:{rect:o},nextRenderState:{rect:n.getBoundingClientRect()}};i.onIframeResize(f)}c()&&L();function L(){v(window,()=>{const e=()=>{const n={type:"iframe-resized",width:document.documentElement.scrollWidth,height:h(document)};window.parent.postMessage(n,"*")};new ResizeObserver(e).observe(document.body)})}const D=({previousRenderState:e,nextRenderState:t,interactionState:n})=>{n.isHovered&&window.scrollBy(0,t.rect.bottom-e.rect.bottom)};a.initialize=E,a.initializeChildListener=L,a.updateParentScrollOnResize=D,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.0",
4
+ "version": "1.3.0",
5
5
  "description": "Open-source modern iframe resizer",
6
6
  "license": "MIT",
7
7
  "repository": {
@@ -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
  }
package/dist/child.d.ts DELETED
@@ -1,2 +0,0 @@
1
- declare function initializeChildListener(): void;
2
- export { initializeChildListener };
package/dist/common.d.ts DELETED
@@ -1,16 +0,0 @@
1
- import { Settings } from './type';
2
- export declare const isInIframe: () => boolean;
3
- export declare const isHtmlIframeElement: (element: Element) => element is HTMLIFrameElement;
4
- export declare const deferWhenWindowIsLoaded: (_window: Window, executable: () => void) => void;
5
- /**
6
- * Post the message twice, it assures the target to receive the message at least once
7
- */
8
- export declare const safePostMessageToCrossOriginIframe: (iframe: HTMLIFrameElement, executable: () => void) => void;
9
- export declare const deferWhenSameOriginIframeIsLoaded: (iframe: HTMLIFrameElement, executable: () => void) => void;
10
- export declare const getDefaultSettings: () => Settings;
11
- export declare const isIframeSameOrigin: (iframe: HTMLIFrameElement) => boolean;
12
- export declare const extractIframeOrigin: (iframe: HTMLIFrameElement) => string | null;
13
- export declare const removeUndefinedProperties: <T extends {
14
- [key: string]: unknown;
15
- }>(object: T) => T;
16
- export declare const getBoundingRectHeight: (document: Document) => number;
package/dist/compat.d.ts DELETED
@@ -1,2 +0,0 @@
1
- export declare function sendLegacyLibInitMessageOnIframeLoad(iframe: HTMLIFrameElement): void;
2
- export declare function handleLegacyLibResizeMessage(event: MessageEvent): number | null;
package/dist/parent.d.ts DELETED
@@ -1,3 +0,0 @@
1
- import { InitializeFunction } from './type';
2
- declare const initialize: InitializeFunction;
3
- export { initialize };
@@ -1,8 +0,0 @@
1
- import { ResizeContext } from './type';
2
- /**
3
- * Resize handler that scrolls to restore the iframe's position in the viewport as it was before the resize.
4
- *
5
- * *Note:* This behavior only triggers if the iframe is currently being hovered by the user,
6
- * in order to try to limit the number of scroll as it can affect the user experience.
7
- */
8
- export declare const updateParentScrollOnResize: ({ previousRenderState, nextRenderState, interactionState }: ResizeContext) => void;
package/dist/type.d.ts DELETED
@@ -1,68 +0,0 @@
1
- interface Window {
2
- iframeResizer: {
3
- initialize: InitializeFunction;
4
- };
5
- }
6
-
7
- /**
8
- * Automatically resizes the selected iframes.
9
- * @param settings The settings for the selected iframes. The default settings properties are picked if empty.
10
- * @param selector The selector for the iframe(s) or the HTMLIFrameElement to be resized. If empty, all document iframe elements will be selected.
11
- * @returns A result array, which can be used to clean up the listeners if you often remove iframes from the document.
12
- */
13
- export type InitializeFunction = (settings?: Partial<Settings>, selector?: string | HTMLIFrameElement) => InitializeResult[];
14
- export type InitializeResult = { unsubscribe: () => void };
15
-
16
- export type Settings = {
17
- /**
18
- * Offset added to the resize size (in pixels).
19
- *
20
- * Default: `0`
21
- */
22
- offsetSize: number;
23
- /**
24
- * Specifies whether to check the origin of incoming messages.
25
- * Accepts an array of allowed origins or a boolean.
26
- * If `true`, incoming messages are allowed from the origins of the registered iframes.
27
- *
28
- * Default: `true`
29
- */
30
- checkOrigin: string[] | boolean;
31
- /**
32
- * Allows the library to communicate with a cross-origin child iframe
33
- * containing the original "iframe-resizer" script.
34
- * Useful if you do not control the child domain.
35
- *
36
- * Default: `false`
37
- */
38
- enableLegacyLibSupport: boolean;
39
- /**
40
- * Listener that is called after the iframe has been resized.
41
- * You can use a predefined handler like `updateParentScrollOnResize` or create your own custom handler.
42
- *
43
- * Default: `undefined`
44
- */
45
- onIframeResize?: (context: ResizeContext) => void;
46
- };
47
-
48
- export type IframeResizeEventData = {
49
- type: "iframe-resized";
50
- width: number;
51
- height?: number;
52
- };
53
-
54
- export type IframeResizeEvent = MessageEvent<IframeResizeEventData>;
55
-
56
- export type InteractionState = {
57
- isHovered: boolean;
58
- };
59
-
60
- export type ResizeRenderState = { rect: DOMRect };
61
-
62
- export type ResizeContext = {
63
- iframe: HTMLIFrameElement;
64
- settings: Settings;
65
- interactionState: InteractionState;
66
- previousRenderState: ResizeRenderState;
67
- nextRenderState: ResizeRenderState;
68
- };