Skip to content

Commit a5cfe2a

Browse files
Add copy buttons to snipplets
1 parent bf8139f commit a5cfe2a

3 files changed

Lines changed: 69 additions & 6 deletions

File tree

css/style.css

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,40 @@ code {
264264
box-shadow: 0 0 15px rgba(16, 185, 129, 0.5);
265265
}
266266

267+
/* Code block copy button */
268+
.code-block {
269+
position: relative;
270+
}
271+
272+
.btn-code-copy {
273+
position: absolute;
274+
top: 50%;
275+
transform: translateY(-50%);
276+
right: 0.75rem;
277+
background: #1a1d27;
278+
border: 1px solid rgba(255, 255, 255, 0.25);
279+
color: var(--text-primary);
280+
border-radius: 5px;
281+
width: 28px;
282+
height: 28px;
283+
cursor: pointer;
284+
display: flex;
285+
align-items: center;
286+
justify-content: center;
287+
opacity: 0;
288+
transition: opacity 0.15s, background 0.15s, color 0.15s, border-color 0.15s;
289+
}
290+
291+
.code-block:hover .btn-code-copy {
292+
opacity: 1;
293+
}
294+
295+
.btn-code-copy:hover {
296+
background: var(--primary-accent);
297+
color: #000;
298+
border-color: var(--primary-accent);
299+
}
300+
267301
/* Terminal Demo Window */
268302
.terminal-window {
269303
background: #09090b;
@@ -744,9 +778,6 @@ footer {
744778
margin-bottom: 1rem;
745779
}
746780

747-
.btn-copy {
748-
width: 100%;
749-
}
750781

751782
.grid-3 {
752783
grid-template-columns: 1fr;

index.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ <h2>Quick Start</h2>
138138
<pre><code>luca install tuist/tuist@4.78.0 --asset tuist.zip</code></pre>
139139
</div>
140140

141-
<div class="example card">
141+
<div class="example card no-copy">
142142
<div class="example-title">
143143
<span class="command">Step 2: Use Your Tools</span>
144144
</div>
@@ -294,7 +294,7 @@ <h2>Additional Features</h2>
294294
</div>
295295
</div>
296296
<div class="command-examples grid-3">
297-
<div class="example card">
297+
<div class="example card no-copy">
298298
<div class="example-title">
299299
<span class="command">List Installed</span>
300300
</div>
@@ -311,7 +311,7 @@ <h2>Additional Features</h2>
311311
tuist:
312312
- 4.78.0</code></pre>
313313
</div>
314-
<div class="example card">
314+
<div class="example card no-copy">
315315
<div class="example-title">
316316
<span class="command">List Linked</span>
317317
</div>

js/main.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,40 @@ function copyInstallCommand() {
2020
});
2121
}
2222

23+
const CLIPBOARD_ICON = `<svg xmlns="http://www.w3.org/2000/svg" width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><rect x="8" y="8" width="12" height="12" rx="2"/><path d="M16 8V6a2 2 0 0 0-2-2H6a2 2 0 0 0-2 2v8a2 2 0 0 0 2 2h2"/></svg>`;
24+
const CHECK_ICON = `<svg xmlns="http://www.w3.org/2000/svg" width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.5" stroke-linecap="round" stroke-linejoin="round"><polyline points="20 6 9 17 4 12"></polyline></svg>`;
25+
2326
// Add smooth scrolling for anchor links
2427
document.addEventListener('DOMContentLoaded', () => {
28+
// Inject copy buttons into all pre elements inside cards
29+
document.querySelectorAll('.card:not(.no-copy) pre').forEach(pre => {
30+
const wrapper = document.createElement('div');
31+
wrapper.className = 'code-block';
32+
pre.parentNode.insertBefore(wrapper, pre);
33+
wrapper.appendChild(pre);
34+
35+
const btn = document.createElement('button');
36+
btn.className = 'btn-code-copy';
37+
btn.title = 'Copy to clipboard';
38+
btn.innerHTML = CLIPBOARD_ICON;
39+
wrapper.appendChild(btn);
40+
41+
btn.addEventListener('click', () => {
42+
const text = pre.querySelector('code') ? pre.querySelector('code').textContent : pre.textContent;
43+
navigator.clipboard.writeText(text);
44+
btn.innerHTML = CHECK_ICON;
45+
btn.style.color = '#10b981';
46+
btn.style.borderColor = '#10b981';
47+
btn.style.background = 'rgba(16, 185, 129, 0.15)';
48+
setTimeout(() => {
49+
btn.innerHTML = CLIPBOARD_ICON;
50+
btn.style.color = '';
51+
btn.style.borderColor = '';
52+
btn.style.background = '';
53+
}, 2000);
54+
});
55+
});
56+
2557
// Add animation for features on scroll
2658
const features = document.querySelectorAll('.feature');
2759

0 commit comments

Comments
 (0)