Skip to content

Commit cab7a07

Browse files
committed
Add copy feedback
1 parent 967d59b commit cab7a07

File tree

2 files changed

+33
-21
lines changed

2 files changed

+33
-21
lines changed

packages/mdx/dev/files.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import fs from "fs"
22
import { remarkCodeHike } from "../src/index"
33
import { compile } from "@mdx-js/mdx"
4-
import theme from "shiki/themes/nord.json"
4+
import theme from "shiki/themes/slack-ochin.json"
55
import { withDebugger } from "mdx-debugger"
66

77
export async function getFiles() {

packages/mdx/src/smooth-code/copy-button.tsx

Lines changed: 32 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
1+
import React from "react"
2+
13
export function CopyButton({
24
content,
35
}: {
46
content: string
57
}) {
8+
const [copied, setCopied] = React.useState(false)
9+
610
return (
711
<svg
812
style={{
@@ -11,19 +15,36 @@ export function CopyButton({
1115
margin: "0 0.8em",
1216
cursor: "pointer",
1317
}}
14-
onClick={() => copyToClipboard(content)}
18+
onClick={() => {
19+
copyToClipboard(content)
20+
setCopied(true)
21+
setTimeout(() => {
22+
setCopied(false)
23+
}, 1200)
24+
}}
25+
className="ch-copy-button"
1526
fill="none"
1627
stroke="currentColor"
1728
viewBox="0 0 24 24"
1829
xmlns="http://www.w3.org/2000/svg"
1930
>
2031
<title>Copy</title>
21-
<path
22-
strokeLinecap="round"
23-
strokeLinejoin="round"
24-
strokeWidth="2"
25-
d="M8 16H6a2 2 0 01-2-2V6a2 2 0 012-2h8a2 2 0 012 2v2m-6 12h8a2 2 0 002-2v-8a2 2 0 00-2-2h-8a2 2 0 00-2 2v8a2 2 0 002 2z"
26-
></path>
32+
33+
{copied ? (
34+
<path
35+
strokeLinecap="round"
36+
strokeLinejoin="round"
37+
strokeWidth={2}
38+
d="M5 13l4 4L19 7"
39+
/>
40+
) : (
41+
<path
42+
strokeLinecap="round"
43+
strokeLinejoin="round"
44+
strokeWidth="2"
45+
d="M8 16H6a2 2 0 01-2-2V6a2 2 0 012-2h8a2 2 0 012 2v2m-6 12h8a2 2 0 002-2v-8a2 2 0 00-2-2h-8a2 2 0 00-2 2v8a2 2 0 002 2z"
46+
/>
47+
)}
2748
</svg>
2849
)
2950
}
@@ -33,16 +54,7 @@ function copyToClipboard(text: string) {
3354
fallbackCopyTextToClipboard(text)
3455
return
3556
}
36-
navigator.clipboard.writeText(text).then(
37-
function () {
38-
console.log(
39-
"Async: Copying to clipboard was successful!"
40-
)
41-
},
42-
function (err) {
43-
console.error("Async: Could not copy text: ", err)
44-
}
45-
)
57+
navigator.clipboard.writeText(text)
4658
}
4759

4860
function fallbackCopyTextToClipboard(text: string) {
@@ -60,10 +72,10 @@ function fallbackCopyTextToClipboard(text: string) {
6072

6173
try {
6274
var successful = document.execCommand("copy")
63-
var msg = successful ? "successful" : "unsuccessful"
64-
console.log("Fallback: Copying text command was " + msg)
75+
// var msg = successful ? "successful" : "unsuccessful"
76+
// console.log("Fallback: Copying text command was " + msg)
6577
} catch (err) {
66-
console.error("Fallback: Oops, unable to copy", err)
78+
// console.error("Fallback: Oops, unable to copy", err)
6779
}
6880

6981
document.body.removeChild(textArea)

0 commit comments

Comments
 (0)