-
Notifications
You must be signed in to change notification settings - Fork 250
/
Copy pathcontainer.js
97 lines (79 loc) · 2.26 KB
/
container.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/* @flow */
/* eslint react/react-in-jsx-scope: off */
import { destroyElement, toCSS } from "@krakenjs/belter/src";
import { type RenderOptionsType } from "../../parent/parent";
import { EVENT } from "../../constants";
const CLASS = {
VISIBLE: "zoid-visible",
INVISIBLE: "zoid-invisible",
};
export function defaultContainerTemplate<P>({
uid,
frame,
prerenderFrame,
doc,
props,
event,
dimensions,
}: RenderOptionsType<P>): ?HTMLElement {
const { width, height } = dimensions;
if (__ZOID__.__DEFAULT_CONTAINER__) {
if (!frame || !prerenderFrame) {
return;
}
const div = doc.createElement("div");
div.setAttribute("id", uid);
const style = doc.createElement("style");
if (props.cspNonce) {
style.setAttribute("nonce", props.cspNonce);
}
style.appendChild(
doc.createTextNode(`
#${uid} {
display: inline-block;
position: relative;
width: ${width};
height: ${height};
}
#${uid} > iframe {
display: inline-block;
position: absolute;
width: 100%;
height: 100%;
top: 0;
left: 0;
transition: opacity .2s ease-in-out;
}
#${uid} > iframe.${CLASS.INVISIBLE} {
opacity: 0;
}
#${uid} > iframe.${CLASS.VISIBLE} {
opacity: 1;
}
`)
);
div.appendChild(frame);
div.appendChild(prerenderFrame);
div.appendChild(style);
prerenderFrame.classList.add(CLASS.VISIBLE);
frame.classList.add(CLASS.INVISIBLE);
event.on(EVENT.RENDERED, () => {
prerenderFrame.classList.remove(CLASS.VISIBLE);
prerenderFrame.classList.add(CLASS.INVISIBLE);
frame.classList.remove(CLASS.INVISIBLE);
frame.classList.add(CLASS.VISIBLE);
setTimeout(() => {
destroyElement(prerenderFrame);
}, 1);
});
event.on(EVENT.RESIZE, ({ width: newWidth, height: newHeight }) => {
if (typeof newWidth === "number") {
div.style.width = toCSS(newWidth);
}
if (typeof newHeight === "number") {
div.style.height = toCSS(newHeight);
}
});
return div;
}
}