Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Highlight component w/ bash, JS, TS, CSS, JSON support #7

Merged
merged 4 commits into from
Jun 6, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
"dependencies": {
"global": "^4.3.2",
"polished": "^3.3.0",
"prismjs": "1.13.0",
"react-helmet": "^5.2.1",
"react-lazyload": "^2.5.0",
"react-modal": "^3.8.1",
Expand Down
66 changes: 66 additions & 0 deletions src/components/Highlight.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/* eslint-disable react/no-danger */
import React from 'react';
import ReactDOM from 'react-dom';
import styled from 'styled-components';
import Prism from 'prismjs';
import loadLanguages from 'prismjs/components/index';
import PropTypes from 'prop-types';

import { color, typography } from './shared/styles';

import 'prismjs/themes/prism.css';

loadLanguages(['bash', 'typescript', 'json']);

// Tweak Prism styling
const HighlightBlock = styled.div`
*:not(pre) > code[class*='language-'],
pre[class*='language-'] {
background: ${color.lighter};
margin: 2em 0;
}

code[class*='language-'],
pre[class*='language-'] {
font-size: ${typography.size.s3}px;
}

code {
font-size: 17px;
}

.aside code {
font-size: 15px;
}
`;

class Highlight extends React.Component {
componentDidMount() {
this.highlightCode();
}

componentDidUpdate() {
this.highlightCode();
}

highlightCode() {
const domNode = ReactDOM.findDOMNode(this); // eslint-disable-line
Prism.highlightAllUnder(domNode);
}

render() {
const { children, ...rest } = this.props;

return (
<HighlightBlock {...rest}>
<div dangerouslySetInnerHTML={{ __html: children }} />
</HighlightBlock>
);
}
}

Highlight.propTypes = {
children: PropTypes.node.isRequired,
};

export default Highlight;
71 changes: 71 additions & 0 deletions src/components/Highlight.stories.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import React from 'react';
import { storiesOf } from '@storybook/react';
import Highlight from './Highlight';

const bash = `
<pre class="language-bash"><code class="language-bash"><span class="token comment"># Highlight bash:</span>
npx install some-package-name
<span class="token function">cd</span> some-package-name
</code></pre>
`;

const javascript = `
<pre class="language-javascript"><code class="language-javascript"><span class="token comment">// Highlight JavaScript:</span>
import React from 'react'

const MyComponent = () => (
&#x3C;div>My component renders all the things&#x3C;/div>
)

export default MyComponent
</code></pre>
`;

const typescript = `
<pre class="language-javascript"><code class="language-javascript"><span class="token comment">// Highlight Typescript:</span>
import React from 'react'

interface InterfaceMyComponentProps {
isCool: boolean;
}

const MyComponent: React.SFC&#x3C;InterfaceMyComponentProps> = ({ isCool }) => (
&#x3C;div>My component isCool: {isCool}&#x3C;/div>
)

export default MyComponent
</code></pre>
`;

const css = `
<pre class="language-css"><code class="language-css"><span class="token comment">/* Highlight CSS: */</span>
.someClass {
position: relative;
}
</code></pre>
`;

const json = `
<pre class="language-json"><code class="language-json">{
"name": "@yourorg/package",
"version": "0.0.1",
"description": "A sweet package",
"repository": {
"type": "git",
"url": "https://github.com/yourorg/package.git"
}
}
</code></pre>
`;

storiesOf('Design System|Highlight', module)
.addParameters({ component: Highlight })
.add('all languages', () => (
<div>
<Highlight>{bash}</Highlight>
<Highlight>{javascript}</Highlight>
<Highlight>{typescript}</Highlight>
<Highlight>{css}</Highlight>
<Highlight>{json}</Highlight>
</div>
));
10 changes: 9 additions & 1 deletion src/components/Link.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,11 @@ Link.propTypes = {
children: PropTypes.node,
withArrow: PropTypes.bool,
containsIcon: PropTypes.bool,
LinkWrapper: PropTypes.func,
LinkWrapper: PropTypes.oneOfType([PropTypes.func, PropTypes.object]),
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was throwing some PropTypes errors in the console

inverse: PropTypes.bool,
nochrome: PropTypes.bool,
secondary: PropTypes.bool,
tertiary: PropTypes.bool,
};

Link.defaultProps = {
Expand All @@ -169,4 +173,8 @@ Link.defaultProps = {
withArrow: false,
containsIcon: false,
LinkWrapper: undefined,
inverse: false,
nochrome: false,
secondary: false,
tertiary: false,
};
7 changes: 7 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7472,6 +7472,13 @@ pretty-hrtime@^1.0.3:
version "1.0.3"
resolved "https://registry.yarnpkg.com/pretty-hrtime/-/pretty-hrtime-1.0.3.tgz#b7e3ea42435a4c9b2759d99e0f201eb195802ee1"

[email protected]:
version "1.13.0"
resolved "https://registry.npmjs.org/prismjs/-/prismjs-1.13.0.tgz#edcc14a90bbd72a03e5ffd2bab81a04c79a607a6"
integrity sha512-0/1Fiyg3MCzepo6t6Wzx2Ef4nftGKeQIv+Z6LO38RcYJN5QV2ePHh8W41ZkFn57B++6BnglF6fCiJ9b80YlzkQ==
optionalDependencies:
clipboard "^2.0.0"

prismjs@^1.8.4, prismjs@~1.16.0:
version "1.16.0"
resolved "https://registry.yarnpkg.com/prismjs/-/prismjs-1.16.0.tgz#406eb2c8aacb0f5f0f1167930cb83835d10a4308"
Expand Down