• Stars
    star
    2,290
  • Rank 19,391 (Top 0.4 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created almost 9 years ago
  • Updated about 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Copy-to-clipboard React component

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/[email protected]/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 [email protected]: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

More Repositories

1

react-collapse

Component-wrapper for collapse animation with react-motion for elements with variable (and dynamic) height
JavaScript
1,116
star
2

react-debounce-input

React component that renders Input with debounced onChange
JavaScript
447
star
3

react-height

Component-wrapper to determine and report children elements height
JavaScript
181
star
4

component-router

Redux-based component routing solution
JavaScript
93
star
5

react-component-template

Base for React Components
JavaScript
56
star
6

esnext-quickstart

Bring compilation and code validation to your front-end development process
JavaScript
55
star
7

react-works

πŸ‡ Monorepo for React components
JavaScript
46
star
8

react-page-click

πŸ„πŸ» **MOVED** React component-wrapper to detect page clicks (outside of wrapped element).
JavaScript
45
star
9

react-interval

πŸ„πŸ» **MOVED** Safe React wrapper for setInterval
37
star
10

ng-modular

Truly modular Angular app, built with Webpack. Slides
JavaScript
36
star
11

react-motion-loop

Looping animation for react-motion Spring
JavaScript
28
star
12

react-text-filter

πŸ„πŸ» **MOVED** React component that renders filtering Input
18
star
13

react-swap

πŸ„πŸ» **MOVED** React component-wrapper to swap one element with another and back
18
star
14

nightwatch-autorun

Zero-configuration runner to automatically install and run End-to-End tests with Nightwatch and Selenium
JavaScript
14
star
15

react-normalized-select

πŸ„πŸ» **MOVED** Normalized Select wrapper, returns array of values in multiple mode
14
star
16

react-element-resize

πŸ„πŸ» **MOVED** Element resize sensor with optional debounce
10
star
17

vagrant-node

Bootstrap for Node development.
Scheme
8
star
18

css-modules-component-template

Styled UI component template with CSS-Modules
JavaScript
5
star
19

minicat

Windows compatible `cat` shell command polyfill
JavaScript
5
star
20

build

Automatic building with Grunt
JavaScript
3
star
21

geovis

GeoVis
JavaScript
3
star
22

ui

Monorepo for UI components, React hooks, elements, no build, only sources
JavaScript
2
star
23

node-girls

🎀 Slides for NodeGirls Sydney @ 1.0.0
JavaScript
2
star
24

un-responsive-demo

Demo app for Un.Responsive talk
JavaScript
2
star
25

wtf-tf

Terraform for beginners, demystifying CI/CD for developers
HCL
2
star
26

itunes-scrobbler

Scrobble retrospectively plays and loved tracks to Last.FM from iTunes database.
JavaScript
2
star
27

cf-react-component-template

🚫 **DEPRECATED** React component template scaffold
JavaScript
2
star
28

gleam

Reusable models for the Node server and browsers
JavaScript
2
star
29

flux-common-store

Common Flux store
JavaScript
2
star
30

react-component-router

Official ComponentRouter bindings for React
JavaScript
2
star
31

gleam-demo

Simple chat application to demo Gleam library.
CSS
2
star
32

react-bulkhead

πŸ„πŸ» **MOVED** React component to allow 3rd party components to operate over DOM-tree (d3, three.js)
2
star
33

sydjs2

SydJS talk: Modular, please
1
star
34

oss

OpenSource Talk
JavaScript
1
star
35

scrobbler

Manual bulk scrobbler from text or json file to LastFM
JavaScript
1
star
36

opera-github-sidebar

Opera sidebar plugin for Github
JavaScript
1
star
37

haskell-book

HaskellBook exercises
Haskell
1
star
38

v7tabs

Restyled V7 Tabs Opera extension
JavaScript
1
star
39

sublime2-todo

TODO language and syntax highlight for Sublime Text 2
1
star
40

react-jasmine-matchers

🚫 **DEPRECATED** Custom matchers for ReactTestUtils
JavaScript
1
star
41

puma

Fast and simple framework for modular application
JavaScript
1
star
42

nnj

Node Ninjas talk
JavaScript
1
star
43

un-responsive

Un.Responsive talk for ReactSydney
JavaScript
1
star
44

cli-tester

Lean promisified wrapper to test NodeJS CLI scripts
JavaScript
1
star
45

geovis-standalone

GeoVis Standalone
JavaScript
1
star
46

event-done

Extension of EventEmitter which allows to pass callback that will be invoked when all listeners completed
JavaScript
1
star
47

wscli

WebSocket CLI util
JavaScript
1
star
48

css-modules-component-template-test

Example consumer app for css-modules-component-template
1
star