Socket
Socket
Sign inDemoInstall

react-clipboard.js

Package Overview
Dependencies
14
Maintainers
1
Versions
44
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    react-clipboard.js

Clipboard.js wrapper for ReactJS


Version published
Weekly downloads
29K
decreased by-17.94%
Maintainers
1
Install size
421 kB
Created
Weekly downloads
 

Readme

Source

React-Clipboard

React wrapper for clipboard.js

Build
Status Dependency
Status

Installation

$ npm i --save react-clipboard.js

Usage

You can use clipboard.js original data-* attributes:

import ReactDOM from 'react-dom';
import React, { Component } from 'react';
import Clipboard from 'react-clipboard.js';

class MyView extends Component {
  render() {
    return (
      <Clipboard data-clipboard-text="I'll be copied">
        copy to clipboard
      </Clipboard>
    );
  }
}

ReactDOM.render(<MyView/>, document.getElementById('app'));
  • If you want to provide any constructor option as in new Clipboard('#id', options), you may use option-* attributes

  • callbacks will be connected via on* attributes (such as onSuccess)

import ReactDOM from 'react-dom';
import React, { Component } from 'react';
import Clipboard from 'react-clipboard.js';

class MyView extends Component {
  constructor() {
    super();

    this.onSuccess = this.onSuccess.bind(this);
    this.getText = this.getText.bind(this);
  }

  onSuccess() {
    console.info('successfully copied');
  }

  getText() {
    return 'I\'ll be copied';
  }

  render() {
    // Providing option-text as this.getText works the same way as providing:
    //
    // var clipboard = new Clipboard('#anything', {
    //   text: this.getText,
    // });
    //
    // onSuccess works as a 'success' callback:
    //
    // clipboard.on('success', this.onSuccess);
    return (
      <Clipboard option-text={this.getText} onSuccess={this.onSuccess}>
        copy to clipboard
      </Clipboard>
    );
  }
}

ReactDOM.render(<MyView/>, document.getElementById('app'));

Custom HTML tags may be used as well (you can use custom components as well): Beware: Stateless/Functional components are yet to be added

import ReactDOM from 'react-dom';
import React, { Component } from 'react';
import Clipboard from 'react-clipboard.js';

class MyView extends Component {
  render() {
    // Clipboard is now rendered as an '<a>'
    return (
      <Clipboard component="a" button-href="#" data-clipboard-text="I'll be copied">
        copy to clipboard
      </Clipboard>
    );
  }
}

ReactDOM.render(<MyView/>, document.getElementById('app'));

Default html properties may be passed with the button-* pattern:

import ReactDOM from 'react-dom';
import React, { Component } from 'react';
import Clipboard from 'react-clipboard.js';

class MyView extends Component {
  render() {
    return (
      <Clipboard data-clipboard-text="I'll be copied" button-title="I'm a tooltip">
        copy to clipboard
      </Clipboard>
    );
  }
}

ReactDOM.render(<MyView/>, document.getElementById('react-body'));

License

This code is released under CC0 (Public Domain)

Keywords

FAQs

Last updated on 12 Sep 2019

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc