1 | # react-copy-to-clipboard [](https://www.npmjs.com/package/react-copy-to-clipboard)
|
---|
2 |
|
---|
3 | [](https://gitter.im/nkbt/help)
|
---|
4 |
|
---|
5 | [](https://circleci.com/gh/nkbt/react-copy-to-clipboard)
|
---|
6 | [](https://david-dm.org/nkbt/react-copy-to-clipboard)
|
---|
7 | [](https://david-dm.org/nkbt/react-copy-to-clipboard#info=devDependencies)
|
---|
8 |
|
---|
9 | Copy to clipboard React component
|
---|
10 |
|
---|
11 | Based on [copy-to-clipboard](https://npm.im/copy-to-clipboard)
|
---|
12 |
|
---|
13 | > 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'
|
---|
14 |
|
---|
15 |
|
---|
16 | 
|
---|
17 |
|
---|
18 |
|
---|
19 | ## Installation
|
---|
20 |
|
---|
21 | ### NPM
|
---|
22 |
|
---|
23 | ```sh
|
---|
24 | npm install --save react-copy-to-clipboard
|
---|
25 | ```
|
---|
26 |
|
---|
27 | Don't forget to manually install peer dependencies (`react`) if you use npm@3.
|
---|
28 |
|
---|
29 |
|
---|
30 | ### 1998 Script Tag:
|
---|
31 | ```html
|
---|
32 | <script src="https://unpkg.com/react@16.0.0/umd/react.production.min.js"></script>
|
---|
33 | <script src="https://unpkg.com/react-copy-to-clipboard/build/react-copy-to-clipboard.js"></script>
|
---|
34 | (Module exposed as `CopyToClipboard`)
|
---|
35 | ```
|
---|
36 |
|
---|
37 | ## Live design system demo
|
---|
38 |
|
---|
39 | [https://www.jinno.io/app/18/](https://www.jinno.io/app/18/?source=react-copy-to-clipboard)
|
---|
40 |
|
---|
41 | ## Simple web demo
|
---|
42 |
|
---|
43 | [http://nkbt.github.io/react-copy-to-clipboard](http://nkbt.github.io/react-copy-to-clipboard)
|
---|
44 |
|
---|
45 | ## Codepen demo
|
---|
46 |
|
---|
47 | [http://codepen.io/nkbt/pen/eNPoQv](http://codepen.io/nkbt/pen/eNPoQv?editors=0010)
|
---|
48 |
|
---|
49 | ## Usage
|
---|
50 | ```js
|
---|
51 | import React from 'react';
|
---|
52 | import ReactDOM from 'react-dom';
|
---|
53 | import {CopyToClipboard} from 'react-copy-to-clipboard';
|
---|
54 |
|
---|
55 | class App extends React.Component {
|
---|
56 | state = {
|
---|
57 | value: '',
|
---|
58 | copied: false,
|
---|
59 | };
|
---|
60 |
|
---|
61 | render() {
|
---|
62 | return (
|
---|
63 | <div>
|
---|
64 | <input value={this.state.value}
|
---|
65 | onChange={({target: {value}}) => this.setState({value, copied: false})} />
|
---|
66 |
|
---|
67 | <CopyToClipboard text={this.state.value}
|
---|
68 | onCopy={() => this.setState({copied: true})}>
|
---|
69 | <span>Copy to clipboard with span</span>
|
---|
70 | </CopyToClipboard>
|
---|
71 |
|
---|
72 | <CopyToClipboard text={this.state.value}
|
---|
73 | onCopy={() => this.setState({copied: true})}>
|
---|
74 | <button>Copy to clipboard with button</button>
|
---|
75 | </CopyToClipboard>
|
---|
76 |
|
---|
77 | {this.state.copied ? <span style={{color: 'red'}}>Copied.</span> : null}
|
---|
78 | </div>
|
---|
79 | );
|
---|
80 | }
|
---|
81 | }
|
---|
82 |
|
---|
83 | const appRoot = document.createElement('div');
|
---|
84 | document.body.appendChild(appRoot);
|
---|
85 | ReactDOM.render(<App />, appRoot);
|
---|
86 | ```
|
---|
87 |
|
---|
88 | ## Options
|
---|
89 |
|
---|
90 |
|
---|
91 | #### `text`: PropTypes.string.isRequired
|
---|
92 |
|
---|
93 | Text to be copied to clipboard
|
---|
94 |
|
---|
95 |
|
---|
96 | #### `onCopy`: PropTypes.func
|
---|
97 |
|
---|
98 | Optional callback, will be called when text is copied
|
---|
99 |
|
---|
100 | ```
|
---|
101 | onCopy(text, result)
|
---|
102 | ```
|
---|
103 | `result (bool)`: Returns `true` if copied successfully, else `false`.
|
---|
104 |
|
---|
105 |
|
---|
106 | #### `options`: PropTypes.shape({debug: bool, message: string})
|
---|
107 |
|
---|
108 | Optional [copy-to-clipboard](https://npm.im/copy-to-clipboard) options.
|
---|
109 |
|
---|
110 | See [API docs](https://npm.im/copy-to-clipboard#api) for details
|
---|
111 |
|
---|
112 | #### `children`: PropTypes.node.isRequired
|
---|
113 |
|
---|
114 | 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.
|
---|
115 |
|
---|
116 | ```js
|
---|
117 | <CopyToClipboard text="Hello!">
|
---|
118 | <button>Copy to clipboard</button>
|
---|
119 | </CopyToClipboard>
|
---|
120 | ```
|
---|
121 |
|
---|
122 | ## Development and testing
|
---|
123 |
|
---|
124 | Currently is being developed and tested with the latest stable `Node 8` on `OSX`.
|
---|
125 |
|
---|
126 | To run example covering all `CopyToClipboard` features, use `yarn start`, which will compile `example/Example.js`
|
---|
127 |
|
---|
128 | ```bash
|
---|
129 | git clone git@github.com:nkbt/react-copy-to-clipboard.git
|
---|
130 | cd react-copy-to-clipboard
|
---|
131 | yarn install
|
---|
132 | yarn start
|
---|
133 |
|
---|
134 | # then
|
---|
135 | open http://localhost:8080
|
---|
136 | ```
|
---|
137 |
|
---|
138 | ## Tests
|
---|
139 |
|
---|
140 | ```bash
|
---|
141 | # to run ESLint check
|
---|
142 | yarn lint
|
---|
143 |
|
---|
144 | # to run tests
|
---|
145 | yarn test
|
---|
146 |
|
---|
147 | # to run end-to-end tests
|
---|
148 | # first, run `selenium/standalone-firefox:3.4.0` docker image
|
---|
149 | docker run -p 4444:4444 selenium/standalone-firefox:3.4.0
|
---|
150 | # then run test
|
---|
151 | yarn e2e
|
---|
152 | ```
|
---|
153 |
|
---|
154 | ## License
|
---|
155 |
|
---|
156 | MIT
|
---|