aboutsummaryrefslogtreecommitdiff
path: root/src/components/SpanCopyable.vue
blob: c753d2e0b2376aca5c7769f0d40ff565dc21a482 (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
<script setup lang="ts">
import { useClipboard } from '@vueuse/core';
import { ref, toRefs } from 'vue';

const props = withDefaults(defineProps<{ value?: string }>(), { value: '' });
const { value } = toRefs(props);

const initialText = 'Copy to clipboard';
const tooltipText = ref(initialText);

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

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

  setTimeout(() => (tooltipText.value = initialText), 1000);
}
</script>

<template>
  <n-tooltip trigger="hover">
    <template #trigger>
      <span class="value" @click="handleClick">{{ value }}</span>
    </template>
    {{ tooltipText }}
  </n-tooltip>
</template>

<style scoped lang="less">
.value {
  cursor: pointer;
  font-family: monospace;
}
</style>