Skip to content

Commit 28e81ba

Browse files
authored
adding copy code to code samples in rest docs (github#26043)
1 parent feb3f25 commit 28e81ba

File tree

2 files changed

+56
-24
lines changed

2 files changed

+56
-24
lines changed

components/rest/CodeBlock.tsx

Lines changed: 55 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,68 @@
11
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'
26

37
import styles from './CodeBlock.module.scss'
48

59
type Props = {
610
verb?: string
11+
headingLang?: string
712
codeBlock: string
813
highlight?: string
914
}
1015

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+
1221
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>
2367
)
2468
}

components/rest/RestCodeSamples.tsx

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,10 @@ export function RestCodeSamples({ slug, xCodeSamples }: Props) {
1818
{xCodeSamples.map((sample, index) => {
1919
const sampleElements: JSX.Element[] = []
2020
if (sample.lang !== 'Ruby') {
21-
sampleElements.push(
22-
sample.lang === 'JavaScript' ? (
23-
<h5 key={`${sample.lang}-${index}`}>
24-
{sample.lang} (
25-
<a className="text-underline" href="https://github.com/octokit/core.js#readme">
26-
@octokit/core.js
27-
</a>
28-
)
29-
</h5>
30-
) : (
31-
<h5 key={`${sample.lang}-${index}`}>{sample.lang}</h5>
32-
)
33-
)
3421
sampleElements.push(
3522
<CodeBlock
3623
key={sample.lang + index}
24+
headingLang={sample.lang}
3725
codeBlock={sample.source}
3826
highlight={sample.lang === 'JavaScript' ? 'javascript' : 'curl'}
3927
></CodeBlock>

0 commit comments

Comments
 (0)