diff --git a/_css/react.scss b/_css/react.scss
index d130086fc..daa000270 100644
--- a/_css/react.scss
+++ b/_css/react.scss
@@ -580,6 +580,11 @@ div.CodeMirror-linenumber:after {
border: none;
}
+/* hide the cursor. Mostly used when code's in plain JS */
+.CodeMirror-readonly div.CodeMirror-cursor {
+ visibility: hidden;
+}
+
small code,
li code,
p code {
@@ -596,23 +601,28 @@ p code {
@include clearfix;
}
-.playground::before {
+.playground-tab {
border-bottom: none !important;
border-radius: 3px 3px 0 0;
- padding: 3px 7px;
+ padding: 6px 8px;
font-size: 12px;
font-weight: bold;
color: #c2c0bc;
background-color: #f1ede4;
- content: 'Live editor';
+ display: inline-block;
+ cursor: pointer;
}
-.playground::before,
.playgroundCode,
+.playground-tab,
.playgroundPreview {
border: 1px solid rgba(16,16,16,0.1);
}
+.playground-tab-active {
+ color: $darkestColor;
+}
+
.playgroundCode {
border-radius: 0 3px 3px 3px;
float: left;
diff --git a/_js/examples/timer.js b/_js/examples/timer.js
index 685e57e70..00e60f959 100644
--- a/_js/examples/timer.js
+++ b/_js/examples/timer.js
@@ -3,6 +3,7 @@
*/
var TIMER_COMPONENT = "\
+/** @jsx React.DOM */\n\
var Timer = React.createClass({\n\
getInitialState: function() {\n\
return {secondsElapsed: 0};\n\
@@ -17,13 +18,13 @@ var Timer = React.createClass({\n\
clearInterval(this.interval);\n\
},\n\
render: function() {\n\
- return React.DOM.div({},\n\
- 'Seconds Elapsed: ', this.state.secondsElapsed\n\
+ return (\n\
+
+
{editor}
);
@@ -67,7 +72,7 @@ var selfCleaningTimeout = {
var ReactPlayground = React.createClass({
mixins: [selfCleaningTimeout],
- MODES: {XJS: 'XJS', JS: 'JS'}, //keyMirror({XJS: true, JS: true}),
+ MODES: {JSX: 'JSX', JS: 'JS'}, //keyMirror({JSX: true, JS: true}),
propTypes: {
codeText: React.PropTypes.string.isRequired,
@@ -84,15 +89,19 @@ var ReactPlayground = React.createClass({
},
getInitialState: function() {
- return {mode: this.MODES.XJS, code: this.props.codeText};
+ return {
+ mode: this.MODES.JSX,
+ code: this.props.codeText,
+ };
},
- bindState: function(name) {
- return function(value) {
- var newState = {};
- newState[name] = value;
- this.setState(newState);
- }.bind(this);
+ handleCodeChange: function(value) {
+ this.setState({code: value});
+ this.executeCode();
+ },
+
+ handleCodeModeSwitch: function(mode) {
+ this.setState({mode: mode});
},
compileCode: function() {
@@ -100,25 +109,53 @@ var ReactPlayground = React.createClass({
},
render: function() {
- var content;
- if (this.state.mode === this.MODES.XJS) {
- content =
-
;
- } else if (this.state.mode === this.MODES.JS) {
- content =
-
- {this.compileCode()}
-
;
- }
+ var isJS = this.state.mode === this.MODES.JS;
+ var compiledCode = '';
+ try {
+ compiledCode = this.compileCode();
+ } catch (err) {}
+
+ // we're creating both versions, to avoid the flicker when switching from
+ // one view to another when CodeMirror recompiles
+ var jsContent =
+
;
+
+ var jsxContent =
+
;
+
+ var JSXTabClassName =
+ 'playground-tab' + (isJS ? '' : ' playground-tab-active');
+ var JSTabClassName =
+ 'playground-tab' + (isJS ? ' playground-tab-active' : '');
return (
+
+
+ Live JSX Editor
+
+
+ Compiled JS
+
+
- {content}
+ {jsxContent}
+ {jsContent}
@@ -126,12 +163,19 @@ var ReactPlayground = React.createClass({
);
},
+
componentDidMount: function() {
this.executeCode();
},
- componentDidUpdate: function() {
- this.executeCode();
+
+ componentWillUpdate: function(nextProps, nextState) {
+ // execute code only when the state's not being updated by switching tab
+ // this avoids re-displaying the error, which comes after a certain delay
+ if (this.state.mode === nextState.mode) {
+ this.executeCode();
+ };
},
+
executeCode: function() {
var mountNode = this.refs.mount.getDOMNode();
@@ -149,10 +193,10 @@ var ReactPlayground = React.createClass({
} else {
eval(compiledCode);
}
- } catch (e) {
+ } catch (err) {
this.setTimeout(function() {
React.renderComponent(
-
{e.toString()}
,
+
{err.toString()}
,
mountNode
);
}, 500);
diff --git a/_layouts/default.html b/_layouts/default.html
index 15177f386..9cc8bef4c 100644
--- a/_layouts/default.html
+++ b/_layouts/default.html
@@ -16,9 +16,9 @@
-
+