Skip to content

Commit 361e27c

Browse files
authored
Merge pull request #141 from criticalbh/JSX-view-99
add non-jsx view to interactive code samples
2 parents 8acec80 + 8a0eda3 commit 361e27c

File tree

1 file changed

+41
-11
lines changed

1 file changed

+41
-11
lines changed

src/components/CodeEditor/CodeEditor.js

Lines changed: 41 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,23 @@ import ReactDOM from 'react-dom';
1111
import Remarkable from 'remarkable';
1212
// TODO: switch back to the upstream version of react-live
1313
// once https://github.com/FormidableLabs/react-live/issues/37 is fixed.
14-
import {LiveProvider, LiveEditor} from '@gaearon/react-live';
14+
import {LiveEditor, LiveProvider} from '@gaearon/react-live';
1515
import {colors, media} from 'theme';
1616
import MetaTitle from 'templates/components/MetaTitle';
1717

18-
const compile = code =>
19-
Babel.transform(code, {presets: ['es2015', 'react']}).code; // eslint-disable-line no-undef
18+
const compileES5 = (
19+
code, // eslint-disable-next-line no-undef
20+
) => Babel.transform(code, {presets: ['es2015', 'react']}).code;
21+
22+
// eslint-disable-next-line no-undef
23+
const compileES6 = code => Babel.transform(code, {presets: ['react']}).code;
2024

2125
class CodeEditor extends Component {
2226
constructor(props, context) {
2327
super(props, context);
2428

2529
this.state = this._updateState(props.code);
30+
this.state.showJSX = true;
2631
}
2732

2833
componentDidMount() {
@@ -38,11 +43,11 @@ class CodeEditor extends Component {
3843
}
3944

4045
render() {
41-
const {children, code} = this.props;
42-
const {error} = this.state;
46+
const {children} = this.props;
47+
const {compiledES6, code, error, showJSX} = this.state;
4348

4449
return (
45-
<LiveProvider code={code} mountStylesheet={false}>
50+
<LiveProvider code={showJSX ? code : compiledES6} mountStylesheet={false}>
4651
<div
4752
css={{
4853
[media.greaterThan('xlarge')]: {
@@ -112,7 +117,23 @@ class CodeEditor extends Component {
112117
background: colors.darker,
113118
color: colors.white,
114119
}}>
115-
<MetaTitle onDark={true}>Live JSX Editor</MetaTitle>
120+
<MetaTitle onDark={true}>
121+
Live JSX Editor
122+
<label
123+
css={{
124+
fontSize: 14,
125+
float: 'right',
126+
cursor: 'pointer',
127+
}}>
128+
<input
129+
checked={this.state.showJSX}
130+
onChange={event =>
131+
this.setState({showJSX: event.target.checked})}
132+
type="checkbox"
133+
/>{' '}
134+
JSX?
135+
</label>
136+
</MetaTitle>
116137
</div>
117138
<div
118139
css={{
@@ -265,12 +286,21 @@ class CodeEditor extends Component {
265286
this._mountNode = ref;
266287
};
267288

268-
_updateState(code) {
289+
_updateState(code, showJSX = true) {
269290
try {
270-
return {
271-
compiled: compile(code),
291+
let newState = {
292+
compiled: compileES5(code),
272293
error: null,
273294
};
295+
296+
if (showJSX) {
297+
newState.code = code;
298+
newState.compiledES6 = compileES6(code);
299+
} else {
300+
newState.compiledES6 = code;
301+
}
302+
303+
return newState;
274304
} catch (error) {
275305
console.error(error);
276306

@@ -282,7 +312,7 @@ class CodeEditor extends Component {
282312
}
283313

284314
_onChange = code => {
285-
this.setState(this._updateState(code));
315+
this.setState(state => this._updateState(code, state.showJSX));
286316
};
287317
}
288318

0 commit comments

Comments
 (0)