Skip to content

Commit 75fb7bc

Browse files
committed
add: click to copy
1 parent 567df7e commit 75fb7bc

File tree

1 file changed

+30
-1
lines changed

1 file changed

+30
-1
lines changed

src/components/code-block/CodeBlock.vue

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,13 @@
2121
<span>{{ props.fileName }}</span>
2222
</div>
2323
<div
24-
@click="copyCodeToClipboard(props.code)"
24+
@click="copyCode"
2525
class="actions">
2626
<button
2727
type="button"
2828
class="copy-button">
2929
<svg
30+
v-if="!copied"
3031
fill="none"
3132
stroke="currentColor"
3233
stroke-width="1.5"
@@ -39,6 +40,17 @@
3940
<path
4041
d="M18 12.25h-4a1 1 0 0 1-1-1v-1.5a1 1 0 0 1 1-1h4a1 1 0 0 1 1 1v1.5a1 1 0 0 1-1 1ZM13.75 16.25h4.5M13.75 19.25h4.5"></path>
4142
</svg>
43+
<svg
44+
v-else
45+
class="check-icon"
46+
viewBox="0 0 36 36">
47+
<path
48+
fill="#77B255"
49+
d="M36 32a4 4 0 0 1-4 4H4a4 4 0 0 1-4-4V4a4 4 0 0 1 4-4h28a4 4 0 0 1 4 4v28z"></path>
50+
<path
51+
fill="#FFF"
52+
d="M29.28 6.362a2.502 2.502 0 0 0-3.458.736L14.936 23.877l-5.029-4.65a2.5 2.5 0 1 0-3.394 3.671l7.209 6.666c.48.445 1.09.665 1.696.665c.673 0 1.534-.282 2.099-1.139c.332-.506 12.5-19.27 12.5-19.27a2.5 2.5 0 0 0-.737-3.458z"></path>
53+
</svg>
4254
</button>
4355
</div>
4456
</div>
@@ -89,6 +101,16 @@
89101
const codeBlock: Ref<HTMLDivElement | null> = ref(null);
90102
const code = computed(() => parseCodeIntoLines(props.code, props.language));
91103
const currentTheme = computed(() => themes[props.theme]);
104+
const copied = ref(false);
105+
106+
const copyCode = () => {
107+
copied.value = true;
108+
copyCodeToClipboard(props.code);
109+
110+
setTimeout(() => {
111+
copied.value = false;
112+
}, 2000);
113+
};
92114
93115
onMounted(() => {
94116
addThemeToCodeBlock(codeBlock.value, currentTheme.value);
@@ -165,6 +187,13 @@
165187
height: 2rem;
166188
}
167189
190+
.vuejs-code-block pre .header .actions .check-icon {
191+
width: 2rem;
192+
height: 2rem;
193+
scale: 0.8;
194+
object-position: center;
195+
}
196+
168197
.vuejs-code-block pre .line {
169198
padding: 0 1rem;
170199
display: flex;

0 commit comments

Comments
 (0)