Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add: Copy buttons to code section #1843

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
23 changes: 23 additions & 0 deletions docs/main.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,26 @@ document.querySelectorAll('.copy-icon').forEach(icon => {
}, 750)
})
})

const preTags = document.querySelectorAll('pre');

// Function to add copy buttons
preTags.forEach(pre => {
const copyButton = document.createElement('button');
copyButton.className = 'copy-button';
copyButton.innerHTML = `<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-copy"><rect width="14" height="14" x="8" y="8" rx="2" ry="2"/><path d="M4 16c-1.1 0-2-.9-2-2V4c0-1.1.9-2 2-2h10c1.1 0 2 .9 2 2"/></svg>`;

pre.appendChild(copyButton);
copyButton.addEventListener('click', () => {
const codeToCopy = pre.innerText;
navigator.clipboard.writeText(codeToCopy).then(() => {
// copyButton.innerText = 'Copied!';
copyButton.innerHTML = `<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="#0900ff" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-copy-check"><path d="m12 15 2 2 4-4"/><rect width="14" height="14" x="8" y="8" rx="2" ry="2"/><path d="M4 16c-1.1 0-2-.9-2-2V4c0-1.1.9-2 2-2h10c1.1 0 2 .9 2 2"/></svg>`;
setTimeout(() => {
copyButton.innerHTML = `<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-copy"><rect width="14" height="14" x="8" y="8" rx="2" ry="2"/><path d="M4 16c-1.1 0-2-.9-2-2V4c0-1.1.9-2 2-2h10c1.1 0 2 .9 2 2"/></svg>`;
}, 1500);
}).catch(err => {
console.error('Failed to copy:', err);
});
});
});
14 changes: 13 additions & 1 deletion docs/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,24 @@ code {

pre {
padding: 1rem;

position: relative;
background-color: var(--c-dark);
border-radius: 0.25em;
color: #fff;
}

.copy-button{
position: absolute;
top: 0;
right: 0;
padding: 0.5em 1em;
color: #fff;
border: none;
border-radius: 0 0 0 0.25em;
cursor: pointer;
background: none;
}

pre code {
background-color: transparent;
font-size: 0.8rem !important;
Expand Down