|
1 | 1 | import cx from 'classnames' |
| 2 | +import { CheckIcon, CopyIcon } from '@primer/octicons-react' |
| 3 | +import { Tooltip } from '@primer/react' |
| 4 | + |
| 5 | +import useClipboard from 'components/hooks/useClipboard' |
2 | 6 |
|
3 | 7 | import styles from './CodeBlock.module.scss' |
4 | 8 |
|
5 | 9 | type Props = { |
6 | 10 | verb?: string |
| 11 | + // Only Code samples should have a copy icon - if there's a headingLang it's a code sample |
| 12 | + headingLang?: string |
7 | 13 | codeBlock: string |
8 | 14 | highlight?: string |
9 | 15 | } |
10 | 16 |
|
11 | | -export function CodeBlock({ verb, codeBlock, highlight }: Props) { |
| 17 | +export function CodeBlock({ verb, headingLang, codeBlock, highlight }: Props) { |
| 18 | + const [isCopied, setCopied] = useClipboard(codeBlock, { |
| 19 | + successDuration: 1400, |
| 20 | + }) |
| 21 | + |
12 | 22 | return ( |
13 | | - <pre className={cx(styles.methodCodeBlock, 'rounded-1 border')} data-highlight={highlight}> |
14 | | - <code> |
15 | | - {verb && ( |
16 | | - <span className="color-bg-accent-emphasis color-fg-on-emphasis rounded-1 text-uppercase"> |
17 | | - {verb} |
18 | | - </span> |
19 | | - )}{' '} |
20 | | - {codeBlock} |
21 | | - </code> |
22 | | - </pre> |
| 23 | + <div className="code-extra"> |
| 24 | + {headingLang && ( |
| 25 | + <header className="d-flex flex-justify-between flex-items-center p-2 text-small rounded-top-1 border"> |
| 26 | + {headingLang === 'JavaScript' ? ( |
| 27 | + <span> |
| 28 | + {headingLang} ( |
| 29 | + <a className="text-underline" href="https://github.com/octokit/core.js#readme"> |
| 30 | + @octokit/core.js |
| 31 | + </a> |
| 32 | + ) |
| 33 | + </span> |
| 34 | + ) : ( |
| 35 | + `${headingLang}` |
| 36 | + )} |
| 37 | + <Tooltip direction="w" aria-label={isCopied ? 'Copied!' : 'Copy to clipboard'}> |
| 38 | + <button className="js-btn-copy btn-octicon" onClick={() => setCopied()}> |
| 39 | + {isCopied ? <CheckIcon /> : <CopyIcon />} |
| 40 | + </button> |
| 41 | + </Tooltip> |
| 42 | + </header> |
| 43 | + )} |
| 44 | + <pre |
| 45 | + className={cx( |
| 46 | + styles.methodCodeBlock, |
| 47 | + 'd-flex flex-justify-between flex-items-center rounded-1 border' |
| 48 | + )} |
| 49 | + data-highlight={highlight} |
| 50 | + > |
| 51 | + <code> |
| 52 | + {verb && ( |
| 53 | + <span className="color-bg-accent-emphasis color-fg-on-emphasis rounded-1 text-uppercase"> |
| 54 | + {verb} |
| 55 | + </span> |
| 56 | + )}{' '} |
| 57 | + {codeBlock} |
| 58 | + </code> |
| 59 | + </pre> |
| 60 | + </div> |
23 | 61 | ) |
24 | 62 | } |
0 commit comments