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