Skip to content

Commit 1a981a6

Browse files
authored
fix(CodeBlock): update copy states (#1018)
1 parent 60bb924 commit 1a981a6

File tree

4 files changed

+65
-27
lines changed

4 files changed

+65
-27
lines changed

assets/images/icon-copy.svg

Lines changed: 1 addition & 0 deletions
Loading

assets/javascripts/new-javascripts/application.js

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -45,28 +45,41 @@ document.addEventListener('DOMContentLoaded', function () {
4545
if (navigator && navigator.clipboard) {
4646
const codeBlocks = document.querySelectorAll(
4747
':is([class^="language-"] pre.highlight, .code-box pre code)',
48-
)
48+
);
49+
50+
const copyIcon = `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" width="24" height="24" fill="currentColor"><path d="M16 1H4c-1.1 0-2 .9-2 2v14h2V3h12V1zm3 4H8c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h11c1.1 0 2-.9 2-2V7c0-1.1-.9-2-2-2zm0 16H8V7h11v14z"/></svg>`;
51+
const copiedIcon = `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" width="24" height="24" fill="currentColor"><path d="M9 16.2L4.8 12l-1.4 1.4L9 19 21 7l-1.4-1.4L9 16.2z"/></svg>`;
4952

5053
codeBlocks.forEach((codeBlock) => {
51-
const button = document.createElement('button')
52-
const codeElement = codeBlock.querySelector('code') || codeBlock
53-
const container = codeBlock.parentElement
54+
const button = document.createElement('button');
55+
const codeElement = codeBlock.querySelector('code') || codeBlock;
56+
const container = codeBlock.parentElement;
5457

55-
container.style.position = 'relative'
56-
container.appendChild(button)
57-
button.innerText = 'copy'
58+
button.classList.add('copy-button');
59+
container.appendChild(button);
60+
button.innerHTML = copyIcon
5861

5962
button.addEventListener('mousedown', async () => {
60-
const originalText = button.innerText
63+
const originalIcon = copyIcon; // Store the original icon
6164

62-
await navigator.clipboard.writeText(codeElement.innerText)
63-
button.innerText = 'copied!'
65+
try {
66+
await navigator.clipboard.writeText(codeElement.innerText);
6467

65-
setTimeout(() => {
66-
button.innerText = originalText
67-
}, 1000)
68-
})
69-
})
68+
button.classList.add('copied');
69+
button.innerHTML = copiedIcon;
70+
71+
setTimeout(() => {
72+
button.classList.remove('copied');
73+
button.innerHTML = originalIcon;
74+
}, 1000);
75+
} catch (err) {
76+
console.error('Failed to copy text: ', err);
77+
setTimeout(() => {
78+
button.innerHTML = originalIcon; // Revert to original icon
79+
}, 1000);
80+
}
81+
});
82+
});
7083
}
7184

7285
// Check for reduced motion setting

assets/stylesheets/new-stylesheets/_syntax.scss

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,43 @@
33
position: relative;
44

55
button {
6-
background-color: #c5c5c5;
7-
color: white;
8-
border: 0;
9-
padding: 5px 8px;
10-
font-family: sans-serif;
11-
transition: background-color 0.2s;
12-
cursor: pointer;
136
position: absolute;
14-
top: 0;
15-
right: 0;
7+
top: 1em;
8+
right: 1em;
9+
background: none;
10+
border: medium;
11+
cursor: pointer;
12+
padding: 7px 6px;
13+
border-radius: 6px;
14+
transition: all 0.2s ease-in-out;
1615
display: none;
17-
border-radius: 0 10px 0 0;
16+
background-color: var(--color-syntax-clipboard-bg);
17+
18+
svg {
19+
width: 20px;
20+
height: 20px;
21+
opacity: 0.8;
22+
}
23+
24+
&.copied {
25+
svg {
26+
color: var(--color-syntax-clipboard-check-color);
27+
}
28+
}
29+
1830

1931
&:hover {
20-
background-color: #868686;
32+
background-color: var(--color-syntax-clipboard-hover-bg);
33+
34+
svg {
35+
opacity: 1;
36+
}
2137
}
2238
}
2339

2440
&:hover {
2541
button {
26-
display: block;
42+
display: flex;
2743
}
2844
}
2945
}

assets/stylesheets/new-stylesheets/_themes.scss

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,10 @@
7070
--nav-scroller-gradient-end: #fff;
7171

7272
// syntax
73+
--color-syntax-clipboard-bg: rgb(223, 223, 247);
74+
--color-syntax-clipboard-hover-bg: rgb(216, 216, 242);
75+
--color-syntax-clipboard-check-color: rgb(12, 156, 12);
76+
7377
--color-syntax-attributes: rgb(148, 113, 0);
7478
--color-syntax-characters: rgb(39, 42, 216);
7579
--color-syntax-comments: rgb(112, 127, 140);
@@ -165,6 +169,10 @@
165169
--nav-scroller-gradient-end: #000;
166170

167171
// syntax
172+
--color-syntax-clipboard-bg: rgb(71, 79, 110);
173+
--color-syntax-clipboard-hover-bg: rgb(80, 89, 124);
174+
--color-syntax-clipboard-check-color: rgb(11, 242, 11);
175+
168176
--color-syntax-attributes: rgb(204, 151, 104);
169177
--color-syntax-characters: rgb(217, 201, 124);
170178
--color-syntax-comments: rgb(127, 140, 152);

0 commit comments

Comments
 (0)