Skip to content

Add copy to clipboard for code snippets #1548

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

Merged
merged 4 commits into from
May 19, 2025
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
21 changes: 21 additions & 0 deletions assets/css/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -1073,4 +1073,25 @@ a[href*="#no-click"], img[src*="#no-click"] {

.dd-item .highlight:hover {
color: #5961ff;
}

.copy-button {
background-color: #eee;
border: none;
padding: 4px;
cursor: pointer;
border-radius: 4px;
display: flex;
align-items: center;
justify-content: center;
}

.copy-button svg {
width: 16px;
height: 16px;
fill: #333;
}

.copy-button:hover {
background-color: #ddd;
}
2 changes: 1 addition & 1 deletion layouts/commands/list.html
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ <h1 class="truncate font-mono font-medium text-lg mb-1.5 px-6">
{{ end }}
</div>
</div>

{{ partial "commands-nav.html" . }}
{{ partial "scripts.html" . }}
</main>
{{ end }}
2 changes: 1 addition & 1 deletion layouts/commands/single.html
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,6 @@ <h2>History</h2>
</dl>
{{ end }}
</aside>

{{ partial "scripts.html" . }}
</main>
{{ end }}
1 change: 1 addition & 0 deletions layouts/develop/list.html
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,6 @@ <h1>
</section>
</div>
{{ partial "docs-toc.html" . }}
{{ partial "scripts.html" . }}
</main>
{{ end }}
1 change: 1 addition & 0 deletions layouts/develop/single.html
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,6 @@ <h1>
</section>
</div>
{{ partial "docs-toc.html" . }}
{{ partial "scripts.html" . }}
</main>
{{ end }}
2 changes: 1 addition & 1 deletion layouts/integrate/list.html
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ <h1 class="text-xl leading-tight px-6 py-4">
{{ end }}
</div>
</div>

{{ partial "commands-nav.html" . }}
{{ partial "scripts.html" . }}
</main>
{{ end }}
1 change: 1 addition & 0 deletions layouts/integrate/single.html
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,6 @@ <h1>{{ .Title }}</h1>
</section>
</div>
{{ partial "docs-toc.html" . }}
{{ partial "scripts.html" . }}
</main>
{{ end }}
1 change: 1 addition & 0 deletions layouts/integration/list.html
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,6 @@ <h1>{{ .Title }}</h1>
</section>
</div>
{{ partial "docs-toc.html" . }}
{{ partial "scripts.html" . }}
</main>
{{ end }}
1 change: 1 addition & 0 deletions layouts/integration/single.html
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,6 @@ <h1>{{ .Title }}</h1>
</section>
</div>
{{ partial "docs-toc.html" . }}
{{ partial "scripts.html" . }}
</main>
{{ end }}
65 changes: 64 additions & 1 deletion layouts/partials/scripts.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,68 @@
<!-- Code for version selectors -->
<!-- Code for copy to clipboard button -->
<script>
document.addEventListener("DOMContentLoaded", function () {

// don't run script for tabbed code examples
if (!document.querySelector('.codetabs.cli.group.flex.justify-start.items-center.flex-wrap.box-border.rounded-lg.mt-0.mb-0.mx-auto.bg-slate-900')) {
document.querySelectorAll('div.highlight').forEach(block => {
// Create a wrapper for the button and message
const wrapper = document.createElement('div');

if (block.parentElement.classList.contains("expand-content")) {
wrapper.style = 'position:absolute;top:10px;right:20px;z-index:1;display:flex;align-items:center;gap:6px;';
} else {
wrapper.style = 'position:absolute;top:10px;right:10px;z-index:1;display:flex;align-items:center;gap:6px;';
}
// Create the copy button
const button = document.createElement('button');
button.innerHTML = `
<button class="clipboard-button text-neutral-400 hover:text-slate-100 bg-slate-600 relative h-7 w-7 p-1 rounded rounded-mx" title="Copy to clipboard">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 48 48" fill="currentColor">
<path d="M9 43.95q-1.2 0-2.1-.9-.9-.9-.9-2.1V10.8h3v30.15h23.7v3Zm6-6q-1.2 0-2.1-.9-.9-.9-.9-2.1v-28q0-1.2.9-2.1.9-.9 2.1-.9h22q1.2 0 2.1.9.9.9.9 2.1v28q0 1.2-.9 2.1-.9.9-2.1.9Zm0-3h22v-28H15v28Zm0 0v-28 28Z"></path>
</svg>
`;

// Create the tooltip container
const tooltipContainer = document.createElement('div');
tooltipContainer.className = 'tooltip relative inline-block';

// Create the tooltip span
const tooltip = document.createElement('span');
tooltip.className = 'tooltiptext hidden bg-slate-200 rounded rounded-mx text-slate-800 text-center text-xs p-1 absolute';
tooltip.style.bottom = '-0.6rem';
tooltip.style.left = '-5.5rem';
tooltip.textContent = 'Copied!';

tooltipContainer.appendChild(tooltip);
button.appendChild(tooltipContainer);

// Handle copy logic
button.addEventListener('click', () => {
const lines = block.querySelectorAll('span.cl');
const text = Array.from(lines).map(line => line.textContent).join('');

navigator.clipboard.writeText(text).then(() => {
tooltip.style.display = 'block';
setTimeout(() => {
tooltip.style.display = 'none';
}, 2000);
}).catch(err => {
console.error("Copy failed", err);
});
});

// Append button and message
wrapper.appendChild(button);
wrapper.appendChild(tooltipContainer);
block.style.position = 'relative';
block.appendChild(wrapper);
});
}
});
</script>


<!-- Code for version selectors -->
<script>
window.onload = function(){
var currentUrl = window.location.href
Expand Down