-
Notifications
You must be signed in to change notification settings - Fork 591
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Highlight component w/ bash, JS, TS, CSS, JSON support
- Loading branch information
Showing
4 changed files
with
145 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/* 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 } = this.props; | ||
return ( | ||
<HighlightBlock> | ||
<div dangerouslySetInnerHTML={{ __html: children }} /> | ||
</HighlightBlock> | ||
); | ||
} | ||
} | ||
|
||
Highlight.propTypes = { | ||
children: PropTypes.node.isRequired, | ||
}; | ||
|
||
export default Highlight; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
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 = () => ( | ||
<div>My component renders all the things</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<InterfaceMyComponentProps> = ({ isCool }) => ( | ||
<div>My component isCool: {isCool}</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> | ||
)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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" | ||
|