adding copy code to code samples in rest docs (#26043)
This commit is contained in:
@@ -1,24 +1,68 @@
|
|||||||
import cx from 'classnames'
|
import cx from 'classnames'
|
||||||
|
import { CheckIcon, CopyIcon } from '@primer/octicons-react'
|
||||||
|
import { Tooltip } from '@primer/react'
|
||||||
|
|
||||||
|
import useClipboard from 'components/hooks/useClipboard'
|
||||||
|
|
||||||
import styles from './CodeBlock.module.scss'
|
import styles from './CodeBlock.module.scss'
|
||||||
|
|
||||||
type Props = {
|
type Props = {
|
||||||
verb?: string
|
verb?: string
|
||||||
|
headingLang?: string
|
||||||
codeBlock: string
|
codeBlock: string
|
||||||
highlight?: string
|
highlight?: string
|
||||||
}
|
}
|
||||||
|
|
||||||
export function CodeBlock({ verb, codeBlock, highlight }: Props) {
|
export function CodeBlock({ verb, headingLang, codeBlock, highlight }: Props) {
|
||||||
|
const [isCopied, setCopied] = useClipboard(codeBlock, {
|
||||||
|
successDuration: 1400,
|
||||||
|
})
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<pre className={cx(styles.methodCodeBlock, 'rounded-1 border')} data-highlight={highlight}>
|
<div className="code-extra">
|
||||||
<code>
|
{headingLang && (
|
||||||
{verb && (
|
<header className="d-flex flex-justify-between flex-items-center p-2 text-small rounded-top-1 border">
|
||||||
<span className="color-bg-accent-emphasis color-fg-on-emphasis rounded-1 text-uppercase">
|
{headingLang === 'JavaScript' ? (
|
||||||
{verb}
|
<span>
|
||||||
</span>
|
{headingLang} (
|
||||||
)}{' '}
|
<a className="text-underline" href="https://github.com/octokit/core.js#readme">
|
||||||
{codeBlock}
|
@octokit/core.js
|
||||||
</code>
|
</a>
|
||||||
</pre>
|
)
|
||||||
|
</span>
|
||||||
|
) : (
|
||||||
|
`${headingLang}`
|
||||||
|
)}
|
||||||
|
<Tooltip direction="w" aria-label={isCopied ? 'Copied!' : 'Copy to clipboard'}>
|
||||||
|
<button className="btn-octicon" onClick={() => setCopied()}>
|
||||||
|
{isCopied ? <CheckIcon /> : <CopyIcon />}
|
||||||
|
</button>
|
||||||
|
</Tooltip>
|
||||||
|
</header>
|
||||||
|
)}
|
||||||
|
<pre
|
||||||
|
className={cx(
|
||||||
|
styles.methodCodeBlock,
|
||||||
|
'd-flex flex-justify-between flex-items-center rounded-1 border'
|
||||||
|
)}
|
||||||
|
data-highlight={highlight}
|
||||||
|
>
|
||||||
|
<code>
|
||||||
|
{verb && (
|
||||||
|
<span className="color-bg-accent-emphasis color-fg-on-emphasis rounded-1 text-uppercase">
|
||||||
|
{verb}
|
||||||
|
</span>
|
||||||
|
)}{' '}
|
||||||
|
{codeBlock}
|
||||||
|
</code>
|
||||||
|
{!headingLang && (
|
||||||
|
<Tooltip direction="w" aria-label={isCopied ? 'Copied!' : 'Copy to clipboard'}>
|
||||||
|
<button className="btn-octicon" onClick={() => setCopied()}>
|
||||||
|
{isCopied ? <CheckIcon /> : <CopyIcon />}
|
||||||
|
</button>
|
||||||
|
</Tooltip>
|
||||||
|
)}
|
||||||
|
</pre>
|
||||||
|
</div>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,22 +18,10 @@ export function RestCodeSamples({ slug, xCodeSamples }: Props) {
|
|||||||
{xCodeSamples.map((sample, index) => {
|
{xCodeSamples.map((sample, index) => {
|
||||||
const sampleElements: JSX.Element[] = []
|
const sampleElements: JSX.Element[] = []
|
||||||
if (sample.lang !== 'Ruby') {
|
if (sample.lang !== 'Ruby') {
|
||||||
sampleElements.push(
|
|
||||||
sample.lang === 'JavaScript' ? (
|
|
||||||
<h5 key={`${sample.lang}-${index}`}>
|
|
||||||
{sample.lang} (
|
|
||||||
<a className="text-underline" href="https://github.com/octokit/core.js#readme">
|
|
||||||
@octokit/core.js
|
|
||||||
</a>
|
|
||||||
)
|
|
||||||
</h5>
|
|
||||||
) : (
|
|
||||||
<h5 key={`${sample.lang}-${index}`}>{sample.lang}</h5>
|
|
||||||
)
|
|
||||||
)
|
|
||||||
sampleElements.push(
|
sampleElements.push(
|
||||||
<CodeBlock
|
<CodeBlock
|
||||||
key={sample.lang + index}
|
key={sample.lang + index}
|
||||||
|
headingLang={sample.lang}
|
||||||
codeBlock={sample.source}
|
codeBlock={sample.source}
|
||||||
highlight={sample.lang === 'JavaScript' ? 'javascript' : 'curl'}
|
highlight={sample.lang === 'JavaScript' ? 'javascript' : 'curl'}
|
||||||
></CodeBlock>
|
></CodeBlock>
|
||||||
|
|||||||
Reference in New Issue
Block a user