Skip to content

Commit 69a9d40

Browse files
author
Jesse Haigh
committed
style, very similar to swift.org's code block copy style
1 parent 5b424bb commit 69a9d40

File tree

1 file changed

+72
-3
lines changed

1 file changed

+72
-3
lines changed

src/components/ContentNode/CodeListing.vue

Lines changed: 72 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,32 @@
2222
>{{ fileName }}
2323
</Filename>
2424
<div class="container-general">
25-
<button class="copy-button" @click="copyToClipboard">Copy</button>
25+
<button
26+
class="copy-button"
27+
:class="{ copied: isCopied }"
28+
@click="copyToClipboard"
29+
aria-label="Copy code to clipboard"
30+
>
31+
<svg
32+
v-if="!isCopied"
33+
xmlns="http://www.w3.org/2000/svg"
34+
viewbox="0 0 24 24"
35+
width="24"
36+
height="24"
37+
fill="currentColor"
38+
><path d="M16 1H4c-1.1 0-2 .9-2 2v14h2V3h12V1zm3 4H8c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2
39+
2h11c1.1 0 2-.9 2-2V7c0-1.1-.9-2-2-2zm0 16H8V7h11v14z"/>
40+
</svg>
41+
<svg
42+
v-if="isCopied"
43+
xmlns="http://www.w3.org/2000/svg"
44+
viewbox="0 0 24 24"
45+
width="24"
46+
height="24"
47+
fill="currentColor"
48+
><path d="M9 16.2L4.8 12l-1.4 1.4L9 19 21 7l-1.4-1.4L9 16.2z"/>
49+
</svg>
50+
</button>
2651
<!-- Do not add newlines in <pre>, as they'll appear in the rendered HTML. -->
2752
<pre><CodeBlock><template
2853
v-for="(line, index) in syntaxHighlightedLines"
@@ -56,6 +81,7 @@ export default {
5681
data() {
5782
return {
5883
syntaxHighlightedLines: [],
84+
isCopied: false,
5985
};
6086
},
6187
props: {
@@ -127,8 +153,15 @@ export default {
127153
const lines = this.content;
128154
const text = lines.join('\n');
129155
navigator.clipboard.writeText(text)
130-
.then(() => console.log('Copied!'))
131-
.catch(err => console.error('Failed to copy: ', err));
156+
.then(() => {
157+
this.isCopied = true;
158+
setTimeout(() => {
159+
this.isCopied = false;
160+
}, 1000);
161+
})
162+
.catch(err => (
163+
console.error('Failed to copy text: ', err)
164+
));
132165
},
133166
},
134167
};
@@ -206,11 +239,47 @@ code {
206239
207240
.container-general {
208241
overflow: auto;
242+
position: relative;
209243
}
210244
211245
.container-general,
212246
pre {
213247
flex-grow: 1;
214248
}
215249
250+
.copy-button {
251+
position: absolute;
252+
top: 1em;
253+
right: 1em;
254+
background: var(--color-syntax-clipboard-bg, #e0e0e0);
255+
border: none;
256+
border-radius: 6px;
257+
padding: 7px 6px;
258+
cursor: pointer;
259+
display: none;
260+
transition: all 0.2s ease-in-out;
261+
}
262+
263+
.copy-button svg {
264+
width: 24px;
265+
height: 24px;
266+
opacity: 0.8;
267+
}
268+
269+
.copy-button:hover {
270+
background-color: var(--code-syntax-clipboard-hover-bg, #d0d0d0);
271+
}
272+
273+
.copy-button:hover svg {
274+
opacity: 1;
275+
}
276+
277+
.copy-button.copied svg {
278+
color: var(--color-syntax-clipboard-check-color, #007aff);
279+
}
280+
281+
.container-general:hover .copy-button {
282+
display: flex;
283+
}
284+
216285
</style>

0 commit comments

Comments
 (0)