Skip to content

Commit

Permalink
improved clipboard code
Browse files Browse the repository at this point in the history
  • Loading branch information
neerajkumarc committed Nov 16, 2023
1 parent b779718 commit 1b3f6b4
Showing 1 changed file with 6 additions and 12 deletions.
18 changes: 6 additions & 12 deletions src/demo/js/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,19 +196,13 @@ let preview = {

let clipboard = {
copy: function (el) {
// create input box to copy from
const input = document.createElement("input");
input.value = el.parentElement.querySelector("code").innerText;
document.body.appendChild(input);
// select all
input.select();
input.setSelectionRange(0, 99999);
const textToCopy = el.parentElement.querySelector("code").innerText;
// copy
document.execCommand("copy");
// remove input box
input.parentElement.removeChild(input);
// set tooltip text
el.title = "Copied!";
navigator.clipboard.writeText(textToCopy)
.then(()=>{
// set tooltip text
el.title = "Copied!";
})
},
};

Expand Down

0 comments on commit 1b3f6b4

Please sign in to comment.