UNPKG

7.27 kBMarkdownView Raw
1# clipboard.js
2
3[![Build Status](http://img.shields.io/travis/zenorocha/clipboard.js/master.svg?style=flat)](https://travis-ci.org/zenorocha/clipboard.js)
4![Killing Flash](https://img.shields.io/badge/killing-flash-brightgreen.svg?style=flat)
5
6> Modern copy to clipboard. No Flash. Just 3kb gzipped.
7
8<a href="https://clipboardjs.com/"><img width="728" src="https://cloud.githubusercontent.com/assets/398893/16165747/a0f6fc46-349a-11e6-8c9b-c5fd58d9099c.png" alt="Demo"></a>
9
10## Why
11
12Copying text to the clipboard shouldn't be hard. It shouldn't require dozens of steps to configure or hundreds of KBs to load. But most of all, it shouldn't depend on Flash or any bloated framework.
13
14That's why clipboard.js exists.
15
16## Install
17
18You can get it on npm.
19
20```
21npm install clipboard --save
22```
23
24Or bower, too.
25
26```
27bower install clipboard --save
28```
29
30If you're not into package management, just [download a ZIP](https://github.com/zenorocha/clipboard.js/archive/master.zip) file.
31
32## Setup
33
34First, include the script located on the `dist` folder or load it from [a third-party CDN provider](https://github.com/zenorocha/clipboard.js/wiki/CDN-Providers).
35
36```html
37<script src="dist/clipboard.min.js"></script>
38```
39
40Now, you need to instantiate it by [passing a DOM selector](https://github.com/zenorocha/clipboard.js/blob/master/demo/constructor-selector.html#L18), [HTML element](https://github.com/zenorocha/clipboard.js/blob/master/demo/constructor-node.html#L16-L17), or [list of HTML elements](https://github.com/zenorocha/clipboard.js/blob/master/demo/constructor-nodelist.html#L18-L19).
41
42```js
43new Clipboard('.btn');
44```
45
46Internally, we need to fetch all elements that matches with your selector and attach event listeners for each one. But guess what? If you have hundreds of matches, this operation can consume a lot of memory.
47
48For this reason we use [event delegation](http://stackoverflow.com/questions/1687296/what-is-dom-event-delegation) which replaces multiple event listeners with just a single listener. After all, [#perfmatters](https://twitter.com/hashtag/perfmatters).
49
50# Usage
51
52We're living a _declarative renaissance_, that's why we decided to take advantage of [HTML5 data attributes](https://developer.mozilla.org/en-US/docs/Web/Guide/HTML/Using_data_attributes) for better usability.
53
54### Copy text from another element
55
56A pretty common use case is to copy content from another element. You can do that by adding a `data-clipboard-target` attribute in your trigger element.
57
58The value you include on this attribute needs to match another's element selector.
59
60<a href="https://clipboardjs.com/#example-target"><img width="473" alt="example-2" src="https://cloud.githubusercontent.com/assets/398893/9983467/a4946aaa-5fb1-11e5-9780-f09fcd7ca6c8.png"></a>
61
62```html
63<!-- Target -->
64<input id="foo" value="https://github.com/zenorocha/clipboard.js.git">
65
66<!-- Trigger -->
67<button class="btn" data-clipboard-target="#foo">
68 <img src="assets/clippy.svg" alt="Copy to clipboard">
69</button>
70```
71
72### Cut text from another element
73
74Additionally, you can define a `data-clipboard-action` attribute to specify if you want to either `copy` or `cut` content.
75
76If you omit this attribute, `copy` will be used by default.
77
78<a href="https://clipboardjs.com/#example-action"><img width="473" alt="example-3" src="https://cloud.githubusercontent.com/assets/398893/10000358/7df57b9c-6050-11e5-9cd1-fbc51d2fd0a7.png"></a>
79
80```html
81<!-- Target -->
82<textarea id="bar">Mussum ipsum cacilds...</textarea>
83
84<!-- Trigger -->
85<button class="btn" data-clipboard-action="cut" data-clipboard-target="#bar">
86 Cut to clipboard
87</button>
88```
89
90As you may expect, the `cut` action only works on `<input>` or `<textarea>` elements.
91
92### Copy text from attribute
93
94Truth is, you don't even need another element to copy its content from. You can just include a `data-clipboard-text` attribute in your trigger element.
95
96<a href="https://clipboardjs.com/#example-text"><img width="147" alt="example-1" src="https://cloud.githubusercontent.com/assets/398893/10000347/6e16cf8c-6050-11e5-9883-1c5681f9ec45.png"></a>
97
98```html
99<!-- Trigger -->
100<button class="btn" data-clipboard-text="Just because you can doesn't mean you should — clipboard.js">
101 Copy to clipboard
102</button>
103```
104
105## Events
106
107There are cases where you'd like to show some user feedback or capture what has been selected after a copy/cut operation.
108
109That's why we fire custom events such as `success` and `error` for you to listen and implement your custom logic.
110
111```js
112var clipboard = new Clipboard('.btn');
113
114clipboard.on('success', function(e) {
115 console.info('Action:', e.action);
116 console.info('Text:', e.text);
117 console.info('Trigger:', e.trigger);
118
119 e.clearSelection();
120});
121
122clipboard.on('error', function(e) {
123 console.error('Action:', e.action);
124 console.error('Trigger:', e.trigger);
125});
126```
127
128For a live demonstration, open this [site](https://clipboardjs.com/) and just your console :)
129
130## Advanced Options
131
132If you don't want to modify your HTML, there's a pretty handy imperative API for you to use. All you need to do is declare a function, do your thing, and return a value.
133
134For instance, if you want to dynamically set a `target`, you'll need to return a Node.
135
136```js
137new Clipboard('.btn', {
138 target: function(trigger) {
139 return trigger.nextElementSibling;
140 }
141});
142```
143
144If you want to dynamically set a `text`, you'll return a String.
145
146```js
147new Clipboard('.btn', {
148 text: function(trigger) {
149 return trigger.getAttribute('aria-label');
150 }
151});
152```
153
154Also, if you are working with single page apps, you may want to manage the lifecycle of the DOM more precisely. Here's how you clean up the events and objects that we create.
155
156```js
157var clipboard = new Clipboard('.btn');
158clipboard.destroy();
159```
160
161## Browser Support
162
163This library relies on both [Selection](https://developer.mozilla.org/en-US/docs/Web/API/Selection) and [execCommand](https://developer.mozilla.org/en-US/docs/Web/API/Document/execCommand) APIs. The first one is [supported by all browsers](http://caniuse.com/#search=selection) while the second one is supported in the following browsers.
164
165| <img src="https://clipboardjs.com/assets/images/chrome.png" width="48px" height="48px" alt="Chrome logo"> | <img src="https://clipboardjs.com/assets/images/edge.png" width="48px" height="48px" alt="Edge logo"> | <img src="https://clipboardjs.com/assets/images/firefox.png" width="48px" height="48px" alt="Firefox logo"> | <img src="https://clipboardjs.com/assets/images/ie.png" width="48px" height="48px" alt="Internet Explorer logo"> | <img src="https://clipboardjs.com/assets/images/opera.png" width="48px" height="48px" alt="Opera logo"> | <img src="https://clipboardjs.com/assets/images/safari.png" width="48px" height="48px" alt="Safari logo"> |
166|:---:|:---:|:---:|:---:|:---:|:---:|
167| 42+ ✔ | 12+ ✔ | 41+ ✔ | 9+ ✔ | 29+ ✔ | 10+ ✔ |
168
169The good news is that clipboard.js gracefully degrades if you need to support older browsers. All you have to do is show a tooltip saying `Copied!` when `success` event is called and `Press Ctrl+C to copy` when `error` event is called because the text is already selected.
170
171## License
172
173[MIT License](http://zenorocha.mit-license.org/) © Zeno Rocha