-
-
Notifications
You must be signed in to change notification settings - Fork 9.1k
/
html.js
50 lines (38 loc) · 1.04 KB
/
html.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
import { document, Node } from 'global';
import styles from './styles';
const INNER_ID = 'sb-addon-centered-inner';
const WRAPPER_ID = 'sb-addon-centered-wrapper';
function getOrCreate(id, style) {
const elementOnDom = document.getElementById(id);
if (elementOnDom) {
return elementOnDom;
}
const element = document.createElement('div');
element.setAttribute('id', id);
Object.assign(element.style, style);
return element;
}
function getInnerDiv() {
return getOrCreate(INNER_ID, styles.innerStyle);
}
function getWrapperDiv() {
return getOrCreate(WRAPPER_ID, styles.style);
}
export default function(storyFn) {
const inner = getInnerDiv();
const wrapper = getWrapperDiv();
wrapper.appendChild(inner);
const element = storyFn();
if (typeof element === 'string') {
inner.innerHTML = element;
} else if (element instanceof Node) {
inner.innerHTML = '';
inner.appendChild(element);
} else {
return element;
}
return wrapper;
}
if (module && module.hot && module.hot.decline) {
module.hot.decline();
}