-
Notifications
You must be signed in to change notification settings - Fork 276
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
475 additions
and
285 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
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 |
---|---|---|
@@ -1,59 +1,42 @@ | ||
import React from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import { CodeSnippet } from 'carbon-components-react'; | ||
import Highlight, { defaultProps } from 'prism-react-renderer'; | ||
import { CopyButton } from 'carbon-components-react'; | ||
|
||
/* import Prism from 'prismjs'; | ||
import 'prismjs/components/prism-bash'; | ||
import 'prismjs/components/prism-scss'; */ | ||
import { CopyToClipboard } from 'react-copy-to-clipboard/lib/Component'; | ||
import { settings } from 'carbon-components'; | ||
// import { CopyToClipboard } from 'react-copy-to-clipboard/lib/Component'; | ||
import cx from 'classnames'; | ||
import { Row } from '../Grid'; | ||
|
||
const { prefix } = settings; | ||
|
||
export default class Code extends React.Component { | ||
static propTypes = { | ||
children: PropTypes.node, | ||
}; | ||
|
||
state = { | ||
multi: true, | ||
}; | ||
|
||
componentDidMount() { | ||
// Prism.highlightAll(); | ||
if (this.codeRef) { | ||
if (this.codeRef.clientHeight > 20) { | ||
this.setState({ multi: true }); | ||
} else { | ||
this.setState({ multi: false }); | ||
} | ||
} | ||
} | ||
|
||
/* componentDidUpdate() { | ||
Prism.highlightAll(); | ||
} */ | ||
|
||
render() { | ||
const { children } = this.props; | ||
const type = this.state.multi ? 'multi' : 'single'; | ||
|
||
let textToCopy; | ||
if (children.props.children) { | ||
textToCopy = children.props.children.replace(/(\$ )+/g, ''); | ||
} | ||
|
||
return ( | ||
<Row> | ||
<div className={`${prefix}--snippet--website`}> | ||
<CopyToClipboard text={textToCopy}> | ||
<CodeSnippet type={type}> | ||
<div ref={element => (this.codeRef = element)}>{children}</div> | ||
</CodeSnippet> | ||
</CopyToClipboard> | ||
</div> | ||
</Row> | ||
); | ||
} | ||
} | ||
import { container } from './Code.module.scss'; | ||
|
||
const Code = ({ children, className: classNameProp }) => { | ||
const language = classNameProp.replace(/language-/, ''); | ||
return ( | ||
<Row> | ||
<Highlight {...defaultProps} code={children} language={language}> | ||
{({ className, style, tokens, getLineProps, getTokenProps }) => ( | ||
<pre | ||
className={cx(className, container)} | ||
style={{ ...style, padding: '20px' }} | ||
> | ||
<CopyButton | ||
onClick={() => { | ||
console.log(children); | ||
}} | ||
/> | ||
{tokens.map((line, i) => ( | ||
<div key={i} {...getLineProps({ line, key: i })}> | ||
{line.map((token, key) => ( | ||
<span key={key} {...getTokenProps({ token, key })} /> | ||
))} | ||
</div> | ||
))} | ||
</pre> | ||
)} | ||
</Highlight> | ||
</Row> | ||
); | ||
}; | ||
|
||
export default Code; |
31 changes: 31 additions & 0 deletions
31
packages/gatsby-theme-carbon/src/components/Code/Code.module.scss
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,31 @@ | ||
.container { | ||
padding: $spacing-05; | ||
margin-bottom: $spacing-08; | ||
position: relative; | ||
|
||
// 6 col | ||
@include carbon--breakpoint('md') { | ||
width: 75%; | ||
} | ||
|
||
// 8 col | ||
@include carbon--breakpoint('lg') { | ||
width: 66.667%; | ||
} | ||
} | ||
|
||
.container :global(.bx--snippet-button) { | ||
background-color: transparent; | ||
svg { | ||
fill: $inverse-01; | ||
} | ||
&:hover { | ||
background-color: transparent; | ||
} | ||
} | ||
|
||
.container :global(.bx--btn--copy__feedback) { | ||
&::after { | ||
background-color: red; | ||
} | ||
} |
Oops, something went wrong.