CopyButton.vue 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. <template>
  2. <button
  3. type="button"
  4. class="group/button absolute top-3.5 right-4 overflow-hidden rounded-full py-1 pl-2 pr-3 text-2xs font-medium opacity-0 backdrop-blur transition focus:opacity-100 group-hover:opacity-100"
  5. :class="{
  6. 'bg-emerald-400/10 ring-1 ring-inset ring-emerald-400/20': copied,
  7. 'bg-white/2.5 hover:bg-white/5': !copied
  8. }"
  9. @click="copyCode()">
  10. <span
  11. class="pointer-events-none flex items-center gap-0.5 text-zinc-400 transition duration-300"
  12. :aria-hidden="copied"
  13. :class="{
  14. '-translate-y-1.5 opacity-0': copied
  15. }">
  16. <ClipboardIcon class="h-5 w-5 fill-zinc-500/20 stroke-zinc-500 transition-colors group-hover/button:stroke-zinc-400"/>
  17. Copy
  18. </span>
  19. <span
  20. class="pointer-events-none absolute inset-0 flex items-center justify-center text-emerald-400 transition duration-300"
  21. :aria-hidden="!copied"
  22. :class="{
  23. 'translate-y-1.5 opacity-0': !copied
  24. }">
  25. Copied!
  26. </span>
  27. </button>
  28. </template>
  29. <script setup>
  30. const props = defineProps({
  31. code: {
  32. default: ''
  33. }
  34. });
  35. const copied = ref(false);
  36. const copyCode = () => {
  37. window.navigator.clipboard.writeText( props.code ).then( () => {
  38. copied.value = true;
  39. setTimeout(() => {
  40. copied.value = false;
  41. }, 1000)
  42. })
  43. }
  44. </script>