Skip to content

Commit d1b645e

Browse files
feat: implement copy button UI (#7698)
1 parent 13a1396 commit d1b645e

File tree

3 files changed

+148
-0
lines changed

3 files changed

+148
-0
lines changed
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import { useRef, useState } from 'react';
2+
import PropTypes from 'prop-types';
3+
import './CodeBlockWithCopy.scss';
4+
5+
export default function CodeBlockWithCopy({ children }) {
6+
const preRef = useRef(null);
7+
const [copyStatus, setCopyStatus] = useState('copy');
8+
9+
const handleCopy = async () => {
10+
if (!preRef.current) return;
11+
12+
const codeElement = preRef.current.querySelector('code');
13+
if (!codeElement) return;
14+
15+
const codeText = codeElement.textContent;
16+
let successfulCopy = false;
17+
18+
// Try modern API (navigator.clipboard) -> as document.execCommand() deprecated
19+
try {
20+
if (navigator.clipboard && window.isSecureContext) {
21+
await navigator.clipboard.writeText(codeText);
22+
successfulCopy = true;
23+
}
24+
} catch (err) {
25+
console.log(err);
26+
}
27+
28+
// If modern API failed, fall back to deprecated document.execCommand('copy')
29+
if (!successfulCopy) {
30+
const textarea = document.createElement('textarea');
31+
textarea.value = codeText;
32+
textarea.style.position = 'fixed';
33+
textarea.style.opacity = '0';
34+
35+
document.body.appendChild(textarea);
36+
textarea.select();
37+
38+
try {
39+
// This deprecated method is kept as a fallback for compatibility/iframe environments.
40+
successfulCopy = document.execCommand('copy');
41+
} catch (err) {
42+
successfulCopy = false;
43+
console.log(err);
44+
}
45+
46+
document.body.removeChild(textarea);
47+
}
48+
49+
setCopyStatus(successfulCopy ? 'copied' : 'error');
50+
setTimeout(() => setCopyStatus('copy'), 2000);
51+
};
52+
53+
return (
54+
<div className="code-block-wrapper">
55+
<button onClick={handleCopy} className={`copy-button ${copyStatus}`}>
56+
{copyStatus === 'copied'
57+
? 'Copied!'
58+
: copyStatus === 'error'
59+
? 'Error'
60+
: 'Copy'}
61+
</button>
62+
63+
<pre ref={preRef} className="code-block">
64+
{children}
65+
</pre>
66+
</div>
67+
);
68+
}
69+
70+
CodeBlockWithCopy.propTypes = {
71+
children: PropTypes.node.isRequired,
72+
};
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
.code-block-wrapper {
2+
position: relative;
3+
margin-bottom: 1.5rem;
4+
}
5+
6+
.code-block {
7+
background-color: #2d3748;
8+
color: #e2e8f0;
9+
padding: 1rem;
10+
padding-right: 3.5rem;
11+
border-radius: 0.5rem;
12+
overflow-x: auto;
13+
font-size: 0.875rem;
14+
line-height: 1.5;
15+
box-shadow: inset 0 2px 4px rgba(0, 0, 0, 0.1);
16+
17+
code {
18+
font-family: monospace;
19+
}
20+
}
21+
22+
.copy-button {
23+
position: absolute;
24+
top: 0.32rem;
25+
right: 0.5rem;
26+
z-index: 10;
27+
28+
padding: 0.4rem 0.7rem;
29+
border-radius: 0.35rem;
30+
31+
border: none;
32+
cursor: pointer;
33+
34+
font-size: 0.75rem;
35+
font-weight: 500;
36+
37+
/* Always visible */
38+
opacity: 1;
39+
40+
background-color: #7c3aed;
41+
color: #e2e8f0;
42+
43+
transition:
44+
background-color 0.2s,
45+
transform 0.1s;
46+
47+
&:hover {
48+
background-color: #6d28d9;
49+
}
50+
51+
/* Success */
52+
&.copied {
53+
background-color: #38a169;
54+
}
55+
&.copied:hover {
56+
background-color: #2f855a;
57+
}
58+
59+
/* Error */
60+
&.error {
61+
background-color: #e53e3e;
62+
}
63+
&.error:hover {
64+
background-color: #c53030;
65+
}
66+
67+
&:focus {
68+
outline: none;
69+
}
70+
71+
&:active {
72+
transform: scale(0.95);
73+
}
74+
}

src/mdx-components.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import Badge from './components/Badge/Badge';
22
import LinkComponent from './components/mdxComponents/Link';
33
import StackBlitzPreview from './components/StackBlitzPreview/StackBlitzPreview';
4+
import CodeBlockWithCopy from './components/CodeBlockWithCopy/CodeBlockWithCopy';
45

56
/** @returns {import('mdx/types.js').MDXComponents} */
67
export function useMDXComponents() {
78
return {
89
a: LinkComponent,
910
Badge: Badge,
1011
StackBlitzPreview: StackBlitzPreview,
12+
pre: CodeBlockWithCopy,
1113
};
1214
}

0 commit comments

Comments
 (0)