clipboard.js/README.md

144 lines
5.9 KiB
Markdown
Raw Normal View History

2015-09-19 02:34:09 +03:00
# clipboard.js
2015-09-25 10:28:35 +03:00
> A modern approach to copy text to the clipboard. No Flash. No dependencies. Just 2kb.
2015-09-21 02:08:56 +03:00
<a href="http://zenorocha.github.io/clipboard.js/"><img width="728" src="https://cloud.githubusercontent.com/assets/398893/9983535/5ab0a950-5fb4-11e5-9602-e73c0b661883.jpg" alt="Demo"></a>
2015-09-19 02:34:09 +03:00
2015-09-21 22:44:16 +03:00
## Why
Copy 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.
That's why clipboard.js exists.
2015-09-19 02:34:09 +03:00
## Install
2015-09-21 02:08:56 +03:00
You can get it on npm.
```
npm install clipboard --save
```
Or bower, too.
2015-09-19 02:34:09 +03:00
```
bower install clipboard --save
```
2015-09-21 02:08:56 +03:00
If you're not into package management, just [download a ZIP](https://github.com/zenorocha/clipboard.js/archive/master.zip) file.
2015-09-19 02:34:09 +03:00
2015-09-21 02:08:56 +03:00
## Setup
First, include the script located on the `dist` folder
```html
<script src="dist/clipboard.min.js"></script>
```
2015-09-19 02:34:09 +03:00
2015-09-25 10:28:35 +03:00
Now, you need to instantiate it using a DOM selector. This selector corresponds to the trigger element(s), for example `<button class="btn">`.
2015-09-19 02:34:09 +03:00
```js
new Clipboard('.btn');
```
2015-09-25 10:28:35 +03:00
Internally, 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.
2015-09-21 02:08:56 +03:00
2015-09-25 10:28:35 +03:00
For 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).
2015-09-21 02:08:56 +03:00
2015-09-25 10:28:35 +03:00
# Usage
2015-09-21 02:08:56 +03:00
2015-09-25 10:28:35 +03:00
We'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.
2015-09-19 02:34:09 +03:00
2015-09-21 02:08:56 +03:00
### Copy text from another element
2015-09-25 10:28:35 +03:00
A pretty common use case is to copy content from another element. You can do that by adding a `data-target` attribute in your trigger element.
2015-09-21 02:08:56 +03:00
The value you include on this attribute needs to match another's element `id` attribute.
2015-09-25 10:28:35 +03:00
<a href="http://zenorocha.github.io/clipboard.js/#demo-target"><img width="473" alt="example-2" src="https://cloud.githubusercontent.com/assets/398893/9983467/a4946aaa-5fb1-11e5-9780-f09fcd7ca6c8.png"></a>
2015-09-19 02:34:09 +03:00
```html
2015-09-21 02:08:56 +03:00
<!-- Target -->
<input id="foo" value="https://github.com/zenorocha/clipboard.js.git">
2015-09-21 02:08:56 +03:00
<!-- Trigger -->
<button class="btn" data-target="foo">
<img src="assets/clippy.svg" alt="Copy to clipboard">
</button>
2015-09-19 02:34:09 +03:00
```
2015-09-21 02:08:56 +03:00
### Cut text from another element
Additionally, you can define a `data-action` attribute to specify if you want to either `copy` or `cut` content.
If you omit this attribute, `copy` will be used by default.
2015-09-25 10:28:35 +03:00
<a href="http://zenorocha.github.io/clipboard.js/#demo-action"><img width="473" alt="example-3" src="https://cloud.githubusercontent.com/assets/398893/10000358/7df57b9c-6050-11e5-9cd1-fbc51d2fd0a7.png"></a>
2015-09-19 04:05:49 +03:00
```html
2015-09-21 02:08:56 +03:00
<!-- Target -->
<textarea id="bar">Mussum ipsum cacilds...</textarea>
2015-09-21 02:08:56 +03:00
<!-- Trigger -->
<button class="btn" data-action="cut" data-target="bar">
Cut to clipboard
2015-09-21 02:08:56 +03:00
</button>
2015-09-19 04:05:49 +03:00
```
As you may expect, the `cut` action only works on `<input>` or `<textarea>` elements.
2015-09-25 10:28:35 +03:00
### Copy text from attribute
Truth is, you don't even need another element to copy its content from. You can just include a `data-text` attribute in your trigger element.
<a href="http://zenorocha.github.io/clipboard.js/#demo-text"><img width="147" alt="example-1" src="https://cloud.githubusercontent.com/assets/398893/10000347/6e16cf8c-6050-11e5-9883-1c5681f9ec45.png"></a>
```html
<!-- Trigger -->
<button class="btn" data-text="Just because you can doesn't mean you should — clipboard.js">
Copy to clipboard
</button>
```
2015-09-21 02:08:56 +03:00
## Events
2015-09-25 10:28:35 +03:00
There are cases where you'd like to show some user feedback or capture what has been selected after a copy/cut operation.
2015-09-21 02:08:56 +03:00
That's why we fire custom events such as `success` and `error` for you to listen and implement your custom logic.
2015-09-21 02:08:56 +03:00
```js
var clipboard = new Clipboard('.btn');
2015-09-25 10:28:35 +03:00
clipboard.on('success', function(e) {
console.info('Action:', e.action);
console.info('Text:', e.text);
console.info('Trigger:', e.trigger);
2015-09-21 02:08:56 +03:00
2015-09-25 10:28:35 +03:00
e.clearSelection();
});
clipboard.on('error', function(e) {
console.error('Action:', e.action);
console.error('Trigger:', e.trigger);
});
2015-09-21 02:08:56 +03:00
```
2015-09-25 10:28:35 +03:00
For a live demonstration, open this [site](http://zenorocha.github.io/clipboard.js/) and just your console :)
2015-09-19 02:34:09 +03:00
## Browser Support
2015-09-25 10:28:35 +03:00
This 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 second one is supported in the following browsers.
| <img src="http://zenorocha.github.io/clipboard.js/assets/images/chrome.png" width="48px" height="48px" alt="Chrome logo"> | <img src="http://zenorocha.github.io/clipboard.js/assets/images/firefox.png" width="48px" height="48px" alt="Firefox logo"> | <img src="http://zenorocha.github.io/clipboard.js/assets/images/ie.png" width="48px" height="48px" alt="Internet Explorer logo"> | <img src="http://zenorocha.github.io/clipboard.js/assets/images/opera.png" width="48px" height="48px" alt="Opera logo"> | <img src="http://zenorocha.github.io/clipboard.js/assets/images/safari.png" width="48px" height="48px" alt="Safari logo"> |
2015-09-19 02:34:09 +03:00
|:---:|:---:|:---:|:---:|:---:|
| 42+ ✔ | 41+ ✔ | 9+ ✔ | 29+ ✔ | Nope ✘ |
2015-09-19 02:34:09 +03:00
2015-09-25 10:28:35 +03:00
Although copy/cut operations with [execCommand](https://developer.mozilla.org/en-US/docs/Web/API/Document/execCommand) aren't supported on Safari yet (including mobile), it gracefully degrades because [Selection](https://developer.mozilla.org/en-US/docs/Web/API/Selection) is supported.
That means you can 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.
For a live demonstration, open this [site](http://zenorocha.github.io/clipboard.js/) on Safari.
2015-09-19 02:34:09 +03:00
## License
[MIT License](http://zenorocha.mit-license.org/) © Zeno Rocha