Skip to content

Commit

Permalink
feat: code-snippet initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
vpicone committed Jul 11, 2019
1 parent c056fe0 commit 7394202
Show file tree
Hide file tree
Showing 10 changed files with 475 additions and 285 deletions.
22 changes: 9 additions & 13 deletions packages/example/src/pages/components/Accordion.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -11,36 +11,32 @@ The `<Accordion>` and `<AccordionItem>` components are used together to display
## Example

<Accordion>
<AccordionItem title={(<div>Title 1</div>)}>Content Section</AccordionItem>
<AccordionItem title='Title 2'>Content Section</AccordionItem>
<AccordionItem title='Title 3'>Content Section</AccordionItem>
<AccordionItem title={<div>Title 1</div>}>Content Section</AccordionItem>
<AccordionItem title="Title 2">Content Section</AccordionItem>
<AccordionItem title="Title 3">Content Section</AccordionItem>
</Accordion>



## Code


```
```jsx
<Accordion>
<AccordionItem title='Title 1'>Content Section</AccordionItem>
<AccordionItem title='Title 2'>Content Section</AccordionItem>
<AccordionItem title='Title 3'>Content Section</AccordionItem>
<AccordionItem title="Title 1">Content Section</AccordionItem>
<AccordionItem title="Title 2">Content Section</AccordionItem>
<AccordionItem title="Title 3">Content Section</AccordionItem>
</Accordion>
```


### `Accordion` Props

| property | propType | required | default | description |
| -------- | -------- | -------- | ------- | ----------------------------------------------------------------- |
| --------- | -------- | -------- | ------- | ----------------------------------------------------------------- |
| children | node | | | Pass in the children that will be rendered within the Accordion |
| className | string | | | Specify an optional className to be applied to the container node |

### `AccordionItem` Props

| property | propType | required | default | description |
| --------------- | -------- | -------- | -------------- ----------------| ----------------------------------------------------------------------------------- |
| --------------- | -------- | -------- | ------------------------------ | ----------------------------------------------------------------------------------- |
| title | node | | 'title' | The accordion title |
| renderExpando | func | | props => <button {...props} /> | The callback function to render the expando button. Can be a React component class. |
| iconDescription | string | | 'Expand/Collapse' | The description of the expando icon |
Expand Down
1 change: 1 addition & 0 deletions packages/gatsby-theme-carbon/gatsby-browser.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React from 'react';
import ThemeProvider from './src/components/ThemeProvider';
import { NavContextProvider } from './src/util/context/NavContext';
import './src/styles/prism.css';

export const wrapRootElement = ({ element }) => (
<NavContextProvider>
Expand Down
1 change: 1 addition & 0 deletions packages/gatsby-theme-carbon/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
"lodash": "^4.17.11",
"mkdirp": "^0.5.1",
"node-sass": "^4.11.0",
"prism-react-renderer": "^0.1.7",
"prop-types": "^15.7.2",
"react-copy-to-clipboard": "^5.0.1",
"react-helmet": "^6.0.0-beta",
Expand Down
89 changes: 36 additions & 53 deletions packages/gatsby-theme-carbon/src/components/Code/Code.js
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 packages/gatsby-theme-carbon/src/components/Code/Code.module.scss
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;
}
}
Loading

0 comments on commit 7394202

Please sign in to comment.