Skip to content

Commit 8888221

Browse files
committed
Add copy code button functionality with styling for code blocks
1 parent 0de8b75 commit 8888221

File tree

2 files changed

+89
-0
lines changed

2 files changed

+89
-0
lines changed

assets/css/common.css

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,3 +134,48 @@ hr {
134134
border-right: none !important;
135135
margin-bottom: 1.75rem !important;
136136
}
137+
138+
/* Copy code button */
139+
.copy-code-button {
140+
position: absolute;
141+
right: 0.25rem;
142+
bottom: 0.25rem;
143+
opacity: 0;
144+
padding: 0.25rem 0.5rem;
145+
font-size: 0.75rem;
146+
line-height: normal;
147+
border: none;
148+
border-radius: 0.25rem;
149+
cursor: pointer;
150+
background-color: var(--white-1);
151+
color: var(--gray-5);
152+
transition: opacity 0.2s ease-in-out, background-color 0.2s ease-in-out;
153+
}
154+
155+
pre:hover .copy-code-button {
156+
opacity: 1;
157+
}
158+
159+
.copy-code-button:hover {
160+
background-color: var(--white-2);
161+
}
162+
163+
[data-theme="light"] .copy-code-button {
164+
background-color: var(--gray-2);
165+
color: var(--white-1);
166+
}
167+
168+
[data-theme="light"] .copy-code-button:hover {
169+
background-color: var(--gray-3);
170+
}
171+
172+
@media (prefers-color-scheme: light) {
173+
.copy-code-button {
174+
background-color: var(--gray-2);
175+
color: var(--white-1);
176+
}
177+
178+
.copy-code-button:hover {
179+
background-color: var(--gray-3);
180+
}
181+
}

assets/js/copy-code.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Function to add copy buttons to code blocks
2+
document.addEventListener("DOMContentLoaded", function () {
3+
// Find all pre elements that contain code blocks
4+
const codeBlocks = document.querySelectorAll("pre");
5+
6+
codeBlocks.forEach((codeBlock) => {
7+
// Create the copy button
8+
const copyButton = document.createElement("button");
9+
copyButton.className = "copy-code-button";
10+
copyButton.type = "button";
11+
copyButton.innerHTML = "Copy";
12+
13+
// Add the button to the code block
14+
codeBlock.appendChild(copyButton);
15+
16+
// Add position relative to the pre element for absolute positioning of the button
17+
codeBlock.style.position = "relative";
18+
19+
// Add click event listener to the button
20+
copyButton.addEventListener("click", function () {
21+
// Get the text content from the code block
22+
const code = codeBlock.querySelector("code") || codeBlock;
23+
const text = code.textContent;
24+
25+
// Copy the text to clipboard
26+
navigator.clipboard.writeText(text).then(
27+
function () {
28+
// Visual feedback - change button text temporarily
29+
copyButton.innerHTML = "Copied!";
30+
setTimeout(function () {
31+
copyButton.innerHTML = "Copy";
32+
}, 2000);
33+
},
34+
function (err) {
35+
console.error("Could not copy text: ", err);
36+
copyButton.innerHTML = "Error";
37+
setTimeout(function () {
38+
copyButton.innerHTML = "Copy";
39+
}, 2000);
40+
}
41+
);
42+
});
43+
});
44+
});

0 commit comments

Comments
 (0)