Vue 3 Clipboard Composition API

2.0.0 · active · verified Sun Apr 19

vue-clipboard3 is a Vue 3 Composition API utility for easily copying text to the clipboard. Currently at version 2.0.0, it provides a simple `useClipboard` hook that leverages `clipboard.js` internally to handle the complexities of cross-browser clipboard access. The package follows a lightweight approach, foregoing directives in favor of a direct method call within the `setup()` function, aligning with Vue 3's modern Composition API patterns. Its release cadence has been responsive, with recent updates focused on compatibility with modern build tools like Vite and ESM module standards. This library differentiates itself by offering a clean, hook-based API for Vue 3 projects that require clipboard functionality without the overhead of older directive-based solutions, ensuring a straightforward and explicit developer experience.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use the `useClipboard` hook within a Vue 3 Composition API component to copy both static strings and reactive ref values to the clipboard, including error handling.

import { defineComponent, ref } from 'vue';
import useClipboard from 'vue-clipboard3';

export default defineComponent({
  setup() {
    const { toClipboard } = useClipboard();
    const inputText = ref('Hello, clipboard!');

    const copyText = async (textToCopy: string) => {
      try {
        await toClipboard(textToCopy);
        console.log(`'${textToCopy}' copied to clipboard successfully!`);
      } catch (e) {
        console.error('Failed to copy text:', e);
        // Optionally, show a user-friendly error message
      }
    };

    const copyInputContent = async () => {
      await copyText(inputText.value);
    }

    return {
      inputText,
      copyText,
      copyInputContent
    };
  },
  template: `
    <div style="font-family: sans-serif; max-width: 600px; margin: 20px auto; padding: 20px; border: 1px solid #eee; border-radius: 8px; box-shadow: 0 2px 4px rgba(0,0,0,0.1);">
      <h1 style="color: #333; font-size: 1.8em;">Vue-Clipboard3 Example</h1>
      <p style="color: #555;">Click buttons to copy text to clipboard.</p>
      
      <button @click="copyText('Simple text copy from a button')" style="background-color: #4CAF50; color: white; padding: 10px 15px; border: none; border-radius: 5px; cursor: pointer; font-size: 1em; margin-bottom: 20px;">Copy 'Simple text copy'</button>
      
      <div style="margin-top: 15px; display: flex; align-items: center;">
        <input type="text" v-model="inputText" placeholder="Enter text to copy" style="flex-grow: 1; padding: 10px; border: 1px solid #ccc; border-radius: 5px; font-size: 1em;">
        <button @click="copyInputContent" style="margin-left: 10px; background-color: #008CBA; color: white; padding: 10px 15px; border: none; border-radius: 5px; cursor: pointer; font-size: 1em;">Copy Input Content</button>
      </div>

      <div style="margin-top: 25px; padding-top: 15px; border-top: 1px dashed #eee;">
        <p style="color: #666; font-size: 0.9em;">This demonstrates using the <code>useClipboard</code> hook with both static strings and reactive refs. Check your browser's developer console for success or error messages after clicking the buttons.</p>
      </div>
    </div>
  `
});

view raw JSON →