it-tools/src/components/SpanCopyable.vue

36 lines
781 B
Vue
Raw Normal View History

2023-03-28 23:31:11 +02:00
<template>
<n-tooltip trigger="hover">
<template #trigger>
<span class="value" @click="handleClick">{{ value }}</span>
2023-03-28 23:31:11 +02:00
</template>
{{ tooltipText }}
</n-tooltip>
</template>
<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);
2023-03-28 23:31:11 +02:00
const initialText = 'Copy to clipboard';
const tooltipText = ref(initialText);
const { copy } = useClipboard({ source: value });
2023-03-28 23:31:11 +02:00
function handleClick() {
copy();
tooltipText.value = 'Copied!';
setTimeout(() => (tooltipText.value = initialText), 1000);
}
</script>
<style scoped lang="less">
.value {
2023-03-28 23:31:11 +02:00
cursor: pointer;
font-family: monospace;
2023-03-28 23:31:11 +02:00
}
</style>