aboutsummaryrefslogtreecommitdiff
path: root/src/components/InputCopyable.vue
blob: eaf29c81dc8f1e2be707ac8b71c0078c44f8e9c9 (plain) (blame)
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
<template>
  <n-input v-model:value="value">
    <template #suffix>
      <n-tooltip trigger="hover">
        <template #trigger>
          <c-button circle variant="text" @click="onCopyClicked">
            <n-icon :component="ContentCopyFilled" />
          </c-button>
        </template>
        {{ tooltipText }}
      </n-tooltip>
    </template>
  </n-input>
</template>

<script setup lang="ts">
import { useVModel, useClipboard } from '@vueuse/core';
import { ContentCopyFilled } from '@vicons/material';
import { ref } from 'vue';

const props = defineProps<{ value: string }>();
const emit = defineEmits(['update:value']);

const value = useVModel(props, 'value', emit);
const tooltipText = ref('Copy to clipboard');

const { copy } = useClipboard({ source: value });

function onCopyClicked() {
  copy();
  tooltipText.value = 'Copied !';

  setTimeout(() => {
    tooltipText.value = 'Copy to clipboard';
  }, 2000);
}
</script>

<style scoped>
::v-deep(.n-input-wrapper) {
  padding-right: 5px;
}
</style>