Skip to content

Commit

Permalink
Split component example (carbon-design-system#341)
Browse files Browse the repository at this point in the history
* chore(ComponentExample): extract out vanilla instantiation code

This change extracts out instantiation/release code of vanilla
JavaScript library from `<ComponentExample>` and plain-HTML-specific
code from `<CodeExample>`. Doing so paves the cowpath toward better
integration of React live demo to those component.

Refs carbon-design-system#1936.

* fix(ComponentExample): remove unintentional change

* fix(ComponentExample): fix typo

* chore(ComponentExample): remove stale component name list
  • Loading branch information
asudoh authored and alisonjoseph committed Sep 19, 2019
1 parent 21ae620 commit 0b7f38f
Show file tree
Hide file tree
Showing 6 changed files with 321 additions and 246 deletions.
19 changes: 14 additions & 5 deletions src/components/CodeExample/CodeExample.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,23 @@ import PropTypes from 'prop-types';
import CopyToClipboard from 'react-copy-to-clipboard';
import classnames from 'classnames';
import ReactGA from 'react-ga';
import Prism from 'prismjs';
import { highlightAll } from 'prismjs/components/prism-core';
import 'prismjs/components/prism-clike';
import 'prismjs/components/prism-javascript';
import 'prismjs/components/prism-markup';
import 'prismjs/components/prism-jsx';
import { Copy20, ChevronDown16 } from '@carbon/icons-react';

import './prism.css';

class CodeExample extends Component {
static propTypes = {
htmlFile: PropTypes.string,
codeClassName: PropTypes.string,
};

static defaultProps = {
codeClassName: 'language-html',
};

state = {
Expand All @@ -21,12 +30,12 @@ class CodeExample extends Component {

componentDidMount = () => {
if (this.codeBlock.offsetHeight > 190) {
this.setState({ showBtn: true });
this.setState({ showBtn: true }); // eslint-disable-line react/no-did-mount-set-state
}
};

componentDidUpdate = () => {
Prism.highlightAll();
highlightAll();
};

handleCopy = () => {
Expand All @@ -48,7 +57,7 @@ class CodeExample extends Component {
};

render() {
const { htmlFile } = this.props;
const { htmlFile, codeClassName } = this.props;
const copyBtnClass = classnames({
'bx--btn--copy__feedback': true,
'bx--btn--copy__feedback--displayed': this.state.copied,
Expand Down Expand Up @@ -81,7 +90,7 @@ class CodeExample extends Component {
}}>
<span className="code-title">Code:</span>
<pre className="line-numbers">
<code className="language-html">{htmlFile}</code>
<code className={codeClassName}>{htmlFile}</code>
</pre>
</div>
<CopyToClipboard text={htmlFile} onCopy={this.handleCopy}>
Expand Down
32 changes: 32 additions & 0 deletions src/components/CodeExample/CodeExampleHTML.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import React from 'react';
import PropTypes from 'prop-types';
import CodeExample from './CodeExample';
import getHTMLFile from '../../utils/getHTMLFile';

const CodeExampleHTML = ({ component, variation, useLightVersion }) => {
const html = getHTMLFile({
component,
variation,
useLightVersion,
});
return <CodeExample htmlFile={html} codeClassName="language-html" />;
};

CodeExampleHTML.propTypes = {
/**
* The component name.
*/
component: PropTypes.string,

/**
* The component variation name.
*/
variation: PropTypes.string,

/**
* `true` to use the light version.
*/
useLightVersion: PropTypes.bool,
};

export default CodeExampleHTML;
12 changes: 0 additions & 12 deletions src/components/ComponentCode/ComponentCode.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,25 +26,13 @@ export default class ComponentCode extends React.Component {
experimental,
} = this.props;

let htmlFile;
try {
// eslint-disable-next-line import/no-dynamic-require, global-require
htmlFile = require(`carbon-components/html/${component}/${variation}.html`);
} catch (err) {
// eslint-disable-next-line no-console
console.error(
`Unable to import htmlFile at 'carbon-components/html/${component}/${variation}.html'`
);
}

return (
<div className="component-variation bx--row">
<div className="bx--col-lg-12 bx--no-gutter">
<ComponentExample
codepenSlug={codepen}
component={component}
variation={variation}
htmlFile={htmlFile}
hideViewFullRender={this.props.hideViewFullRender}
hasLightVersion={hasLightVersion}
hasReactVersion={hasReactVersion}
Expand Down
Loading

0 comments on commit 0b7f38f

Please sign in to comment.