Created
November 26, 2020 00:15
-
-
Save LucasArruda/a054e7d970950f892974b981fcb4c18d to your computer and use it in GitHub Desktop.
Copies a supplied text to the clipboard. Useful for react components that loop and can't depends on ids.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Copies 'text' to clipboard. Doesn't depends on id's, targets. Just text. | |
const copyToClipboard = (text) => { | |
const el = document.createElement('input'); | |
document.body.appendChild(el); | |
el.value = text; | |
el.select(); | |
el.setSelectionRange(0, 99999); /*For mobile devices*/ | |
document.execCommand("copy"); | |
document.body.removeChild(el); | |
}; | |
// call it with | |
copyToClipboard('text to copy'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment