react-copy-to-clipboard vs clipboard-polyfill vs react-clipboard.js
Clipboard Interaction Libraries Comparison
1 Year
react-copy-to-clipboardclipboard-polyfillreact-clipboard.jsSimilar Packages:
What's Clipboard Interaction Libraries?

Clipboard interaction libraries in JavaScript provide developers with tools to programmatically access and manipulate the clipboard, allowing for actions like copying and pasting text or images. These libraries abstract the complexities of the Clipboard API, offering simple interfaces for tasks such as copying content to the clipboard, handling paste events, and managing clipboard data across different browsers. They are particularly useful in web applications that require enhanced clipboard functionality, such as text editors, sharing tools, or any app that needs to interact with the user's clipboard seamlessly. clipboard-polyfill is a lightweight library that provides a consistent API for clipboard operations across browsers, including those that do not fully support the modern Clipboard API. react-clipboard.js is a React component that simplifies copying text to the clipboard with minimal setup, leveraging the Clipboard API and providing built-in support for handling copy events. react-copy-to-clipboard is a flexible React component that allows developers to wrap any element and enable copying its content to the clipboard with a simple API, including support for customizable copy actions and event handling.

Package Weekly Downloads Trend
Github Stars Ranking
Stat Detail
Package
Downloads
Stars
Size
Issues
Publish
License
react-copy-to-clipboard1,575,9812,36340.6 kB23-MIT
clipboard-polyfill122,200925404 kB96 months agoMIT
react-clipboard.js32,132273-106 years agoCC0
Feature Comparison: react-copy-to-clipboard vs clipboard-polyfill vs react-clipboard.js

Cross-Browser Compatibility

  • react-copy-to-clipboard:

    react-copy-to-clipboard is designed to work with the modern Clipboard API, providing good compatibility across current browsers. It does not specifically address older browsers, so developers should be aware of this limitation when using it in applications that require backward compatibility.

  • clipboard-polyfill:

    clipboard-polyfill provides robust cross-browser compatibility, including support for older browsers and those with incomplete Clipboard API implementations. It ensures consistent behavior across different environments, making it reliable for applications that need to function on a wide range of devices and browsers.

  • react-clipboard.js:

    react-clipboard.js relies on the modern Clipboard API for its functionality, which is supported in most modern browsers. However, it may not handle older browsers or those with limited support as gracefully as clipboard-polyfill, making it more suitable for applications targeting contemporary web environments.

Ease of Use

  • react-copy-to-clipboard:

    react-copy-to-clipboard is highly intuitive and easy to use, especially for React developers. Its simple API allows for quick implementation of copy functionality, and its flexibility in wrapping elements makes it versatile for various use cases.

  • clipboard-polyfill:

    clipboard-polyfill offers a straightforward API for clipboard operations, but it requires some understanding of how to integrate it into your application. It is more of a utility library than a plug-and-play solution, which may require additional setup for optimal use.

  • react-clipboard.js:

    react-clipboard.js provides an easy-to-use interface for copying text in React applications. Its component-based design allows for quick integration, and it handles the complexities of clipboard interactions, making it user-friendly for developers.

Customization

  • react-copy-to-clipboard:

    react-copy-to-clipboard excels in customization, allowing developers to specify the text to be copied, handle copy events, and even customize the appearance and behavior of the component. This makes it a great choice for applications that require more control over the copy functionality.

  • clipboard-polyfill:

    clipboard-polyfill is not highly customizable, as it focuses on providing a consistent API for clipboard operations. However, its simplicity allows developers to use it in a variety of contexts without needing extensive configuration.

  • react-clipboard.js:

    react-clipboard.js offers limited customization, primarily around the text being copied and the handling of copy events. It is designed to be simple and straightforward, which may limit its flexibility for more complex use cases.

Code Example

  • react-copy-to-clipboard:

    React Copy to Clipboard Example

    import React, { useState } from 'react';
    import { CopyToClipboard } from 'react-copy-to-clipboard';
    
    function CopyExample() {
      const [text, setText] = useState('Hello, World!');
      const [copied, setCopied] = useState(false);
    
      return (
        <div>
          <input
            type="text"
            value={text}
            onChange={(e) => setText(e.target.value)}
          />
          <CopyToClipboard
            text={text}
            onCopy={() => setCopied(true)}
          >
            <button>Copy</button>
          </CopyToClipboard>
          {copied && <span>Copied!</span>}
        </div>
      );
    }
    
    export default CopyExample;
    
  • clipboard-polyfill:

    Clipboard Polyfill Example

    import { writeText } from 'clipboard-polyfill/text';
    
    async function copyToClipboard(text) {
      try {
        await writeText(text);
        console.log('Text copied to clipboard!');
      } catch (err) {
        console.error('Failed to copy text: ', err);
      }
    }
    
    copyToClipboard('Hello, World!');
    
  • react-clipboard.js:

    React Clipboard.js Example

    import React from 'react';
    import { CopyToClipboard } from 'react-clipboard.js';
    
    function ClipboardComponent() {
      return (
        <CopyToClipboard text="Hello, World!">
          <button>Copy to Clipboard</button>
        </CopyToClipboard>
      );
    }
    
    export default ClipboardComponent;
    
How to Choose: react-copy-to-clipboard vs clipboard-polyfill vs react-clipboard.js
  • react-copy-to-clipboard:

    Choose react-copy-to-clipboard if you need a highly customizable and flexible solution for copying content in a React app, with support for wrapping any element and handling copy actions programmatically.

  • clipboard-polyfill:

    Choose clipboard-polyfill if you need a lightweight, cross-browser solution that provides a consistent API for clipboard operations, especially in environments with limited support for the modern Clipboard API.

  • react-clipboard.js:

    Choose react-clipboard.js if you are building a React application and want a simple, component-based approach to copying text to the clipboard, with built-in support for copy events and minimal configuration.

README for react-copy-to-clipboard

react-copy-to-clipboard npm

Gitter

CircleCI Dependencies Dev Dependencies

Copy to clipboard React component

Based on copy-to-clipboard

Would try to use execCommand with fallback to IE specific clipboardData interface and finally, fallback to simple prompt with proper text content & 'Copy to clipboard: Ctrl+C, Enter'

Copy to clipboard

Installation

NPM

npm install --save react-copy-to-clipboard

Don't forget to manually install peer dependencies (react) if you use npm@3.

1998 Script Tag:

<script src="https://unpkg.com/react@16.0.0/umd/react.production.min.js"></script>
<script src="https://unpkg.com/react-copy-to-clipboard/build/react-copy-to-clipboard.js"></script>
(Module exposed as `CopyToClipboard`)

Live design system demo

https://www.jinno.io/app/18/

Simple web demo

http://nkbt.github.io/react-copy-to-clipboard

Codepen demo

http://codepen.io/nkbt/pen/eNPoQv

Usage

import React from 'react';
import ReactDOM from 'react-dom';
import {CopyToClipboard} from 'react-copy-to-clipboard';

class App extends React.Component {
  state = {
    value: '',
    copied: false,
  };

  render() {
    return (
      <div>
        <input value={this.state.value}
          onChange={({target: {value}}) => this.setState({value, copied: false})} />

        <CopyToClipboard text={this.state.value}
          onCopy={() => this.setState({copied: true})}>
          <span>Copy to clipboard with span</span>
        </CopyToClipboard>

        <CopyToClipboard text={this.state.value}
          onCopy={() => this.setState({copied: true})}>
          <button>Copy to clipboard with button</button>
        </CopyToClipboard>

        {this.state.copied ? <span style={{color: 'red'}}>Copied.</span> : null}
      </div>
    );
  }
}

const appRoot = document.createElement('div');
document.body.appendChild(appRoot);
ReactDOM.render(<App />, appRoot);

Options

text: PropTypes.string.isRequired

Text to be copied to clipboard

onCopy: PropTypes.func

Optional callback, will be called when text is copied

onCopy(text, result)

result (bool): Returns true if copied successfully, else false.

options: PropTypes.shape({debug: bool, message: string})

Optional copy-to-clipboard options.

See API docs for details

children: PropTypes.node.isRequired

CopyToClipboard is a simple wrapping component, it does not render any tags, so it requires the only child element to be present, which will be used to capture clicks.

<CopyToClipboard text="Hello!">
  <button>Copy to clipboard</button>
</CopyToClipboard>

Development and testing

Currently is being developed and tested with the latest stable Node 8 on OSX.

To run example covering all CopyToClipboard features, use yarn start, which will compile example/Example.js

git clone git@github.com:nkbt/react-copy-to-clipboard.git
cd react-copy-to-clipboard
yarn install
yarn start

# then
open http://localhost:8080

Tests

# to run ESLint check
yarn lint

# to run tests
yarn test

# to run end-to-end tests
# first, run `selenium/standalone-firefox:3.4.0` docker image
docker run -p 4444:4444 selenium/standalone-firefox:3.4.0
# then run test
yarn e2e

License

MIT