Last active
April 21, 2017 07:50
-
-
Save isaklafleur/caa407d3f1ddb20853e77e2d3ebc76c1 to your computer and use it in GitHub Desktop.
Build a JSX Live Compiler as a React Component
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
body { | |
margin: 0; | |
padding: 0; | |
font-family: monospace; | |
} | |
header { | |
display: block; | |
height: 5vh; | |
overflow: auto; | |
background-color: pink; | |
color: red; | |
font-size: 28px; | |
} | |
.container { | |
height: 95vh; | |
display: flex; | |
} | |
.pre { | |
background-color: #f8f8f8; | |
} | |
pre, textarea { | |
width: 50%; | |
font-family: 28px; | |
margin: 0px; | |
padding: 10px; | |
color: #222; | |
} | |
textarea:focus {outline: 0}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from 'react'; | |
import '../App.css'; | |
class App extends React.Component { | |
constructor() { | |
super(); | |
this.state = { | |
input: '/* add your jsx here */', | |
output: '', | |
err: '', | |
}; | |
} | |
update(e) { | |
let code = e.target.value; | |
try { | |
this.setState({ | |
output: window.Babel | |
.transform(code, { presets: ['es2015', 'react'] }) | |
.code, | |
err: '', | |
}); | |
} catch (err) { | |
this.setState({ err: err.message }); | |
} | |
} | |
render() { | |
return ( | |
<div> | |
<header>{this.state.err}</header> | |
<div className="container"> | |
<textarea | |
onChange={this.update.bind(this)} | |
defaultValue={this.state.input} | |
/> | |
<pre> | |
{this.state.output} | |
</pre> | |
</div> | |
</div> | |
); | |
} | |
} | |
export default App; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<title>React App</title> | |
<meta charset="UTF-8"> | |
<meta name="viewport" content="width=device-width, initial-scale=1"> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/babel-standalone/6.24.0/babel.min.js"></script> | |
</head> | |
<body> | |
<div id="root"></div> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment