-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Preview.js
107 lines (91 loc) · 2.56 KB
/
Preview.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
98
99
100
101
102
103
104
105
106
107
import React, { Component, PropTypes } from 'react';
import ReactDOM from 'react-dom';
import noop from 'lodash/noop';
import { transform } from 'buble';
import PlaygroundError from 'rsg-components/PlaygroundError';
import Wrapper from 'rsg-components/Wrapper';
const compileCode = code => transform(code, {
objectAssign: 'Object.assign',
}).code;
export default class Preview extends Component {
static propTypes = {
code: PropTypes.string.isRequired,
evalInContext: PropTypes.func.isRequired,
};
state = {
error: null,
};
componentDidMount() {
this.executeCode();
}
componentDidUpdate(prevProps) {
if (this.props.code !== prevProps.code) {
this.executeCode();
}
}
executeCode() {
ReactDOM.unmountComponentAtNode(this.mountNode);
this.setState({
error: null,
});
const { code } = this.props;
if (!code) {
return;
}
try {
const compiledCode = compileCode(this.props.code);
// 1. Use setter/with to call our callback function when user write `initialState = {...}`
// 2. Wrap code in JSON.stringify/eval to catch the component and return it
const exampleComponentCode = `
var stateWrapper = Object.assign({}, {
set initialState(value) {
__setInitialState(value)
},
}, window.ReactStyleguidistComponents)
with (stateWrapper) {
return eval(${JSON.stringify(compiledCode)})
}
`;
const exampleComponent = this.props.evalInContext(exampleComponentCode);
// Wrap everything in a React component to leverage the state management of this component
class PreviewComponent extends Component { // eslint-disable-line react/no-multi-comp
constructor() {
super();
this.state = {};
this.setState = this.setState.bind(this);
this.setInitialState = this.setInitialState.bind(this);
}
// Synchronously set initial state, so it will be ready before first render
// Ignore all consequent calls
setInitialState(initialState) {
Object.assign(this.state, initialState);
this.setInitialState = noop;
}
render() {
return exampleComponent(this.state, this.setState, this.setInitialState);
}
}
const wrappedComponent = (
<Wrapper>
<PreviewComponent />
</Wrapper>
);
ReactDOM.render(wrappedComponent, this.mountNode);
}
catch (err) {
ReactDOM.unmountComponentAtNode(this.mountNode);
this.setState({
error: err.toString(),
});
}
}
render() {
const { error } = this.state;
return (
<div>
<div ref={ref => (this.mountNode = ref)}></div>
{error && <PlaygroundError message={error} />}
</div>
);
}
}