Merge pull request #430 from Chalarangelo/copyToClipboard
[FEATURE] Add copyToClipboard
This commit is contained in:
32
snippets/copyToClipboard.md
Normal file
32
snippets/copyToClipboard.md
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
### copyToClipboard
|
||||||
|
|
||||||
|
Copy a string to the clipboard. Only works as a result of user action (i.e. inside a `click` event listener).
|
||||||
|
|
||||||
|
Create a new `<textarea>` element, fill it with the supplied data and add it to the HTML document.
|
||||||
|
Use `Selection.getRangeAt()`to store the selected range (if any).
|
||||||
|
Use `document.execCommand('copy')` to copy to the clipboard.
|
||||||
|
Remove the `<textarea>` element from the HTML document.
|
||||||
|
Finally, use `Selection().addRange()` to recover the original selected range (if any).
|
||||||
|
|
||||||
|
```js
|
||||||
|
const copyToClipboard = str => {
|
||||||
|
const el = document.createElement('textarea');
|
||||||
|
el.value = str;
|
||||||
|
el.setAttribute('readonly', '');
|
||||||
|
el.style.position = 'absolute';
|
||||||
|
el.style.left = '-9999px';
|
||||||
|
document.body.appendChild(el);
|
||||||
|
const selected = document.getSelection().rangeCount > 0 ? document.getSelection().getRangeAt(0) : false;
|
||||||
|
el.select();
|
||||||
|
document.execCommand('copy');
|
||||||
|
document.body.removeChild(el);
|
||||||
|
if (selected) {
|
||||||
|
document.getSelection().removeAllRanges();
|
||||||
|
document.getSelection().addRange(selected);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
```js
|
||||||
|
copyToClipboard('Lorem ipsum'); // 'Lorem ipsum' copied to clipboard.
|
||||||
|
```
|
||||||
@ -16,6 +16,7 @@ collatz:math
|
|||||||
collectInto:adapter
|
collectInto:adapter
|
||||||
compact:array
|
compact:array
|
||||||
compose:function
|
compose:function
|
||||||
|
copyToClipboard:browser
|
||||||
countOccurrences:array
|
countOccurrences:array
|
||||||
countVowels:string
|
countVowels:string
|
||||||
currentURL:browser
|
currentURL:browser
|
||||||
|
|||||||
Reference in New Issue
Block a user