-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adding visual indicator of clipboard copy success
- Loading branch information
1 parent
701302e
commit 6e9ebf3
Showing
3 changed files
with
22 additions
and
12 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,30 @@ | ||
const button = document.getElementById("copy-btn"); | ||
|
||
function copyToClipboard(text) { | ||
navigator.clipboard.writeText(text) | ||
.then(function () { | ||
button.textContent = "Copied!"; | ||
button.style.paddingLeft = "3.38rem"; | ||
button.style.paddingRight = "3.38rem"; | ||
|
||
setTimeout(function () { | ||
button.textContent = "Copy to clipboard"; | ||
button.style.removeProperty("padding-left"); | ||
button.style.removeProperty("padding-right"); | ||
}, 750); | ||
|
||
console.log("content copied to clipboard."); | ||
}) | ||
.catch(function (err) { | ||
console.error("failed to copy content to clipboard.", err); | ||
}); | ||
} | ||
|
||
document | ||
.getElementById("copy-btn") | ||
.addEventListener( | ||
"click", | ||
function () { | ||
const text = document.getElementsByTagName("pre").item(0).innerText; | ||
copyToClipboard(text); | ||
}, | ||
false, | ||
); | ||
button.addEventListener( | ||
"click", | ||
function () { | ||
const text = document.getElementsByTagName("pre").item(0).innerText; | ||
copyToClipboard(text); | ||
}, | ||
false, | ||
); |