Skip to content

Commit 4b3ad26

Browse files
committed
Update copy to clipboard button and add it to commands as well
1 parent 7b0985e commit 4b3ad26

File tree

3 files changed

+50
-39
lines changed

3 files changed

+50
-39
lines changed

layouts/commands/list.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ <h1 class="truncate font-mono font-medium text-lg mb-1.5 px-6">
132132
{{ end }}
133133
</div>
134134
</div>
135-
136135
{{ partial "commands-nav.html" . }}
136+
{{ partial "scripts.html" . }}
137137
</main>
138138
{{ end }}

layouts/commands/single.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,6 @@ <h2>History</h2>
130130
</dl>
131131
{{ end }}
132132
</aside>
133-
133+
{{ partial "scripts.html" . }}
134134
</main>
135135
{{ end }}

layouts/partials/scripts.html

Lines changed: 48 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,59 @@
11
<!-- Code for copy to clipboard button -->
22
<script>
33
document.addEventListener("DOMContentLoaded", function () {
4-
document.querySelectorAll('div.highlight').forEach(block => {
5-
// Create a wrapper for the button and message
6-
const wrapper = document.createElement('div');
7-
wrapper.style = 'position:absolute;top:10px;right:10px;z-index:1;display:flex;align-items:center;gap:6px;';
8-
9-
// Create the copy button
10-
const button = document.createElement('button');
11-
button.className = 'copy-button';
12-
button.innerHTML = `
13-
<svg viewBox="0 0 24 24" width="18" height="18" fill="none" xmlns="http://www.w3.org/2000/svg">
14-
<path fill-rule="evenodd" clip-rule="evenodd" d="M21 8C21 6.34315 19.6569 5 18 5H10C8.34315 5 7 6.34315 7 8V20C7 21.6569 8.34315 23 10 23H18C19.6569 23 21 21.6569 21 20V8ZM19 8C19 7.44772 18.5523 7 18 7H10C9.44772 7 9 7.44772 9 8V20C9 20.5523 9.44772 21 10 21H18C18.5523 21 19 20.5523 19 20V8Z" fill="#0F0F0F"/>
15-
<path d="M6 3H16C16.5523 3 17 2.55228 17 2C17 1.44772 16.5523 1 16 1H6C4.34315 1 3 2.34315 3 4V18C3 18.5523 3.44772 19 4 19C4.55228 19 5 18.5523 5 18V4C5 3.44772 5.44772 3 6 3Z" fill="#0F0F0F"/>
4+
5+
// don't run script for tabbed code examples
6+
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')) {
7+
document.querySelectorAll('div.highlight').forEach(block => {
8+
// Create a wrapper for the button and message
9+
const wrapper = document.createElement('div');
10+
wrapper.style = 'position:absolute;top:10px;right:10px;z-index:1;display:flex;align-items:center;gap:6px;';
11+
12+
// Create the copy button
13+
const button = document.createElement('button');
14+
button.innerHTML = `
15+
<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">
16+
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 48 48" fill="currentColor">
17+
<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>
1618
</svg>
1719
`;
1820

19-
// Create the "Copied!" message span
20-
const message = document.createElement('span');
21-
message.className = 'copied-message';
22-
message.innerText = 'Copied to clipboard!';
23-
message.style = 'opacity:0;transition:opacity 0.3s ease;font-size:0.75em;color:white;';
24-
25-
// Handle copy logic
26-
button.addEventListener('click', () => {
27-
const lines = block.querySelectorAll('span.cl');
28-
const text = Array.from(lines).map(line => line.textContent).join('');
29-
30-
navigator.clipboard.writeText(text).then(() => {
31-
message.style.opacity = '1';
32-
setTimeout(() => {
33-
message.style.opacity = '0';
34-
}, 2000);
35-
}).catch(err => {
36-
console.error("Copy failed", err);
21+
// Create the tooltip container
22+
const tooltipContainer = document.createElement('div');
23+
tooltipContainer.className = 'tooltip relative inline-block';
24+
25+
// Create the tooltip span
26+
const tooltip = document.createElement('span');
27+
tooltip.className = 'tooltiptext hidden bg-slate-200 rounded rounded-mx text-slate-800 text-center text-xs p-1 absolute';
28+
tooltip.style.bottom = '-0.6rem';
29+
tooltip.style.left = '-5.5rem';
30+
tooltip.textContent = 'Copied!';
31+
32+
tooltipContainer.appendChild(tooltip);
33+
button.appendChild(tooltipContainer);
34+
35+
// Handle copy logic
36+
button.addEventListener('click', () => {
37+
const lines = block.querySelectorAll('span.cl');
38+
const text = Array.from(lines).map(line => line.textContent).join('');
39+
40+
navigator.clipboard.writeText(text).then(() => {
41+
tooltip.style.display = 'block';
42+
setTimeout(() => {
43+
tooltip.style.display = 'none';
44+
}, 2000);
45+
}).catch(err => {
46+
console.error("Copy failed", err);
47+
});
3748
});
38-
});
3949

40-
// Append button and message
41-
wrapper.appendChild(message);
42-
wrapper.appendChild(button);
43-
block.style.position = 'relative';
44-
block.appendChild(wrapper);
45-
});
50+
// Append button and message
51+
wrapper.appendChild(button);
52+
wrapper.appendChild(tooltipContainer);
53+
block.style.position = 'relative';
54+
block.appendChild(wrapper);
55+
});
56+
}
4657
});
4758
</script>
4859

0 commit comments

Comments
 (0)