-
-
Notifications
You must be signed in to change notification settings - Fork 162
/
Copy pathCopyButton.vue
64 lines (59 loc) · 1.31 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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
<template>
<button :id="id"
type="button"
class="button is-link photo_copy-btn"
:data-clipboard-target="el">
<slot v-if="!success" default />
<template v-else>Copied!</template>
</button>
</template>
<script>
/* eslint-disable */
import Clipboard from 'clipboard'
export default {
data: () => ({
success: false,
clipboard: null,
}),
props: {
el: {
required: true,
},
id: {
required: true,
},
},
methods: {
onCopySuccess(e) {
this.success = true;
this.$emit('copied', { content: e.text });
setTimeout(() => {
this.success = false;
}, 2000);
e.clearSelection();
},
onCopyError(e) {
this.$emit('copyFailed');
e.clearSelection();
},
},
mounted() {
this.clipboard = new Clipboard(`#${this.$props.id}`);
this.clipboard.on('success', this.onCopySuccess);
this.clipboard.on('error', this.onCopyError);
},
destroyed() {
this.clipboard.destroy();
},
}
</script>
<style scoped>
.photo_copy-btn {
width: 10rem;
display: block;
margin-top: .5rem;
}
button {
cursor: pointer;
}
</style>