d7769018d757d0bd3e47f8f2a36d7f8ad7a47dd6ee2cabedd09980e6aafdbca0a643acfaa3014a726cf2630bebd5ec098e7a02f750153b2ba93bf7cc8aa127 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. # clipboard.js
  2. ![Build Status](https://github.com/zenorocha/clipboard.js/workflows/build/badge.svg)
  3. ![Killing Flash](https://img.shields.io/badge/killing-flash-brightgreen.svg?style=flat)
  4. > Modern copy to clipboard. No Flash. Just 3kb gzipped.
  5. <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>
  6. ## Why
  7. 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.
  8. That's why clipboard.js exists.
  9. ## Install
  10. You can get it on npm.
  11. ```
  12. npm install clipboard --save
  13. ```
  14. Or if you're not into package management, just [download a ZIP](https://github.com/zenorocha/clipboard.js/archive/master.zip) file.
  15. ## Setup
  16. 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).
  17. ```html
  18. <script src="dist/clipboard.min.js"></script>
  19. ```
  20. 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).
  21. ```js
  22. new ClipboardJS('.btn');
  23. ```
  24. 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.
  25. For this reason we use [event delegation](https://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).
  26. # Usage
  27. 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.
  28. ### Copy text from another element
  29. 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.
  30. The value you include on this attribute needs to match another's element selector.
  31. <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>
  32. ```html
  33. <!-- Target -->
  34. <input id="foo" value="https://github.com/zenorocha/clipboard.js.git" />
  35. <!-- Trigger -->
  36. <button class="btn" data-clipboard-target="#foo">
  37. <img src="assets/clippy.svg" alt="Copy to clipboard" />
  38. </button>
  39. ```
  40. ### Cut text from another element
  41. Additionally, you can define a `data-clipboard-action` attribute to specify if you want to either `copy` or `cut` content.
  42. If you omit this attribute, `copy` will be used by default.
  43. <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>
  44. ```html
  45. <!-- Target -->
  46. <textarea id="bar">Mussum ipsum cacilds...</textarea>
  47. <!-- Trigger -->
  48. <button class="btn" data-clipboard-action="cut" data-clipboard-target="#bar">
  49. Cut to clipboard
  50. </button>
  51. ```
  52. As you may expect, the `cut` action only works on `<input>` or `<textarea>` elements.
  53. ### Copy text from attribute
  54. 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.
  55. <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>
  56. ```html
  57. <!-- Trigger -->
  58. <button
  59. class="btn"
  60. data-clipboard-text="Just because you can doesn't mean you should — clipboard.js"
  61. >
  62. Copy to clipboard
  63. </button>
  64. ```
  65. ## Events
  66. There are cases where you'd like to show some user feedback or capture what has been selected after a copy/cut operation.
  67. That's why we fire custom events such as `success` and `error` for you to listen and implement your custom logic.
  68. ```js
  69. var clipboard = new ClipboardJS('.btn');
  70. clipboard.on('success', function (e) {
  71. console.info('Action:', e.action);
  72. console.info('Text:', e.text);
  73. console.info('Trigger:', e.trigger);
  74. e.clearSelection();
  75. });
  76. clipboard.on('error', function (e) {
  77. console.error('Action:', e.action);
  78. console.error('Trigger:', e.trigger);
  79. });
  80. ```
  81. For a live demonstration, go to this [site](https://clipboardjs.com/) and open your console.
  82. ## Tooltips
  83. Each application has different design needs, that's why clipboard.js does not include any CSS or built-in tooltip solution.
  84. The tooltips you see on the [demo site](https://clipboardjs.com/) were built using [GitHub's Primer](https://primer.style/css/components/tooltips). You may want to check that out if you're looking for a similar look and feel.
  85. ## Advanced Options
  86. 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.
  87. For instance, if you want to dynamically set a `target`, you'll need to return a Node.
  88. ```js
  89. new ClipboardJS('.btn', {
  90. target: function (trigger) {
  91. return trigger.nextElementSibling;
  92. },
  93. });
  94. ```
  95. If you want to dynamically set a `text`, you'll return a String.
  96. ```js
  97. new ClipboardJS('.btn', {
  98. text: function (trigger) {
  99. return trigger.getAttribute('aria-label');
  100. },
  101. });
  102. ```
  103. For use in Bootstrap Modals or with any other library that changes the focus you'll want to set the focused element as the `container` value.
  104. ```js
  105. new ClipboardJS('.btn', {
  106. container: document.getElementById('modal'),
  107. });
  108. ```
  109. 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.
  110. ```js
  111. var clipboard = new ClipboardJS('.btn');
  112. clipboard.destroy();
  113. ```
  114. ## Browser Support
  115. 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 first one is [supported by all browsers](https://caniuse.com/#search=selection) while the second one is supported in the following browsers.
  116. | <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"> |
  117. | :-------------------------------------------------------------------------------------------------------: | :---------------------------------------------------------------------------------------------------: | :---------------------------------------------------------------------------------------------------------: | :--------------------------------------------------------------------------------------------------------------: | :-----------------------------------------------------------------------------------------------------: | :-------------------------------------------------------------------------------------------------------: |
  118. | 42+ ✔ | 12+ ✔ | 41+ ✔ | 9+ ✔ | 29+ ✔ | 10+ ✔ |
  119. The 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.
  120. You can also check if clipboard.js is supported or not by running `ClipboardJS.isSupported()`, that way you can hide copy/cut buttons from the UI.
  121. ## Bonus
  122. A browser extension that adds a "copy to clipboard" button to every code block on _GitHub, MDN, Gist, StackOverflow, StackExchange, npm, and even Medium._
  123. Install for [Chrome](https://chrome.google.com/webstore/detail/codecopy/fkbfebkcoelajmhanocgppanfoojcdmg) and [Firefox](https://addons.mozilla.org/en-US/firefox/addon/codecopy/).
  124. ## License
  125. [MIT License](https://zenorocha.mit-license.org/) © Zeno Rocha