Skip to content

Commit 148f499

Browse files
committed
Use ResizeObserver to detect DOM changes and notify resize event to the parent window
1 parent 522f1f3 commit 148f499

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-0
lines changed

src/index.js

+19
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import qs from 'qs';
2+
import ResizeObserver from './lib/ResizeObserver';
23
import controller from './lib/controller';
34
import log from './lib/log';
45
import './styles/vendor.styl';
@@ -63,6 +64,24 @@ import(`./widgets/${widget}`)
6364
}
6465
}, '*');
6566
});
67+
68+
new ResizeObserver(() => {
69+
// Use the postMessage API for inter-frame communication
70+
window.parent.postMessage({
71+
token: params.token,
72+
action: {
73+
type: 'resize',
74+
payload: {
75+
clientHeight: document.body.clientHeight,
76+
clientWidth: document.body.clientWidth,
77+
offsetHeight: document.body.offsetHeight,
78+
offsetWidth: document.body.offsetWidth,
79+
scrollHeight: document.body.scrollHeight,
80+
scrollWidth: document.body.scrollWidth
81+
}
82+
}
83+
}, '*');
84+
}).observe(document.body);
6685
})
6786
.catch(err => {
6887
log.error(err);

src/lib/ResizeObserver.js

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
class ResizeObserver {
2+
callback = null;
3+
observer = null;
4+
5+
constructor(callback) {
6+
if (typeof callback === 'function') {
7+
this.callback = callback;
8+
}
9+
return this;
10+
}
11+
observe(target) {
12+
if (this.observer) {
13+
this.observer.disconnect();
14+
this.observer = null;
15+
}
16+
17+
this.callback && this.callback();
18+
19+
this.observer = new MutationObserver(mutations => {
20+
this.callback && this.callback();
21+
});
22+
23+
this.observer.observe(target, {
24+
attributes: true,
25+
attributeOldValue: false,
26+
characterData: true,
27+
characterDataOldValue: false,
28+
childList: true,
29+
subtree: true
30+
});
31+
}
32+
disconnect() {
33+
if (this.observer) {
34+
this.observer.disconnect();
35+
this.observer = null;
36+
}
37+
}
38+
}
39+
40+
export default ResizeObserver;

0 commit comments

Comments
 (0)