-
Notifications
You must be signed in to change notification settings - Fork 114
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #409 from newrelic/jerel/unify-code-snippets
Unify code blocks into a single component
- Loading branch information
Showing
27 changed files
with
791 additions
and
298 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
{ | ||
"presets": ["babel-preset-gatsby"], | ||
"plugins": [ | ||
[ | ||
"babel-plugin-prismjs", | ||
{ | ||
"languages": [ | ||
"css", | ||
"hcl", | ||
"javascript", | ||
"json", | ||
"jsx", | ||
"ruby", | ||
"shell", | ||
"sql" | ||
] | ||
} | ||
] | ||
] | ||
} |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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 |
---|---|---|
|
@@ -46,3 +46,7 @@ | |
color: var(--color-brand-400); | ||
} | ||
} | ||
|
||
.small { | ||
font-size: 0.75rem; | ||
} |
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,113 @@ | ||
import React, { useState, useEffect } from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import cx from 'classnames'; | ||
import Button from './Button'; | ||
import CodeEditor from './CodeEditor'; | ||
import CodeHighlight from './CodeHighlight'; | ||
import FeatherIcon from './FeatherIcon'; | ||
import MiddleEllipsis from 'react-middle-ellipsis'; | ||
import { LiveError, LivePreview, LiveProvider } from 'react-live'; | ||
import styles from './CodeBlock.module.scss'; | ||
import useClipboard from '../hooks/useClipboard'; | ||
import useFormattedCode from '../hooks/useFormattedCode'; | ||
|
||
const defaultComponents = { | ||
Preview: LivePreview, | ||
}; | ||
|
||
const CodeBlock = ({ | ||
children, | ||
components: componentOverrides = {}, | ||
copyable, | ||
live, | ||
highlightedLines, | ||
fileName, | ||
language, | ||
lineNumbers, | ||
preview, | ||
scope, | ||
formatOptions, | ||
}) => { | ||
const components = { ...defaultComponents, ...componentOverrides }; | ||
const formattedCode = useFormattedCode(children.trim(), formatOptions); | ||
const [copied, copy] = useClipboard(); | ||
const [code, setCode] = useState(formattedCode); | ||
|
||
useEffect(() => { | ||
setCode(formattedCode); | ||
}, [formattedCode]); | ||
|
||
return ( | ||
<LiveProvider code={code} scope={scope}> | ||
{preview && <components.Preview className={styles.preview} />} | ||
<div className={cx(styles.container, { [styles.withPreview]: preview })}> | ||
<div className={styles.scrollContainer}> | ||
{live ? ( | ||
<CodeEditor | ||
value={code} | ||
language={language} | ||
lineNumbers={lineNumbers} | ||
onChange={setCode} | ||
/> | ||
) : ( | ||
<CodeHighlight | ||
highlightedLines={highlightedLines} | ||
language={language} | ||
lineNumbers={lineNumbers} | ||
> | ||
{code} | ||
</CodeHighlight> | ||
)} | ||
</div> | ||
|
||
{(copyable || fileName) && ( | ||
<div className={styles.statusBar}> | ||
<div className={styles.fileName}> | ||
{fileName && ( | ||
<MiddleEllipsis> | ||
<span title={fileName}>{fileName}</span> | ||
</MiddleEllipsis> | ||
)} | ||
</div> | ||
<Button | ||
type="button" | ||
className={styles.copyButton} | ||
variant={Button.VARIANT.PLAIN} | ||
onClick={() => copy(code)} | ||
size={Button.SIZE.SMALL} | ||
> | ||
<FeatherIcon name="copy" className={styles.copyButtonIcon} /> | ||
{copied ? 'Copied' : 'Copy output'} | ||
</Button> | ||
</div> | ||
)} | ||
</div> | ||
{(live || preview) && <LiveError className={styles.liveError} />} | ||
</LiveProvider> | ||
); | ||
}; | ||
|
||
CodeBlock.propTypes = { | ||
fileName: PropTypes.string, | ||
components: PropTypes.shape({ | ||
Preview: PropTypes.elementType, | ||
}), | ||
copyable: PropTypes.bool, | ||
children: PropTypes.string.isRequired, | ||
formatOptions: PropTypes.object, | ||
highlightedLines: PropTypes.string, | ||
language: PropTypes.string, | ||
lineNumbers: PropTypes.bool, | ||
live: PropTypes.bool, | ||
preview: PropTypes.bool, | ||
scope: PropTypes.object, | ||
}; | ||
|
||
CodeBlock.defaultProps = { | ||
copyable: true, | ||
lineNumbers: false, | ||
live: false, | ||
preview: false, | ||
}; | ||
|
||
export default CodeBlock; |
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,69 @@ | ||
.container { | ||
background: var(--color-nord-0); | ||
border-radius: 4px; | ||
|
||
:global(.light-mode) & { | ||
background: var(--color-nord-6); | ||
} | ||
|
||
&.withPreview { | ||
border-top-left-radius: 0; | ||
border-top-right-radius: 0; | ||
} | ||
} | ||
|
||
.scrollContainer { | ||
max-height: 26em; | ||
overflow: auto; | ||
} | ||
|
||
.statusBar { | ||
color: var(--color-nord-6); | ||
display: flex; | ||
justify-content: space-between; | ||
align-items: center; | ||
background: var(--color-nord-1); | ||
border-bottom-left-radius: 4px; | ||
border-bottom-right-radius: 4px; | ||
padding: 0 1rem; | ||
font-size: 0.75rem; | ||
|
||
:global(.light-mode) & { | ||
color: var(--color-nord-0); | ||
background: var(--color-nord-4); | ||
} | ||
} | ||
|
||
.fileName { | ||
font-family: var(--code-font); | ||
white-space: nowrap; | ||
overflow: hidden; | ||
padding-right: 0.5rem; | ||
} | ||
|
||
.copyButton { | ||
white-space: nowrap; | ||
} | ||
|
||
.copyButtonIcon { | ||
margin-right: 0.5rem; | ||
} | ||
|
||
.liveError { | ||
color: white; | ||
background: var(--color-red-400); | ||
padding: 0.5rem 1rem; | ||
font-size: 0.75rem; | ||
overflow: auto; | ||
margin-top: 0.5rem; | ||
border-radius: 2px; | ||
} | ||
|
||
.preview { | ||
padding: 2rem; | ||
background: var(--color-white); | ||
border: 1px solid var(--color-neutrals-100); | ||
box-shadow: var(--boxshadow); | ||
border-top-left-radius: 4px; | ||
border-top-right-radius: 4px; | ||
} |
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,43 @@ | ||
import React from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import cx from 'classnames'; | ||
import Editor from 'react-simple-code-editor'; | ||
import CodeHighlight from './CodeHighlight'; | ||
import styles from './CodeEditor.module.scss'; | ||
|
||
const CodeEditor = ({ value, language, lineNumbers, onChange }) => { | ||
const lineNumberWidth = value.trim().split('\n').length.toString().length; | ||
|
||
return ( | ||
<Editor | ||
value={value} | ||
padding={16} | ||
onValueChange={onChange} | ||
highlight={(code) => ( | ||
<CodeHighlight | ||
wrap | ||
className={styles.editor} | ||
language={language} | ||
lineNumbers={lineNumbers} | ||
> | ||
{code} | ||
</CodeHighlight> | ||
)} | ||
textareaClassName={cx({ [styles.lineNumbers]: lineNumbers })} | ||
style={{ | ||
fontFamily: 'var(--code-font)', | ||
fontSize: '0.75rem', | ||
'--line-number-width': `${lineNumberWidth}ch`, | ||
}} | ||
/> | ||
); | ||
}; | ||
|
||
CodeEditor.propTypes = { | ||
language: PropTypes.string.isRequired, | ||
lineNumbers: PropTypes.bool, | ||
onChange: PropTypes.func, | ||
value: PropTypes.string.isRequired, | ||
}; | ||
|
||
export default CodeEditor; |
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,7 @@ | ||
.editor { | ||
padding: 0 !important; | ||
} | ||
|
||
.lineNumbers { | ||
padding-left: calc(2rem + var(--line-number-width)) !important; | ||
} |
Oops, something went wrong.