-
Notifications
You must be signed in to change notification settings - Fork 591
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
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
16fd516
Add Highlight component w/ bash, JS, TS, CSS, JSON support
kylesuss 11732d4
Fix LinkWrapper proptypes so it can accept an object
kylesuss 2b1f272
Add missing boolean style modifiers to Link
kylesuss eac7d90
Allow Highlight component to receive styling props
kylesuss File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,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 = () => ( | ||
<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
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" | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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