Skip to content

Commit d255c1a

Browse files
committed
sync js files to codepen
1 parent 2f04c4f commit d255c1a

File tree

9 files changed

+130
-13
lines changed

9 files changed

+130
-13
lines changed

.eslintignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,7 @@ node_modules/*
44
content/*
55

66
# Ignore built files
7-
public/*
7+
public/*
8+
9+
# Ignore examples
10+
examples/*

content/docs/hello-world.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ redirect_from:
1111
- "docs/getting-started-zh-CN.html"
1212
---
1313

14-
The easiest way to get started with React is to use [this Hello World example code on CodePen](http://codepen.io/gaearon/pen/ZpvBNJ?editors=0010). You don't need to install anything; you can just open it in another tab and follow along as we go through examples. If you'd rather use a local development environment, check out the [Installation](/docs/installation.html) page.
14+
The easiest way to get started with React is to use <a href="/examples/hello-world" target="_blank">this Hello World example code on CodePen</a>. You don't need to install anything; you can just open it in another tab and follow along as we go through examples. If you'd rather use a local development environment, check out the [Installation](/docs/installation.html) page.
1515

1616
The smallest React example looks like this:
1717

content/docs/introducing-jsx.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ ReactDOM.render(
4646
);
4747
```
4848

49-
[Try it on CodePen.](http://codepen.io/gaearon/pen/PGEjdG?editors=0010)
49+
<a href="/examples/introducing-jsx" target="_blank">Try it on CodePen.</a>
5050

5151
We split JSX over multiple lines for readability. While it isn't required, when doing this, we also recommend wrapping it in parentheses to avoid the pitfalls of [automatic semicolon insertion](http://stackoverflow.com/q/2846283).
5252

examples/hello-world.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
ReactDOM.render(
2+
<h1>Hello, world!</h1>,
3+
document.getElementById('root')
4+
);

examples/index.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<div id="root">
2+
<!-- This div's content will be managed by React. -->
3+
</div>

examples/introducing-jsx.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
function formatName(user) {
2+
return user.firstName + ' ' + user.lastName;
3+
}
4+
5+
const user = {
6+
firstName: 'Harper',
7+
lastName: 'Perez',
8+
};
9+
10+
const element = (
11+
<h1>
12+
Hello, {formatName(user)}!
13+
</h1>
14+
);
15+
16+
ReactDOM.render(
17+
element,
18+
document.getElementById('root')
19+
);

gatsby-node.js

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
const {resolve} = require('path');
1010
const webpack = require('webpack');
11+
const fs = require('fs');
1112

1213
exports.modifyWebpackConfig = ({config, stage}) => {
1314
// See https://github.com/FormidableLabs/react-live/issues/5
@@ -74,11 +75,11 @@ exports.createPages = async ({graphql, boundActionCreators}) => {
7475
// (which gets created by Gatsby during a separate phase).
7576
} else if (
7677
slug.includes('blog/') ||
77-
slug.includes('community/') ||
78-
slug.includes('contributing/') ||
79-
slug.includes('docs/') ||
80-
slug.includes('tutorial/') ||
81-
slug.includes('warnings/')
78+
slug.includes('community/') ||
79+
slug.includes('contributing/') ||
80+
slug.includes('docs/') ||
81+
slug.includes('tutorial/') ||
82+
slug.includes('warnings/')
8283
) {
8384
let template;
8485
if (slug.includes('blog/')) {
@@ -87,8 +88,8 @@ exports.createPages = async ({graphql, boundActionCreators}) => {
8788
template = communityTemplate;
8889
} else if (
8990
slug.includes('contributing/') ||
90-
slug.includes('docs/') ||
91-
slug.includes('warnings/')
91+
slug.includes('docs/') ||
92+
slug.includes('warnings/')
9293
) {
9394
template = docsTemplate;
9495
} else if (slug.includes('tutorial/')) {
@@ -117,8 +118,8 @@ exports.createPages = async ({graphql, boundActionCreators}) => {
117118
redirect.forEach(fromPath => {
118119
if (redirectToSlugMap[fromPath] != null) {
119120
console.error(`Duplicate redirect detected from "${fromPath}" to:\n` +
120-
`* ${redirectToSlugMap[fromPath]}\n` +
121-
`* ${slug}\n`
121+
`* ${redirectToSlugMap[fromPath]}\n` +
122+
`* ${slug}\n`
122123
);
123124
process.exit(1);
124125
}
@@ -161,6 +162,29 @@ exports.createPages = async ({graphql, boundActionCreators}) => {
161162
redirectInBrowser: true,
162163
toPath: newestBlogNode.fields.slug,
163164
});
165+
166+
// Create Codepen example pages
167+
const htmlTemplate = fs.readFileSync('./examples/index.html', 'utf8');
168+
fs.readdirSync('./examples').forEach(file => {
169+
// Only create pages for the JS files
170+
if (file.toLowerCase().split('.').pop() === 'js') {
171+
const slug = file.substring(0, file.length - 3);
172+
const jsTemplate = fs.readFileSync(`./examples/${file}`, 'utf8');
173+
174+
createPage({
175+
path: `/examples/${slug}`,
176+
component: resolve('./src/templates/codepen-example.js'),
177+
context: {
178+
slug,
179+
payload: {
180+
html: htmlTemplate,
181+
js: jsTemplate,
182+
},
183+
},
184+
});
185+
}
186+
});
187+
164188
};
165189

166190
// Parse date information out of blog post filename.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,4 +84,4 @@
8484
"devDependencies": {
8585
"eslint-config-prettier": "^2.6.0"
8686
}
87-
}
87+
}

src/templates/codepen-example.js

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
'use strict';
2+
3+
import React, {Component} from 'react';
4+
import Container from 'components/Container';
5+
import {colors} from 'theme';
6+
// import {version} from '../site-constants';
7+
8+
// Copied over styles from ButtonLink for the submit btn
9+
const primaryStyle = {
10+
backgroundColor: colors.brand,
11+
color: colors.black,
12+
padding: '10px 25px',
13+
whiteSpace: 'nowrap',
14+
transition: 'background-color 0.2s ease-out',
15+
outline: 0,
16+
border: 'none',
17+
cursor: 'pointer',
18+
19+
':hover': {
20+
backgroundColor: colors.white,
21+
},
22+
23+
display: 'inline-block',
24+
fontSize: 16,
25+
};
26+
27+
class CodepenExample extends Component {
28+
componentDidMount() {
29+
this.codepenForm.submit();
30+
}
31+
32+
render() {
33+
const {payload} = this.props.pathContext;
34+
// Set codepen options
35+
payload.js_pre_processor = 'babel';
36+
// Only have the JS editor open (default for all examples)
37+
payload.editors = '0010';
38+
// We can pass @version in the URL for version locking, if desired.
39+
payload.js_external = `https://unpkg.com/react/umd/react.development.js;https://unpkg.com/react-dom/umd/react-dom.development.js`;
40+
41+
return (
42+
<Container>
43+
<h1>Redirecting to Codepen...</h1>
44+
<form
45+
style={{paddingBottom: '50px'}}
46+
ref={form => {
47+
this.codepenForm = form;
48+
}}
49+
action="https://codepen.io/pen/define"
50+
method="POST">
51+
<input type="hidden" name="data" value={JSON.stringify(payload)} />
52+
53+
<input
54+
style={primaryStyle}
55+
type="submit"
56+
value="Not automatically redirecting? Click here."
57+
/>
58+
</form>
59+
</Container>
60+
);
61+
}
62+
}
63+
64+
export default CodepenExample;

0 commit comments

Comments
 (0)