Skip to content

Commit badf846

Browse files
author
R. S. Doiel
committed
Added CSS for code blocks and copyToClipboard.js for code blocks
1 parent 19924bf commit badf846

File tree

2 files changed

+99
-0
lines changed

2 files changed

+99
-0
lines changed

css/code-blocks.css

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/* code-block.css */
2+
3+
/* CSS variables for code block */
4+
:root {
5+
/* Font family for the code block */
6+
--code-font-family: monospace;
7+
8+
/* Border color for the code block */
9+
--code-border-color: #ccc;
10+
11+
/* Background color for the entire code block */
12+
--code-background-color: #f5f5f5;
13+
14+
/* Copy button variables */
15+
--copy-button-bg-color: #fff;
16+
--copy-button-border-color: #ccc;
17+
--copy-button-text-color: #000;
18+
--copy-button-hover-bg-color: #f0f0f0;
19+
}
20+
21+
pre {
22+
border: 2px solid var(--code-border-color);
23+
border-radius: 5px;
24+
background-color: var(--code-background-color);
25+
padding: 1em;
26+
margin: 0.5em 0;
27+
overflow-x: auto;
28+
max-width: 100%;
29+
position: relative;
30+
}
31+
32+
code {
33+
display: block;
34+
font-family: var(--code-font-family);
35+
line-height: 1.5;
36+
white-space: pre;
37+
}
38+
39+
.copy-button {
40+
background-color: var(--copy-button-bg-color);
41+
border: 1px solid var(--copy-button-border-color);
42+
color: var(--copy-button-text-color);
43+
border-radius: 3px;
44+
padding: 5px 10px;
45+
cursor: pointer;
46+
font-size: 14px;
47+
position: absolute;
48+
right: 10px;
49+
bottom: 10px; /* Position the button at the bottom */
50+
}
51+
52+
.copy-button:hover {
53+
background-color: var(--copy-button-hover-bg-color);
54+
}
55+
56+
.copy-button:hover::after {
57+
content: 'Copy to Clipboard';
58+
position: absolute;
59+
right: 100%;
60+
top: 50%;
61+
background-color: var(--copy-button-hover-bg-color);
62+
color: var(--copy-button-text-color);
63+
border: 1px solid var(--copy-button-border-color);
64+
border-radius: 3px;
65+
padding: 5px 10px;
66+
margin-right: 5px;
67+
white-space: nowrap;
68+
transform: translateY(-50%);
69+
}

src/copyToClipboard.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
document.addEventListener('DOMContentLoaded', function() {
2+
const preElements = document.querySelectorAll('pre');
3+
4+
preElements.forEach(pre => {
5+
const codeElement = pre.querySelector('code');
6+
if (codeElement) {
7+
const copyButton = document.createElement('button');
8+
copyButton.className = 'copy-button';
9+
copyButton.innerHTML = '📋'; // Clipboard emoji
10+
copyButton.setAttribute('title', 'Copy to Clipboard'); // Tooltip text
11+
12+
copyButton.addEventListener('click', () => {
13+
const textToCopy = codeElement.textContent;
14+
navigator.clipboard.writeText(textToCopy).then(() => {
15+
copyButton.textContent = 'Copied!';
16+
setTimeout(() => {
17+
copyButton.innerHTML = '📋';
18+
}, 2000);
19+
}).catch(err => {
20+
console.error('Failed to copy text: ', err);
21+
});
22+
});
23+
24+
pre.style.position = 'relative';
25+
pre.appendChild(copyButton);
26+
}
27+
});
28+
});
29+
30+
export {}; // This makes the file a module

0 commit comments

Comments
 (0)