/
CopyButton.vue
41 lines (37 loc) · 1 KB
/
CopyButton.vue
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
<template>
<button title="Copy to clipboard" @click="copy(text)">
<Icon
name="clipboard"
:class="copied ? 'fill-green-300' : 'fill-gray-200 hover:fill-white'"
/>
<div v-if="copied" class="ml-2 absolute top-0 left-full text-green-300">
Copied!
</div>
</button>
</template>
<script>
export default {
props: {
text: { required: true },
},
data: () => ({
copied: false,
timeout: false,
}),
methods: {
copy(text) {
if (this.timeout) {
window.clearTimeout(this.timeout);
}
const el = document.createElement('textarea');
el.value = text;
document.body.appendChild(el);
el.select();
document.execCommand('copy');
document.body.removeChild(el);
this.copied = true;
this.timeout = window.setTimeout(() => (this.copied = false), 3000);
},
},
};
</script>