Skip to content

Commit ade67ab

Browse files
authored
Merge pull request github#16164 from github/repo-sync
repo sync
2 parents b8985f8 + 39d03e6 commit ade67ab

File tree

5 files changed

+63
-25
lines changed

5 files changed

+63
-25
lines changed

components/lib/events.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,15 @@ function initClipboardEvent() {
245245
})
246246
}
247247

248+
function initCopyButtonEvent() {
249+
document.documentElement.addEventListener('click', (evt) => {
250+
const target = evt.target as HTMLElement
251+
const button = target.closest('.js-btn-copy') as HTMLButtonElement
252+
if (!button) return
253+
sendEvent({ type: EventType.navigate, navigate_label: 'copy icon button' })
254+
})
255+
}
256+
248257
function initLinkEvent() {
249258
document.documentElement.addEventListener('click', (evt) => {
250259
const target = evt.target as HTMLElement
@@ -267,6 +276,7 @@ export default function initializeEvents() {
267276
initPageAndExitEvent() // must come first
268277
initLinkEvent()
269278
initClipboardEvent()
279+
initCopyButtonEvent()
270280
initPrintEvent()
271281
// survey event in ./survey.js
272282
// experiment event in ./experiment.js

components/rest/CodeBlock.tsx

Lines changed: 49 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,62 @@
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+
// Only Code samples should have a copy icon - if there's a headingLang it's a code sample
12+
headingLang?: string
713
codeBlock: string
814
highlight?: string
915
}
1016

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+
1222
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>
2361
)
2462
}

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>

contributing/content-style-guide.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,7 @@ Take care to distinguish between product names and product elements. For more in
322322
| Product | Element |
323323
| --- | --- |
324324
| GitHub Actions | an action |
325+
| GitHub Codespaces | a codespace |
325326
| GitHub Packages | a package |
326327
| GitHub Pages | a GitHub Pages site |
327328

@@ -594,7 +595,7 @@ Avoid ending a sentence with a preposition unless the rewritten sentence would s
594595

595596
### Product names
596597

597-
See the “Product names” section of this guide.
598+
See the “[Product names](#product-names)” section of this guide.
598599

599600
### Terms to use or avoid
600601

data/ui.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ contribution_cta:
9090
products:
9191
graphql:
9292
reference:
93+
implements: Implements
9394
fields: Fields
9495
arguments: Arguments
9596
name: Name

0 commit comments

Comments
 (0)