UNPKG

cheetah-framework

Version:

Cheetah Framework JS used in all our applications

29 lines (25 loc) 958 B
export default class Clipboard { /** * @source https://hackernoon.com/copying-text-to-clipboard-with-javascript-df4d4988697f */ static copy (str) { const el = document.createElement('textarea') el.value = str el.setAttribute('readonly', '') el.style.position = 'absolute' el.style.left = '-9999px' document.body.appendChild(el) // Check if there is any content selected previously const selected = document.getSelection().rangeCount > 0 ? document.getSelection().getRangeAt(0) // Store selection if found : false // Mark as false to know no selection existed before el.select() document.execCommand('copy') document.body.removeChild(el) // If a selection existed before copying if (selected) { document.getSelection().removeAllRanges() // Unselect everything on the HTML document document.getSelection().addRange(selected) // Restore the original selection } } }