Skip to content

Commit 3ead525

Browse files
committed
feat: refactor index page so hot reloading works for code examples
1 parent f4c3087 commit 3ead525

4 files changed

Lines changed: 82 additions & 108 deletions

File tree

src/components/CodeEditor/CodeEditor.js

Lines changed: 2 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -65,47 +65,8 @@ class CodeEditor extends Component {
6565
}
6666

6767
return (
68+
<div css={{ flex: 1 }}>
6869
<LiveProvider code={showJSX ? code : compiledES6} mountStylesheet={false}>
69-
<div
70-
css={{
71-
[media.greaterThan('xlarge')]: {
72-
display: 'flex',
73-
flexDirection: 'row',
74-
},
75-
76-
[media.lessThan('large')]: {
77-
display: 'block',
78-
},
79-
}}>
80-
{children && (
81-
<div
82-
css={{
83-
flex: '0 0 33%',
84-
85-
[media.lessThan('xlarge')]: {
86-
marginBottom: 20,
87-
},
88-
89-
'& h3': {
90-
color: colors.dark,
91-
maxWidth: '11em',
92-
paddingTop: 0,
93-
},
94-
95-
'& p': {
96-
marginTop: 15,
97-
marginRight: 40,
98-
lineHeight: 1.7,
99-
100-
[media.greaterThan('xlarge')]: {
101-
marginTop: 25,
102-
},
103-
},
104-
}}>
105-
{children}
106-
</div>
107-
)}
108-
10970
<div
11071
css={{
11172
[media.greaterThan('medium')]: {
@@ -273,8 +234,8 @@ class CodeEditor extends Component {
273234
</div>
274235
)}
275236
</div>
276-
</div>
277237
</LiveProvider>
238+
</div>
278239
);
279240
}
280241

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import React, { Component } from 'react';
2+
import {colors, media} from 'theme';
3+
import CodeEditor from '../CodeEditor/CodeEditor';
4+
5+
class CodeExample extends Component {
6+
render() {
7+
const {children, code, loaded} = this.props;
8+
return (
9+
<div
10+
css={{
11+
[media.greaterThan('xlarge')]: {
12+
display: 'flex',
13+
flexDirection: 'row',
14+
},
15+
16+
[media.lessThan('large')]: {
17+
display: 'block',
18+
},
19+
}}>
20+
{children && (
21+
<div
22+
css={{
23+
flex: '0 0 33%',
24+
25+
[media.lessThan('xlarge')]: {
26+
marginBottom: 20,
27+
},
28+
29+
'& h3': {
30+
color: colors.dark,
31+
maxWidth: '11em',
32+
paddingTop: 0,
33+
},
34+
35+
'& p': {
36+
marginTop: 15,
37+
marginRight: 40,
38+
lineHeight: 1.7,
39+
40+
[media.greaterThan('xlarge')]: {
41+
marginTop: 25,
42+
},
43+
},
44+
}}>
45+
{children}
46+
</div>
47+
)}
48+
{loaded ? <CodeEditor code={code} /> : <h4>Loading code example...</h4>}
49+
</div>
50+
)
51+
}
52+
}
53+
54+
export default CodeExample;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import CodeExample from './CodeExample';
2+
3+
export default CodeExample;

src/pages/index.js

Lines changed: 23 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -7,72 +7,37 @@
77
import ButtonLink from 'components/ButtonLink';
88
import Container from 'components/Container';
99
import Flex from 'components/Flex';
10-
import mountCodeExample from 'utils/mountCodeExample';
10+
import CodeExample from 'components/CodeExample';
1111
import PropTypes from 'prop-types';
1212
import React, {Component} from 'react';
1313
import {graphql} from 'gatsby';
1414
import TitleAndMetaTags from 'components/TitleAndMetaTags';
1515
import Layout from 'components/Layout';
16+
import loadScript from 'utils/loadScript';
1617
import {colors, media, sharedStyles} from 'theme';
1718
import createOgUrl from 'utils/createOgUrl';
18-
import loadScript from 'utils/loadScript';
1919
import {babelURL} from 'site-constants';
2020
import ReactDOM from 'react-dom';
2121
import logoWhiteSvg from 'icons/logo-white.svg';
2222

2323
class Home extends Component {
24-
constructor(props, context) {
25-
super(props, context);
26-
27-
const {data} = props;
28-
29-
const code = data.code.edges.reduce((map, {node}) => {
30-
map[node.id] = JSON.parse(node.internal.contentDigest);
31-
32-
return map;
33-
}, {});
34-
35-
const examples = data.examples.edges.map(({node}) => ({
36-
content: node.html,
37-
id: node.fields.slug.replace(/^.+\//, '').replace('.html', ''),
38-
title: node.frontmatter.title,
39-
}));
40-
41-
const marketing = data.marketing.edges.map(({node}) => ({
42-
title: node.frontmatter.title,
43-
content: node.html,
44-
}));
45-
46-
this.state = {
47-
code,
48-
examples,
49-
marketing,
50-
};
51-
}
24+
state = {
25+
babelLoaded: false
26+
};
5227

5328
componentDidMount() {
54-
const {code, examples} = this.state;
55-
56-
examples.forEach(({id}) => {
57-
renderExamplePlaceholder(id);
58-
});
59-
60-
function mountCodeExamples() {
61-
examples.forEach(({id}) => {
62-
mountCodeExample(id, code[id]);
29+
loadScript(babelURL).then(() => {
30+
this.setState({
31+
babelLoaded: true
6332
});
64-
}
65-
66-
loadScript(babelURL).then(mountCodeExamples, error => {
33+
}, error => {
6734
console.error('Babel failed to load.');
68-
69-
mountCodeExamples();
7035
});
7136
}
72-
7337
render() {
74-
const {examples, marketing} = this.state;
75-
const {location} = this.props;
38+
const {babelLoaded} = this.state;
39+
const {data, location} = this.props;
40+
const {examples, marketing} = data;
7641

7742
return (
7843
<Layout location={location}>
@@ -217,7 +182,7 @@ class Home extends Component {
217182
whiteSpace: 'nowrap',
218183
},
219184
}}>
220-
{marketing.map((column, index) => (
185+
{marketing.edges.map(({ node: column }, index) => (
221186
<div
222187
key={index}
223188
css={{
@@ -266,9 +231,9 @@ class Home extends Component {
266231
},
267232
},
268233
]}>
269-
{column.title}
234+
{column.frontmatter.title}
270235
</h3>
271-
<div dangerouslySetInnerHTML={{__html: column.content}} />
236+
<div dangerouslySetInnerHTML={{__html: column.html }} />
272237
</div>
273238
))}
274239
</div>
@@ -283,7 +248,7 @@ class Home extends Component {
283248
/>
284249
<section css={sectionStyles}>
285250
<div id="examples">
286-
{examples.map((example, index) => (
251+
{examples.edges.map(({ node }, index) => (
287252
<div
288253
key={index}
289254
css={{
@@ -297,11 +262,12 @@ class Home extends Component {
297262
marginTop: 80,
298263
},
299264
}}>
300-
<h3 css={headingStyles}>{example.title}</h3>
301-
<div
302-
dangerouslySetInnerHTML={{__html: example.content}}
303-
/>
304-
<div id={example.id} />
265+
<CodeExample code={node.code} loaded={babelLoaded}>
266+
<h3 css={headingStyles}>{node.frontmatter.title}</h3>
267+
<div
268+
dangerouslySetInnerHTML={{__html: node.html}}
269+
/>
270+
</CodeExample>
305271
</div>
306272
))}
307273
</div>
@@ -339,7 +305,6 @@ class Home extends Component {
339305

340306
Home.propTypes = {
341307
data: PropTypes.shape({
342-
code: PropTypes.object.isRequired,
343308
examples: PropTypes.object.isRequired,
344309
marketing: PropTypes.object.isRequired,
345310
}).isRequired,
@@ -382,22 +347,13 @@ const CtaItem = ({children, primary = false}) => (
382347

383348
export const pageQuery = graphql`
384349
query IndexMarkdown {
385-
code: allExampleCode {
386-
edges {
387-
node {
388-
id
389-
internal {
390-
contentDigest
391-
}
392-
}
393-
}
394-
}
395350
examples: allMarkdownRemark(
396351
filter: {fileAbsolutePath: {regex: "//home/examples//"}}
397352
sort: {fields: [frontmatter___order], order: ASC}
398353
) {
399354
edges {
400355
node {
356+
code
401357
fields {
402358
slug
403359
}

0 commit comments

Comments
 (0)