Browse Source

chore: minor tweaks and run prettier

main
Dustin Schau 6 years ago
parent
commit
fc0ef96fb6
  1. 2
      gatsby-config.js
  2. 19
      plugins/gatsby-transformer-home-example-code/gatsby-node.js
  3. 8
      src/components/CodeEditor/CodeEditor.js
  4. 43
      src/components/CodeExample/CodeExample.js
  5. 53
      src/pages/index.js
  6. 36
      src/utils/mountCodeExample.js

2
gatsby-config.js

@ -34,7 +34,7 @@ module.exports = {
resolve: 'gatsby-source-filesystem', resolve: 'gatsby-source-filesystem',
options: { options: {
name: 'pages', name: 'pages',
path: `${__dirname}/src/pages` path: `${__dirname}/src/pages`,
}, },
}, },
{ {

19
plugins/gatsby-transformer-home-example-code/gatsby-node.js

@ -1,18 +1,23 @@
const crypto = require(`crypto`) const crypto = require(`crypto`);
// docblock goes here // docblock goes here
const createContentDigest = obj => crypto const createContentDigest = obj =>
.createHash(`md5`) crypto
.update(obj) .createHash(`md5`)
.digest(`hex`) .update(obj)
.digest(`hex`);
// Store code snippets in GraphQL for the home page examples. // Store code snippets in GraphQL for the home page examples.
// Snippets will be matched with markdown templates of the same name. // Snippets will be matched with markdown templates of the same name.
exports.onCreateNode = async ({node, loadNodeContent, actions}) => { exports.onCreateNode = async ({node, loadNodeContent, actions}) => {
const {createNode} = actions; const {createNode} = actions;
const {absolutePath, ext, name, relativeDirectory, sourceInstanceName} = node const {absolutePath, ext, name, relativeDirectory, sourceInstanceName} = node;
if (sourceInstanceName === 'content' && relativeDirectory === 'home/examples' && ext === '.js') { if (
sourceInstanceName === 'content' &&
relativeDirectory === 'home/examples' &&
ext === '.js'
) {
const code = await loadNodeContent(node); const code = await loadNodeContent(node);
createNode({ createNode({
id: name, id: name,

8
src/components/CodeEditor/CodeEditor.js

@ -65,8 +65,10 @@ class CodeEditor extends Component {
} }
return ( return (
<div css={{ flex: 1 }}> <div css={{flex: 1}}>
<LiveProvider code={showJSX ? code : compiledES6} mountStylesheet={false}> <LiveProvider
code={showJSX ? code : compiledES6}
mountStylesheet={false}>
<div <div
css={{ css={{
[media.greaterThan('medium')]: { [media.greaterThan('medium')]: {
@ -234,7 +236,7 @@ class CodeEditor extends Component {
</div> </div>
)} )}
</div> </div>
</LiveProvider> </LiveProvider>
</div> </div>
); );
} }

43
src/components/CodeExample/CodeExample.js

@ -1,4 +1,4 @@
import React, { Component } from 'react'; import React, {Component} from 'react';
import {colors, media} from 'theme'; import {colors, media} from 'theme';
import CodeEditor from '../CodeEditor/CodeEditor'; import CodeEditor from '../CodeEditor/CodeEditor';
@ -7,16 +7,28 @@ class CodeExample extends Component {
const {children, code, loaded} = this.props; const {children, code, loaded} = this.props;
return ( return (
<div <div
css={{ css={{
[media.greaterThan('xlarge')]: { marginTop: 40,
display: 'flex',
flexDirection: 'row', '&:first-child': {
}, marginTop: 0,
},
[media.lessThan('large')]: {
display: 'block', [media.greaterThan('xlarge')]: {
}, marginTop: 80,
}}> },
}}>
<div
css={{
[media.greaterThan('xlarge')]: {
display: 'flex',
flexDirection: 'row',
},
[media.lessThan('large')]: {
display: 'block',
},
}}>
{children && ( {children && (
<div <div
css={{ css={{
@ -45,9 +57,14 @@ class CodeExample extends Component {
{children} {children}
</div> </div>
)} )}
{loaded ? <CodeEditor code={code} /> : <h4>Loading code example...</h4>} {loaded ? (
<CodeEditor code={code} />
) : (
<h4>Loading code example...</h4>
)}
</div>
</div> </div>
) );
} }
} }

53
src/pages/index.js

@ -22,24 +22,28 @@ import logoWhiteSvg from 'icons/logo-white.svg';
class Home extends Component { class Home extends Component {
state = { state = {
babelLoaded: false babelLoaded: false,
}; };
componentDidMount() { componentDidMount() {
loadScript(babelURL).then(() => { loadScript(babelURL).then(
this.setState({ () => {
babelLoaded: true this.setState({
}); babelLoaded: true,
}, error => { });
console.error('Babel failed to load.'); },
}); error => {
console.error('Babel failed to load.');
},
);
} }
render() { render() {
const {babelLoaded} = this.state; const {babelLoaded} = this.state;
const {data, location} = this.props; const {data, location} = this.props;
const {codeExamples, examples, marketing} = data; const {codeExamples, examples, marketing} = data;
const code = codeExamples.edges.reduce((lookup, { node }) => { const code = codeExamples.edges.reduce((lookup, {node}) => {
lookup[node.mdAbsolutePath] = node.code; lookup[node.mdAbsolutePath] = node.code;
return lookup; return lookup;
}, {}); }, {});
@ -187,7 +191,7 @@ class Home extends Component {
whiteSpace: 'nowrap', whiteSpace: 'nowrap',
}, },
}}> }}>
{marketing.edges.map(({ node: column }, index) => ( {marketing.edges.map(({node: column}, index) => (
<div <div
key={index} key={index}
css={{ css={{
@ -238,7 +242,7 @@ class Home extends Component {
]}> ]}>
{column.frontmatter.title} {column.frontmatter.title}
</h3> </h3>
<div dangerouslySetInnerHTML={{__html: column.html }} /> <div dangerouslySetInnerHTML={{__html: column.html}} />
</div> </div>
))} ))}
</div> </div>
@ -253,27 +257,14 @@ class Home extends Component {
/> />
<section css={sectionStyles}> <section css={sectionStyles}>
<div id="examples"> <div id="examples">
{examples.edges.map(({ node }, index) => ( {examples.edges.map(({node}, index) => (
<div <CodeExample
key={index} key={index}
css={{ code={code[node.fileAbsolutePath]}
marginTop: 40, loaded={babelLoaded}>
<h3 css={headingStyles}>{node.frontmatter.title}</h3>
'&:first-child': { <div dangerouslySetInnerHTML={{__html: node.html}} />
marginTop: 0, </CodeExample>
},
[media.greaterThan('xlarge')]: {
marginTop: 80,
},
}}>
<CodeExample code={code[node.fileAbsolutePath]} loaded={babelLoaded}>
<h3 css={headingStyles}>{node.frontmatter.title}</h3>
<div
dangerouslySetInnerHTML={{__html: node.html}}
/>
</CodeExample>
</div>
))} ))}
</div> </div>
</section> </section>

36
src/utils/mountCodeExample.js

@ -1,36 +0,0 @@
/**
* Copyright (c) 2013-present, Facebook, Inc.
*
* @emails react-core
*/
import CodeEditor from '../components/CodeEditor';
import React from 'react';
import ReactDOM from 'react-dom';
// TODO This is a huge hack.
// Remark transform this template to split code examples and their targets apart.
const mountCodeExample = (containerId, code) => {
const container = document.getElementById(containerId);
const parent = container.parentElement;
const children = Array.prototype.filter.call(
parent.children,
child => child !== container,
);
children.forEach(child => parent.removeChild(child));
const description = children
.map(child => child.outerHTML)
.join('')
.replace(/`([^`]+)`/g, '<code>$1</code>');
ReactDOM.render(
<CodeEditor code={code}>
{<div dangerouslySetInnerHTML={{__html: description}} />}
</CodeEditor>,
container,
);
};
export default mountCodeExample;
Loading…
Cancel
Save