<template>
  <button ref="copyButton" @click="copyText">
    <slot>复制链接</slot>
  </button>
</template>

<script lang="ts" setup>
import useClipboard from 'vue-clipboard3'
const props = defineProps({
  text: {
    type: String,
    required: true
  }
});
const { toClipboard } = useClipboard()
const emit = defineEmits(['copy']);

const copyText = async () => {
  try {
    await toClipboard(props.text)
    console.log('Copied to clipboard')
    ElMessage.success('复制成功')
    emit('copy');
  } catch (e) {
    ElMessage.error('复制失败')
    console.error(e)
  }
}
</script>
