Copies from dom so you would create an element with the text in the DOM
Best Way
exportfunctioncopyToClipboard(str) {constel=document.createElement("textarea"); // Create a <textarea> elementel.value = str; // Set its value to the string that you want copiedel.setAttribute("readonly",""); // Make it readonly to be tamper-proofel.style.position ="absolute";el.style.left ="-9999px"; // Move outside the screen to make it invisibledocument.body.appendChild(el); // Append the <textarea> element to the HTML documentconstselected=document.getSelection().rangeCount >0// Check if there is any content selected previously?document.getSelection().getRangeAt(0) // Store selection if found:false; // Mark as false to know no selection existed beforeel.select(); // Select the <textarea> contentdocument.execCommand("copy"); // Copy - only works as a result of a user action (e.g. click events)document.body.removeChild(el); // Remove the <textarea> elementif (selected) {// If a selection existed before copyingdocument.getSelection().removeAllRanges(); // Unselect everything on the HTML documentdocument.getSelection().addRange(selected); // Restore the original selection }}