Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions docs/css/copy-code.css
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
.code-block-wrapper {
position: relative;
}

/* Base button placement & visibility */
.copy-btn {
position: absolute;
Expand All @@ -16,11 +20,12 @@
align-items: center;
justify-content: center;

color: #666; /* default icon color */
color: #666;
/* default icon color */
}

/* Show button only when the code block is hovered */
pre:hover .copy-btn {
.code-block-wrapper:hover .copy-btn {
opacity: 1;
transform: translateY(0);
}
Comment thread
vkarpov15 marked this conversation as resolved.
Expand All @@ -40,4 +45,4 @@ pre:hover .copy-btn {
width: 20px;
height: 20px;
transition: color 0.2s ease;
}
}
13 changes: 9 additions & 4 deletions docs/js/copy-code.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,21 @@ document.addEventListener('DOMContentLoaded', () => {

// Inject a copy button into each <pre><code> block used in docs
document.querySelectorAll('pre code').forEach(block => {
const wrapper = block.parentElement;
wrapper.style.position = 'relative'; // ensures button can be positioned correctly
const pre = block.parentElement;

const wrapper = document.createElement('div');
wrapper.className = 'code-block-wrapper';

pre.parentNode.insertBefore(wrapper, pre);
wrapper.appendChild(pre);

const btn = document.createElement('button');
btn.className = 'copy-btn';
btn.setAttribute('aria-label', 'Copy code to clipboard'); // accessibility support
btn.innerHTML = copyIcon; // initial icon state

// Handle the copy-to-clipboard action
btn.onclick = async() => {
btn.onclick = async () => {
if (btn.dataset.locked) return; // prevents multiple fast clicks
btn.dataset.locked = '1';

Expand All @@ -48,4 +53,4 @@ document.addEventListener('DOMContentLoaded', () => {

wrapper.appendChild(btn); // attach button to code block
});
});
});