There are many ways you can contribute, see below.
## Opening issues
Open an issue to report bugs or to propose new features.
- Reporting bugs: describe the bug as clearly as you can, including steps to reproduce, what happened and what you were expecting to happen. Also include browser version, OS and other related software's (npm, Node.js, etc) versions when applicable.
- Proposing features: explain the proposed feature, what it should do, why it is useful, how users should use it. Give us as much info as possible so it will be easier to discuss, access and implement the proposed feature. When you're unsure about a certain aspect of the feature, feel free to leave it open for others to discuss and find an appropriate solution.
## Proposing pull requests
Pull requests are very welcome. Note that if you are going to propose drastic changes, be sure to open an issue for discussion first, to make sure that your PR will be accepted before you spend effort coding it.
Fork the Clipboard.js repository, clone it locally and create a branch for your proposed bug fix or new feature. Avoid working directly on the master branch.
Implement your bug fix or feature, write tests to cover it and make sure all tests are passing (run a final `npm test` to make sure everything is correct). Then commit your changes, push your bug fix/feature branch to the origin (your forked repo) and open a pull request to the upstream (the repository you originally forked)'s master branch.
## Documentation
Documentation is extremely important and takes a fair deal of time and effort to write and keep updated. Please submit any and all improvements you can make to the repository's docs.
## Known issues
If you're using npm@3 you'll probably face some issues related to peerDependencies.
!function(t){if("object"==typeofexports&&"undefined"!=typeofmodule)module.exports=t();elseif("function"==typeofdefine&&define.amd)define([],t);else{vare;e="undefined"!=typeofwindow?window:"undefined"!=typeofglobal?global:"undefined"!=typeofself?self:this,e.Clipboard=t()}}(function(){vart,e,n;returnfunctiont(e,n,o){functioni(a,c){if(!n[a]){if(!e[a]){vars="function"==typeofrequire&&require;if(!c&&s)returns(a,!0);if(r)returnr(a,!0);varl=newError("Cannot find module '"+a+"'");throwl.code="MODULE_NOT_FOUND",l}varu=n[a]={exports:{}};e[a][0].call(u.exports,function(t){varn=e[a][1][t];returni(n?n:t)},u,u.exports,t,e,n,o)}returnn[a].exports}for(varr="function"==typeofrequire&&require,a=0;a<o.length;a++)i(o[a]);returni}({1:[function(t,e,n){varo=t("matches-selector");e.exports=function(t,e,n){for(vari=n?t:t.parentNode;i&&i!==document;){if(o(i,e))returni;i=i.parentNode}}},{"matches-selector":5}],2:[function(t,e,n){functiono(t,e,n,o,r){vara=i.apply(this,arguments);returnt.addEventListener(n,a,r),{destroy:function(){t.removeEventListener(n,a,r)}}}functioni(t,e,n,o){returnfunction(n){n.delegateTarget=r(n.target,e,!0),n.delegateTarget&&o.call(t,n)}}varr=t("closest");e.exports=o},{closest:1}],3:[function(t,e,n){n.node=function(t){returnvoid0!==t&&tinstanceofHTMLElement&&1===t.nodeType},n.nodeList=function(t){vare=Object.prototype.toString.call(t);returnvoid0!==t&&("[object NodeList]"===e||"[object HTMLCollection]"===e)&&"length"int&&(0===t.length||n.node(t[0]))},n.string=function(t){return"string"==typeoft||tinstanceofString},n.fn=function(t){vare=Object.prototype.toString.call(t);return"[object Function]"===e}},{}],4:[function(t,e,n){functiono(t,e,n){if(!t&&!e&&!n)thrownewError("Missing required arguments");if(!c.string(e))thrownewTypeError("Second argument must be a String");if(!c.fn(n))thrownewTypeError("Third argument must be a Function");if(c.node(t))returni(t,e,n);if(c.nodeList(t))returnr(t,e,n);if(c.string(t))returna(t,e,n);thrownewTypeError("First argument must be a String, HTMLElement, HTMLCollection, or NodeList")}functioni(t,e,n){returnt.addEventListener(e,n),{destroy:function(){t.removeEventListener(e,n)}}}functionr(t,e,n){returnArray.prototype.forEach.call(t,function(t){t.addEventListener(e,n)}),{destroy:function(){Array.prototype.forEach.call(t,function(t){t.removeEventListener(e,n)})}}}functiona(t,e,n){returns(document.body,t,e,n)}varc=t("./is"),s=t("delegate");e.exports=o},{"./is":3,delegate:2}],5:[function(t,e,n){functiono(t,e){if(r)returnr.call(t,e);for(varn=t.parentNode.querySelectorAll(e),o=0;o<n.length;++o)if(n[o]==t)return!0;return!1}vari=Element.prototype,r=i.matchesSelector||i.webkitMatchesSelector||i.mozMatchesSelector||i.msMatchesSelector||i.oMatchesSelector;e.exports=o},{}],6:[function(t,e,n){functiono(t){vare;if("INPUT"===t.nodeName||"TEXTAREA"===t.nodeName)t.focus(),t.setSelectionRange(0,t.value.length),e=t.value;else{t.hasAttribute("contenteditable")&&t.focus();varn=window.getSelection(),o=document.createRange();o.selectNodeContents(t),n.removeAllRanges(),n.addRange(o),e=n.toString()}returne}e.exports=o},{}],7:[function(t,e,n){functiono(){}o.prototype={on:function(t,e,n){varo=this.e||(this.e={});return(o[t]||(o[t]=[])).push({fn:e,ctx:n}),this},once:function(t,e,n){functiono(){i.off(t,o),e.apply(n,arguments)}vari=this;returno._=e,this.on(t,o,n)},emit:function(t){vare=[].slice.call(arguments,1),n=((this.e||(this.e={}))[t]||[]).slice(),o=0,i=n.length;for(o;i>o;o++)n[o].fn.apply(n[o].ctx,e);returnthis},off:function(t,e){varn=this.e||(this.e={}),o=n[t],i=[];if(o&&e)for(varr=0,a=o.length;a>r;r++)o[r].fn!==e&&o[r].fn._!==e&&i.push(o[r]);returni.length?n[t]=i:deleten[t],this}},e.exports=o},{}],8:[function(e,n,o){!function(i,r){if("function"==typeoft&&t.amd)t(["module","select"],r);elseif("undefined"!=typeofo)r(n,e("select"));else{vara={exports:{}};r(a,i.select),i.clipboardAction=a.exports}}(this,function(t,e){"use strict";functionn(t){returnt&&t.__esModule?t:{"default":t}}functiono(t,e){if(!(tinstanceofe))thrownewTypeError("Cannot call a class as a function")}vari=n(e),r="function"==typeofSymbol&&"symbol"==typeofSymbol.iterator?function(t){returntypeoft}:function(t){returnt&&"function"==typeofSymbol&&t.constructor===Symbol?"symbol":typeoft},a=function(){functiont(t,e){for(varn=0;n<e.length;n++){varo=e[n];o.enumerable=o.enumerable||!1,o.configurable=!0,"value"ino&&(o.writable=!0),Object.defineProperty(t,o.key,o)}}returnfunction(e,n,o){returnn&&t(e.prototype,n),o&&t(e,o),e}}(),c=function(){functiont(e){o(this,t),this.resolveOptions(e),this.initSelection()}returnt.prototype.resolveOptions=functiont(){vare=arguments.length<=0||void0===arguments[0]?{}:arguments[0];this.action=e.action,this.emitter=e.emitter,this.target=e.target,this.text=e.text,this.trigger=e.trigger,this.selectedText=""},t.prototype.initSelection=functiont(){this.text?this.selectFake():this.target&&this.selectTarget()},t.prototype.selectFake=functiont(){vare=this,n="rtl"==document.documentElement.getAttribute("dir");this.removeFake(),this.fakeHandlerCallback=function(){returne.removeFake()},this.fakeHandler=document.body.addEventListener("click",this.fakeHandlerCallback)||!0,this.fakeElem=document.createElement("textarea"),this.fakeElem.style.fontSize="12pt",this.fakeElem.style.border="0",this.fakeElem.style.padding="0",this.fakeElem.style.margin="0",this.fakeElem.style.position="absolute",this.fakeElem.style[n?"right":"left"]="-9999px",this.fakeElem.style.top=(window.pageYOffset||document.documentElement.scrollTop)+"px",this.fakeElem.setAttribute("readonly",""),this.fakeElem.value=this.text,document.body.appendChild(this.fakeElem),this.selectedText=(0,i.default)(this.fakeElem),this.copyText()},t.prototype.removeFake=functiont(){this.fakeHandler&&(document.body.removeEventListener("click",this.fakeHandlerCallback),this.fakeHandler=null,this.fakeHandlerCallback=null),this.fakeElem&&(document.body.removeChild(this.fakeElem),this.fakeElem=null)},t.prototype.selectTarget=functiont(){this.selectedText=(0,i.default)(this.target),this.copyText()},t.prototype.copyText=functiont(){vare=void0;try{e=document.execCommand(this.action)}catch(n){e=!1}this.handleResult(e)},t.prototype.handleResult=functiont(e){e?this.emitter.emit("success",{action:this.action,text:this.selectedText,trigger:this.trigger,clearSelection:this.clearSelection.bind(this)}):this.emitter.emit("error",{action:this.action,trigger:this.trigger,clearSelection:this.clearSelection.bind(this)})},t.prototype.clearSelection=functiont(){this.target&&this.target.blur(),window.getSelection().removeAllRanges()},t.prototype.destroy=functiont(){this.removeFake()},a(t,[{key:"action",set:functiont(){vare=arguments.length<=0||void0===arguments[0]?"copy":arguments[0];if(this._action=e,"copy"!==this._action&&"cut"!==this._action)thrownewError('Invalid "action" value, use either "copy" or "cut"')},get:functiont(){returnthis._action}},{key:"target",set:functiont(e){if(void0!==e){if(!e||"object"!==("undefined"==typeofe?"undefined":r(e))||1!==e.nodeType)thrownewError('Invalid "target" value, use a valid Element');if("copy"===this.action&&e.hasAttribute("disabled"))thrownewError('Invalid "target" attribute. Please use "readonly" instead of "disabled" attribute');if("cut"===this.action&&(e.hasAttribute("readonly")||e.hasAttribute("disabled")))thrownewError('Invalid "target" attribute. You can\'t cut text from elements with "readonly" or "disabled" attributes');this._target=e}},get:functiont(){returnthis._target}}]),t}();t.exports=c})},{select:6}],9:[function(e,n,o){!function(i,r){if("function"==typeoft&&t.amd)t(["module","./clipboard-action","tiny-emitter","good-listener"],r);elseif("undefined"!=typeofo)r(n,e("./clipboard-action"),e("tiny-emitter"),e("good-listener"));else{vara={exports:{}};r(a,i.clipboardAction,i.tinyEmitter,i.goodListener),i.clipboard=a.exports}}(this,function(t,e,n,o){"use strict";functioni(t){returnt&&t.__esModule?t:{"default":t}}functionr(t,e){if(!(tinstanceofe))thrownewTypeError("Cannot call a class as a function")}functiona(t,e){if(!t)thrownewReferenceError("this hasn't been initialised - super() hasn't been called");return!e||"object"!=typeofe&&"function"!=typeofe?t:e}functionc(t,e){if("function"!=typeofe&&null!==e)thrownewTypeError("Super expression must either be null or a function, not "+typeofe);t.prototype=Object.create(e&&e.prototype,{constructor:{value:t,enumerable:!1,writable:!0,configurable:!0}}),e&&(Object.setPrototypeOf?Object.setPrototypeOf(t,e):t.__proto__=e)}functions(t,e){varn="data-clipboard-"+t;if(e.hasAttribute(n))returne.getAttribute(n)}varl=i(e),u=i(n),f=i(o),d=function(t){functione(n,o){r(this,e);vari=a(this,t.call(this));returni.resolveOptions(o),i.listenClick(n),i}returnc(e,t),e.prototype.resolveOptions=functiont(){vare=arguments.length<=0||void0===arguments[0]?{}:arguments[0];this.action="function"==typeofe.action?e.action:this.defaultAction,this.target="function"==typeofe.target?e.target:this.defaultTarget,this.text="function"==typeofe.text?e.text:this.defaultText},e.prototype.listenClick=functiont(e){varn=this;this.listener=(0,f.default)(e,"click",function(t){returnn.onClick(t)})},e.prototype.onClick=functiont(e){varn=e.delegateTarget||e.currentTarget;this.clipboardAction&&(this.clipboardAction=null),this.clipboardAction=newl.default({action:this.action(n),target:this.target(n),text:this.text(n),trigger:n,emitter:this})},e.prototype.defaultAction=functiont(e){returns("action",e)},e.prototype.defaultTarget=functiont(e){varn=s("target",e);returnn?document.querySelector(n):void0},e.prototype.defaultText=functiont(e){returns("text",e)},e.prototype.destroy=functiont(){this.listener.destroy(),this.clipboardAction&&(this.clipboardAction.destroy(),this.clipboardAction=null)},e}(u.default);t.exports=d})},{"./clipboard-action":8,"good-listener":4,"tiny-emitter":7}]},{},[9])(9)});
Copying 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.
## Install
You can get it on npm.
```
npm install clipboard --save
```
Or bower, too.
```
bower install clipboard --save
```
If you're not into package management, just [download a ZIP](https://github.com/zenorocha/clipboard.js/archive/master.zip) file.
## Setup
First, 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).
```html
<script src="dist/clipboard.min.js"></script>
```
Now, 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).
```js
newClipboard('.btn');
```
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.
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).
# Usage
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.
### Copy text from another element
A 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.
The value you include on this attribute needs to match another's element selector.
As you may expect, the `cut` action only works on `<input>` or `<textarea>` elements.
### Copy text from attribute
Truth 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.
<buttonclass="btn"data-clipboard-text="Just because you can doesn't mean you should — clipboard.js">
Copy to clipboard
</button>
```
## Events
There are cases where you'd like to show some user feedback or capture what has been selected after a copy/cut operation.
That's why we fire custom events such as `success` and `error` for you to listen and implement your custom logic.
```js
varclipboard=newClipboard('.btn');
clipboard.on('success',function(e){
console.info('Action:',e.action);
console.info('Text:',e.text);
console.info('Trigger:',e.trigger);
e.clearSelection();
});
clipboard.on('error',function(e){
console.error('Action:',e.action);
console.error('Trigger:',e.trigger);
});
```
For a live demonstration, open this [site](http://clipboardjs.com/) and just your console :)
## Advanced Options
If 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.
For instance, if you want to dynamically set a `target`, you'll need to return a Node.
```js
newClipboard('.btn',{
target:function(trigger){
returntrigger.nextElementSibling;
}
});
```
If you want to dynamically set a `text`, you'll return a String.
```js
newClipboard('.btn',{
text:function(trigger){
returntrigger.getAttribute('aria-label');
}
});
```
Also, 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.
```js
varclipboard=newClipboard('.btn');
clipboard.destroy();
```
## Browser Support
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.
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 Command+C to copy` when `error` event is called because the text is already selected.
For a live demonstration, open this [site](http://clipboardjs.com) on Safari.