Vue Copy To Clipboard Component

1.0.3 · maintenance · verified Wed Apr 22

This package provides a simple Vue component for copying text to the user's clipboard. It is currently at version 1.0.3 and appears to be in a maintenance state, with no significant updates since 2020, specifically targeting Vue 2 environments. Its release cadence is very infrequent, reflecting its stable but dormant status. The component offers a declarative, straightforward way to integrate copy-to-clipboard functionality into Vue 2 applications, typically via a `text` prop and a `copy` event. Unlike newer solutions for Vue 3 (such as `vue-clipboard3` or `@vueuse/core`'s `useClipboard` composable), this library adheres to Vue 2's component model and is a direct wrapper, abstracting the underlying Clipboard API or `document.execCommand('copy')` for ease of use within its intended ecosystem.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates how to import and use the `CopyToClipboard` component in a Vue 2 template, binding text and handling the copy event to provide user feedback.

<template>
  <div>
    <p>Content to copy: <strong>{{ text }}</strong></p>
    <copy-to-clipboard :text="text" @copy="handleCopy">
      <button style="padding: 8px 16px; background-color: #42b983; color: white; border: none; border-radius: 4px; cursor: pointer;">
        Copy to Clipboard
      </button>
    </copy-to-clipboard>
    <p v-if="copiedMessage">{{ copiedMessage }}</p>
  </div>
</template>

<script lang="ts">
import CopyToClipboard from 'vue-copy-to-clipboard'
import Vue from 'vue';

export default Vue.extend({
  components: {
    CopyToClipboard
  },
  data() {
    return {
      text: 'This is the dynamic content to be copied!',
      copiedMessage: ''
    }
  },
  methods: {
    handleCopy(result: { text: string, isSuccess: boolean }) {
      if (result.isSuccess) {
        this.copiedMessage = `Successfully copied: '${result.text}'`
      } else {
        this.copiedMessage = 'Failed to copy text.'
      }
      setTimeout(() => { this.copiedMessage = ''; }, 3000);
      console.log('onCopy result:', result);
    }
  }
});
</script>

view raw JSON →