Skip to content

Commit 8c987c2

Browse files
author
OpenClaw Sync
committed
Fix copy button and back-to-top: better DOM handling, cleaner styling
1 parent 7257677 commit 8c987c2

2 files changed

Lines changed: 31 additions & 33 deletions

File tree

assets/css/main.css

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -405,45 +405,39 @@ main {
405405
.highlight .gd { color: #e06c75; } /* diff deleted */
406406
.highlight .gi { color: #98c379; } /* diff inserted */
407407

408-
/* Code block wrapper (added by JS) */
409-
.code-block {
410-
position: relative;
411-
margin: 1.25rem 0;
412-
}
413-
414-
.code-block pre {
415-
margin: 0;
416-
}
417-
408+
/* Copy button */
418409
.copy-btn {
419410
position: absolute;
420-
top: 0.5rem;
421-
right: 0.5rem;
422-
padding: 0.25rem 0.6rem;
423-
border: 1px solid rgba(255, 255, 255, 0.15);
411+
top: 0.6rem;
412+
right: 0.6rem;
413+
padding: 0.3rem 0.65rem;
414+
border: none;
424415
border-radius: 4px;
425-
background: rgba(255, 255, 255, 0.08);
426-
color: rgba(255, 255, 255, 0.5);
416+
background: rgba(255, 255, 255, 0.1);
417+
color: rgba(255, 255, 255, 0.4);
427418
font-family: "IBM Plex Mono", monospace;
428419
font-size: 0.7rem;
429-
letter-spacing: 0.04em;
420+
letter-spacing: 0.05em;
421+
text-transform: uppercase;
430422
cursor: pointer;
431423
opacity: 0;
432424
transition: opacity 150ms ease, background 150ms ease, color 150ms ease;
425+
z-index: 2;
433426
}
434427

435-
.code-block:hover .copy-btn {
428+
.highlight:hover .copy-btn,
429+
pre:hover .copy-btn {
436430
opacity: 1;
437431
}
438432

439433
.copy-btn:hover {
440-
background: rgba(255, 255, 255, 0.15);
441-
color: rgba(255, 255, 255, 0.8);
434+
background: rgba(255, 255, 255, 0.18);
435+
color: rgba(255, 255, 255, 0.7);
442436
}
443437

444438
.copy-btn.copied {
445439
color: #98c379;
446-
border-color: rgba(152, 195, 121, 0.3);
440+
background: rgba(152, 195, 121, 0.12);
447441
}
448442

449443
/* Back to top */

assets/js/main.js

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,23 @@
11
// Code block copy button
22
document.addEventListener('DOMContentLoaded', () => {
3-
document.querySelectorAll('.post-content pre').forEach(pre => {
3+
document.querySelectorAll('.post-content .highlight, .post-content pre').forEach(el => {
4+
// Avoid duplicates: if this pre is inside a .highlight we already processed, skip
5+
if (el.tagName === 'PRE' && el.closest('.highlight')) return;
6+
7+
const container = el;
8+
container.style.position = 'relative';
9+
410
const btn = document.createElement('button');
511
btn.className = 'copy-btn';
612
btn.textContent = 'Copy';
713
btn.setAttribute('aria-label', 'Copy code to clipboard');
814

915
btn.addEventListener('click', async () => {
10-
const code = pre.querySelector('code')
11-
? pre.querySelector('code').textContent
12-
: pre.textContent;
16+
const code = container.querySelector('code');
17+
const text = code ? code.textContent : container.textContent;
1318

1419
try {
15-
await navigator.clipboard.writeText(code);
20+
await navigator.clipboard.writeText(text.trim());
1621
btn.textContent = 'Copied!';
1722
btn.classList.add('copied');
1823
setTimeout(() => {
@@ -25,12 +30,7 @@ document.addEventListener('DOMContentLoaded', () => {
2530
}
2631
});
2732

28-
// Wrap pre in a container for positioning
29-
const wrapper = document.createElement('div');
30-
wrapper.className = 'code-block';
31-
pre.parentNode.insertBefore(wrapper, pre);
32-
wrapper.appendChild(pre);
33-
wrapper.appendChild(btn);
33+
container.appendChild(btn);
3434
});
3535

3636
// Back to top button
@@ -42,7 +42,11 @@ document.addEventListener('DOMContentLoaded', () => {
4242
document.body.appendChild(topBtn);
4343

4444
const toggleTopBtn = () => {
45-
topBtn.classList.toggle('visible', window.scrollY > 600);
45+
if (window.scrollY > 400) {
46+
topBtn.classList.add('visible');
47+
} else {
48+
topBtn.classList.remove('visible');
49+
}
4650
};
4751

4852
window.addEventListener('scroll', toggleTopBtn, { passive: true });

0 commit comments

Comments
 (0)