vue-clipboard2 vs clipboard-copy vs copy-to-clipboard vs ngclipboard vs react-copy-to-clipboard
Copying Text to Clipboard in Web Applications
vue-clipboard2clipboard-copycopy-to-clipboardngclipboardreact-copy-to-clipboardSimilar Packages:

Copying Text to Clipboard in Web Applications

clipboard-copy, copy-to-clipboard, ngclipboard, react-copy-to-clipboard, and vue-clipboard2 are npm packages that help developers implement copy-to-clipboard functionality in web applications. They abstract away browser inconsistencies and security restrictions around the Clipboard API, offering simple interfaces to copy text programmatically or via user interaction. While some are framework-agnostic utilities (clipboard-copy, copy-to-clipboard), others are tightly integrated with specific frontend frameworks like Angular (ngclipboard), React (react-copy-to-clipboard), or Vue.js (vue-clipboard2).

Npm Package Weekly Downloads Trend

3 Years

Github Stars Ranking

Stat Detail

Package
Downloads
Stars
Size
Issues
Publish
License
vue-clipboard2104,9661,759-375 years agoMIT
clipboard-copy0634-75 years agoMIT
copy-to-clipboard01,39715.1 kB48-MIT
ngclipboard038519.4 kB23-MIT
react-copy-to-clipboard02,37137.9 kB14a month agoMIT

Copying Text to Clipboard: A Practical Comparison of Popular npm Packages

Copying text to the clipboard is a common UI requirement — think “copy invite link” buttons or “copy code snippet” actions. But browser security rules make this tricky: you can’t just write to the clipboard whenever you want. You need either a user gesture (like a click) or permission via the modern Async Clipboard API. The packages clipboard-copy, copy-to-clipboard, ngclipboard, react-copy-to-clipboard, and vue-clipboard2 all aim to simplify this, but they do it in very different ways depending on your stack and needs.

🧰 Core Approach: Utility Function vs Framework Integration

clipboard-copy – Element-First, Zero Dependencies

This package takes a DOM element and copies its text content (or a custom value) using the best available method. It’s designed to be called inside event handlers.

// clipboard-copy
import copy from 'clipboard-copy';

document.getElementById('copy-btn').addEventListener('click', async () => {
  await copy('Hello, clipboard!');
  console.log('Copied!');
});

It also supports passing a DOM element whose .textContent will be copied:

const el = document.querySelector('#secret-code');
await copy(el); // Copies el.textContent

Under the hood, it tries the modern navigator.clipboard.writeText() first, falling back to document.execCommand('copy') in older browsers.

copy-to-clipboard – Promise-Based, No DOM Required

This is a pure function that takes a string and returns a promise. No element reference needed.

// copy-to-clipboard
import copy from 'copy-to-clipboard';

async function handleCopy() {
  try {
    await copy('https://example.com/share?token=abc123');
    alert('Link copied!');
  } catch (err) {
    console.error('Failed to copy', err);
  }
}

It uses the same fallback strategy as clipboard-copy but exposes a cleaner async interface. Great when your copy logic lives in a service or utility file, not tied to a specific button.

ngclipboard – Angular Directive (Deprecated Pattern)

This package provides an Angular directive that binds to a string value and triggers copy on click.

<!-- ngclipboard (Angular) -->
<button [ngClipboard]="'Text to copy'" (cbOnSuccess)="onCopied()">
  Copy
</button>

However, the repository hasn’t been updated for Angular Ivy or recent versions. It relies on Renderer2 and manual event binding, and there’s no official support for standalone components or signals. Given its inactive status, avoid in new Angular projects.

react-copy-to-clipboard – React Component with State Feedback

This wraps copy logic in a React component that manages success state for you.

// react-copy-to-clipboard
import { CopyToClipboard } from 'react-copy-to-clipboard';

function ShareButton() {
  const [copied, setCopied] = useState(false);

  return (
    <CopyToClipboard text="invite@example.com" onCopy={() => setCopied(true)}>
      <button>{copied ? 'Copied!' : 'Copy Email'}</button>
    </CopyToClipboard>
  );
}

It uses copy-to-clipboard under the hood but adds React-specific ergonomics. Note: it doesn’t use hooks internally (as of latest release), so it’s class-component friendly but slightly heavier than calling copy-to-clipboard directly in a useCallback.

vue-clipboard2 – Vue 2 Plugin with Directive

For Vue 2, this registers a global directive and a $copyText method.

// vue-clipboard2 (Vue 2)
// In main.js
import VueClipboard from 'vue-clipboard2';
Vue.use(VueClipboard);

Then in a component:

<template>
  <button v-clipboard:copy="text" v-clipboard:success="onCopy">Copy</button>
</template>

<script>
export default {
  data() {
    return { text: 'Hello from Vue!' };
  },
  methods: {
    onCopy() {
      this.$message('Copied!');
    }
  }
};
</script>

But this package does not work with Vue 3. The Vue 3 ecosystem has moved toward composable functions, and this plugin hasn’t adapted. For Vue 3, skip this and use copy-to-clipboard inside a @click handler.

⚠️ Maintenance and Compatibility Reality Check

  • ngclipboard: Last published in 2018. No support for Angular 9+. Repository archived or inactive. Do not use in new projects.
  • vue-clipboard2: Last update in 2020. Explicitly Vue 2 only. No Vue 3 compatibility. Avoid for Vue 3 apps.
  • react-copy-to-clipboard: Actively maintained as of 2023, supports React 16–18, but doesn’t leverage modern hooks patterns.
  • clipboard-copy and copy-to-clipboard: Both actively maintained, framework-agnostic, and support modern browsers with graceful fallbacks.

🔁 Fallback Strategy: How They Handle Older Browsers

All packages attempt to use navigator.clipboard.writeText() first (secure, async, requires HTTPS or localhost). If unavailable (e.g., Safari < 13.1, old Chrome), they fall back to the deprecated but widely supported document.execCommand('copy').

The fallback requires creating a temporary <textarea>, adding it to the DOM, selecting its content, and triggering execCommand. Both clipboard-copy and copy-to-clipboard handle this cleanly:

// Simplified fallback logic (used by both)
function fallbackCopyText(text) {
  const textarea = document.createElement('textarea');
  textarea.value = text;
  textarea.style.position = 'fixed';
  document.body.appendChild(textarea);
  textarea.select();
  try {
    return document.execCommand('copy');
  } finally {
    document.body.removeChild(textarea);
  }
}

Framework-specific packages inherit this behavior from their underlying utilities.

🛠️ When to Use What: Real Engineering Trade-offs

Use copy-to-clipboard if:

  • You’re in a framework-agnostic environment (Svelte, Solid, vanilla JS).
  • Your copy logic is decoupled from the DOM (e.g., triggered by a Redux action or service call).
  • You want clean async/await syntax and don’t need automatic UI feedback.

Use clipboard-copy if:

  • You already have a reference to a DOM element (e.g., in a web component or jQuery-style code).
  • You want the smallest possible footprint with no extra abstractions.
  • You’re building a design system or low-level utility library.

Use react-copy-to-clipboard if:

  • You’re in a React app and want built-in success state without writing useState boilerplate.
  • Your team prefers declarative components over imperative function calls.
  • You’re okay with a small wrapper around copy-to-clipboard.

Avoid ngclipboard and vue-clipboard2 in new projects because:

  • They’re tied to outdated framework versions.
  • Maintenance is uncertain.
  • Modern alternatives (native Clipboard API + lightweight utils) are more reliable.

💡 Pro Tip: In 2024, most apps can safely use the native navigator.clipboard.writeText() directly if you control your browser support matrix. Only reach for a package if you need IE11 or old Safari support — and even then, copy-to-clipboard is the safest bet.

✅ Final Recommendation

For new projects, default to copy-to-clipboard — it’s simple, well-maintained, and works everywhere. If you’re deep in React and love components, react-copy-to-clipboard is fine. But steer clear of ngclipboard and vue-clipboard2 unless you’re maintaining a legacy Angular or Vue 2 app with no upgrade path.

Remember: the clipboard is a user-facing feature. Always provide visual feedback (“Copied!”) and never copy without explicit user intent — browsers will block silent clipboard writes anyway.

How to Choose: vue-clipboard2 vs clipboard-copy vs copy-to-clipboard vs ngclipboard vs react-copy-to-clipboard

  • vue-clipboard2:

    Choose vue-clipboard2 if you’re using Vue 2 and need a directive (v-clipboard) or programmatic API that fits naturally into Vue’s reactivity system. Be aware it is not compatible with Vue 3, and the project shows signs of abandonment — for new Vue 3 projects, use the native Clipboard API or a framework-agnostic utility.

  • clipboard-copy:

    Choose clipboard-copy if you need a tiny, zero-dependency utility that works directly with DOM elements and supports both modern Clipboard API and legacy document.execCommand() fallbacks. It’s ideal for vanilla JavaScript projects or when you want minimal abstraction and full control over element selection and event handling.

  • copy-to-clipboard:

    Choose copy-to-clipboard if you prefer a promise-based, framework-agnostic function that copies text without requiring a DOM element reference. It’s well-suited for logic-heavy applications where clipboard operations are triggered from business logic rather than direct UI events, and you want clean async/await syntax.

  • ngclipboard:

    Choose ngclipboard only if you’re working on an Angular application and want a directive-based approach that integrates with Angular’s template syntax. However, note that the package appears unmaintained and lacks support for modern Angular versions — consider using Angular’s built-in renderer or a lightweight alternative instead.

  • react-copy-to-clipboard:

    Choose react-copy-to-clipboard if you’re building a React app and want a component that handles clipboard state (like success feedback) declaratively. It wraps the underlying copy logic in a render prop or child function pattern, making it easy to show UI changes after copying, though it adds a small layer of abstraction over simpler utility functions.

README for vue-clipboard2

vue-clipboard2

A simple vuejs 2 binding for clipboard.js

Install

npm install --save vue-clipboard2 or use dist/vue-clipboard.min.js without webpack

Usage

For vue-cli user:

import Vue from 'vue'
import VueClipboard from 'vue-clipboard2'

Vue.use(VueClipboard)

For standalone usage:

<script src="vue.min.js"></script>
<!-- must place this line after vue.js -->
<script src="dist/vue-clipboard.min.js"></script>

I want to copy texts without a specific button!

Yes, you can do it by using our new method: this.$copyText. See sample2, where we replace the clipboard directives with a v-on directive.

Modern browsers have some limitations like that you can't use window.open without a user interaction. So there's the same restriction on copying things! Test it before you use it. Make sure you are not using this method inside any async method.

Before using this feature, read: this issue and this page first.

It doesn't work with bootstrap modals

See clipboardjs document and this pull request, container option is available like this:

let container = this.$refs.container
this.$copyText("Text to copy", container)

Or you can let vue-clipboard2 set container to current element by doing this:

import Vue from 'vue'
import VueClipboard from 'vue-clipboard2'

VueClipboard.config.autoSetContainer = true // add this line
Vue.use(VueClipboard)

Sample

<div id="app"></div>

<template id="t">
  <div class="container">
    <input type="text" v-model="message">
    <button type="button"
      v-clipboard:copy="message"
      v-clipboard:success="onCopy"
      v-clipboard:error="onError">Copy!</button>
  </div>
</template>

<script>
new Vue({
  el: '#app',
  template: '#t',
  data: function () {
    return {
      message: 'Copy These Text'
    }
  },
  methods: {
    onCopy: function (e) {
      alert('You just copied: ' + e.text)
    },
    onError: function (e) {
      alert('Failed to copy texts')
    }
  }
})
</script>

Sample 2

<div id="app"></div>

  <template id="t">
    <div class="container">
    <input type="text" v-model="message">
    <button type="button" @click="doCopy">Copy!</button>
    </div>
  </template>

  <script>
  new Vue({
    el: '#app',
    template: '#t',
    data: function () {
      return {
        message: 'Copy These Text'
      }
    },
    methods: {
      doCopy: function () {
        this.$copyText(this.message).then(function (e) {
          alert('Copied')
          console.log(e)
        }, function (e) {
          alert('Can not copy')
          console.log(e)
        })
      }
    }
  })
  </script>

You can use your Vue instance vm.$el to get DOM elements via the usual traversal methods, e.g.:

this.$el.children[1].children[2].textContent

This will allow you to access the rendered content of your components, rather than the components themselves.

Contribution

PRs welcome, and issues as well! If you want any feature that we don't have currently, please fire an issue for a feature request.

License

MIT License