forked from gabrielbull/react-desktop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayground.js
103 lines (90 loc) · 2.06 KB
/
playground.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
import * as React from 'react';
import * as PropTypes from 'prop-types';
import examples from 'examples-loader!examples';
import Sidebar from './ui/sidebar/sidebar';
const styles = {
container: {
width: '100%',
height: '100%',
display: 'flex'
},
sidebar: {
width: '200px'
},
example: {
display: 'flex',
justifyContent: 'center',
alignItems: 'center',
width: '800px',
height: '100%',
margin: '0 auto'
}
};
class Playground extends React.Component {
static childContextTypes = {
playground: PropTypes.object
};
constructor() {
super();
this.state = {
color: '#cc7f29',
theme: 'light',
example: null
};
this.loadState(false);
}
loadState(mounted = true) {
try {
const state = JSON.parse(localStorage['playground']);
if (mounted) this.setState(state);
else this.state = { ...this.state, ...state };
} catch (err) {}
}
persistState() {
localStorage['playground'] = JSON.stringify(this.state);
}
getChildContext() {
return {
playground: this
};
}
componentDidUpdate() {
this.persistState();
}
showExample = (example) => {
this.setState({ example: example });
};
changeColor = (color) => {
this.setState({ color: color });
this.persistState();
};
changeTheme = (theme) => {
this.setState({ theme: theme });
this.persistState();
};
render() {
let example;
if (this.state.example) {
const Example = examples['/examples/' + this.state.example];
if (Example) {
example = <Example color={this.state.color} theme={this.state.theme}/>;
}
}
return (
<div style={styles.container}>
<Sidebar
onColorChange={this.changeColor}
color={this.state.color}
theme={this.state.theme}
onThemeChange={this.changeTheme}
style={styles.sidebar}
defaultExample={this.state.example}
/>
<div style={styles.example}>
{example}
</div>
</div>
);
}
}
export default Playground;