Skip to content

Commit 38518b9

Browse files
committed
Add copy button to outputs, and make them prettier.
1 parent ff6f7fa commit 38518b9

File tree

3 files changed

+416
-675
lines changed

3 files changed

+416
-675
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,7 @@ dist-ssr
2222
*.njsproj
2323
*.sln
2424
*.sw?
25+
26+
# package handlers.
27+
package-lock.json
28+
yarn.lock

src/components/CodeBlock.tsx

Lines changed: 39 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
import { Card } from "react-bootstrap";
1+
import { Button, Card } from "react-bootstrap";
22
import SyntaxHighlighter from "react-syntax-highlighter";
3+
import { CopyToClipboard } from "react-copy-to-clipboard";
4+
import { useState } from "react";
35

46
export default function CodeBlock({
57
code,
@@ -16,27 +18,42 @@ export default function CodeBlock({
1618
wrapLines?: boolean;
1719
wrapLongLines?: boolean;
1820
}) {
21+
const [copied, setCopied] = useState(false);
22+
23+
// Reset the copied state after 2 seconds.
24+
if (copied) {
25+
setTimeout(() => {
26+
setCopied(false);
27+
}, 2000);
28+
}
29+
1930
return (
20-
<Card bg={"secondary"} text="white" className="mt-2">
21-
<Card.Body>
22-
<Card.Text> {title}</Card.Text>
23-
<SyntaxHighlighter
24-
language={language}
25-
style={style}
26-
wrapLines={wrapLines}
27-
wrapLongLines={wrapLongLines}
28-
showLineNumbers={true}
29-
showInlineLineNumbers={false}
30-
header={"Code"}
31-
customStyle={{
32-
border: "1px solid #c3c3c3",
33-
borderRadius: "5px",
34-
marginBottom: 0,
35-
}}
36-
>
37-
{code}
38-
</SyntaxHighlighter>
39-
</Card.Body>
40-
</Card>
31+
<div className="bg-primary mt-2 rounded">
32+
<div className="d-flex flex-row justify-content-between align-items-center p-2">
33+
<h6 className="mb-0 ml-2 text-white"> {title}</h6>
34+
<CopyToClipboard text={code}>
35+
<a className="btn btn-primary" onClick={() => setCopied(true)}>
36+
<small>{copied ? "Gekopieerd" : "Kopieer"}</small>
37+
</a>
38+
</CopyToClipboard>
39+
</div>
40+
41+
<SyntaxHighlighter
42+
language={language}
43+
style={style}
44+
wrapLines={wrapLines}
45+
wrapLongLines={wrapLongLines}
46+
showLineNumbers={true}
47+
showInlineLineNumbers={false}
48+
header={"Code"}
49+
customStyle={{
50+
border: "1px solid #c3c3c3",
51+
borderRadius: "5px",
52+
marginBottom: 0,
53+
}}
54+
>
55+
{code}
56+
</SyntaxHighlighter>
57+
</div>
4158
);
4259
}

0 commit comments

Comments
 (0)