Skip to content

Commit

Permalink
Render Gutenberg examples assuming they are defining a MyComponent co…
Browse files Browse the repository at this point in the history
…mponent
  • Loading branch information
mmtr committed Aug 3, 2018
1 parent 102f7bd commit 72bf72c
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 3 deletions.
16 changes: 13 additions & 3 deletions client/devdocs/design/gutenberg-component-example.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import React from 'react';
import * as components from '@wordpress/components';
import { withState } from '@wordpress/compose';
import { getSettings } from '@wordpress/date';
import { Fragment } from '@wordpress/element';
import { LiveError, LivePreview, LiveProvider } from 'react-live';
import request from 'superagent';
import codeBlocks from 'gfm-code-blocks';
Expand All @@ -33,12 +34,20 @@ class Example extends React.Component {
format: 'markdown',
} )
.then( ( { text } ) => {
let code = codeBlocks( text ).filter( block => 'jsx' === block.lang )[ 0 ].code;
let code = codeBlocks( text ).find( block => 'jsx' === block.lang ).code;

// react-live cannot resolve imports in real time, so we get rid of them
// (dependencies will be injected via the scope property)
// (dependencies will be injected via the scope property).
code = code.replace( /^.*import.*$/gm, '' );

// All the examples are defining a functional component called My<ComponentName> we
// can render.
code = `
${ code }
render( <My${ this.props.component } /> );
`;

this.setState( { code } );
} );
}
Expand All @@ -49,14 +58,15 @@ class Example extends React.Component {
...components,
withState,
getSettings,
Fragment,
};
const className = classnames(
'design__gutenberg-component-example',
`design__gutenberg-component-example--${ this.props.readmeFilePath }`
);

return code ? (
<LiveProvider code={ code } scope={ scope } className={ className }>
<LiveProvider code={ code } scope={ scope } className={ className } noInline={ true }>
<LiveError />
<LivePreview />
</LiveProvider>
Expand Down
3 changes: 3 additions & 0 deletions client/devdocs/design/gutenberg-components.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export default class extends React.Component {

const componentsWithExample = [
'Autocomplete',
'BaseControl',
'Button',
'ButtonGroup',
'CheckboxControl',
Expand All @@ -66,6 +67,7 @@ export default class extends React.Component {
'MenuGroup',
'MenuItem',
'MenuItemsChoice',
'Modal',
];

return (
Expand Down Expand Up @@ -100,6 +102,7 @@ export default class extends React.Component {
<GutenbergComponentExample
key={ componentName }
asyncName={ componentName }
component={ componentName }
readmeFilePath={ readmeFilePath }
/>
);
Expand Down
33 changes: 33 additions & 0 deletions npm-shrinkwrap.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"@wordpress/compose": "1.0.2",
"@wordpress/data": "1.0.1",
"@wordpress/date": "1.0.2",
"@wordpress/element": "1.0.2",
"autoprefixer": "9.1.0",
"autosize": "4.0.2",
"babel-loader": "8.0.0-beta.4",
Expand Down

0 comments on commit 72bf72c

Please sign in to comment.