Skip to content

useCopyToClipboard

A Vue 3 composable to copy text to the clipboard with reactive status tracking.

Overview

This composable provides a simple API to copy text to the user's clipboard, while tracking whether the Clipboard API is supported, if the copy operation succeeded, or if an error occurred.

Installation

bash
npx kuro-ui add useCopyToClipboard
bash
npx github:Bartek-Nowak/Kuro add useCopyToClipboard

Usage example

vue
<script setup lang="ts">
import { useCopyToClipboard } from '@/composables/kuro/useCopyToClipboard';

const { copy, success, error, isSupported } = useCopyToClipboard();

const copyText = async () => {
  await copy('Hello World!');
  if (success.value) {
    console.log('Text copied successfully');
  } else {
    console.error('Copy failed:', error.value);
  }
};
</script>

<template>
  <button @click="copyText" :disabled="!isSupported">
    Copy Text
  </button>
  <p v-if="error">{{ error }}</p>
</template>

API

NameTypeDescription
copy(text: string) => Promise<void>Function to copy the given text to clipboard.
successRef<boolean>Reactive ref indicating if the last copy succeeded.
errorRef<string | null>Reactive ref holding the error message if copying failed, or null.
isSupportedbooleanBoolean indicating whether Clipboard API is supported by the browser.

Notes

  • The composable uses the native Clipboard API.
  • The copy function is async and should be awaited to check results reliably.
  • The isSupported flag helps prevent running copy attempts on unsupported browsers.