Skip to content

Commit

Permalink
adding visual indicator of clipboard copy success
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenmelnicki committed Feb 1, 2024
1 parent 701302e commit 6e9ebf3
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 12 deletions.
2 changes: 1 addition & 1 deletion components/Form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export default function Form() {
Content
</label>
<textarea
class="min-w-full h-44 px-3 py-2 font-mono shadow-sm border-0 rounded-md ring-1 ring-inset ring-gray-600"
class="min-w-full h-44 px-4 py-2 border rounded-md border-gray-600 font-mono"
id="content"
name="content"
type="text"
Expand Down
2 changes: 1 addition & 1 deletion components/Paste.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export default function Paste({ id, contents }: PasteProps) {
<main class="my-6">
<div class="mb-6 flex items-center justify-end gap-x-4">
<a
class="px-4 py-2 font-semibold rounded-md hover:text-gray-200"
class="px-4 py-2 font-semibold rounded-md hover:text-gray-600 hover:dark:text-gray-400"
href={`/${id}/raw`}
>
View raw
Expand Down
30 changes: 20 additions & 10 deletions static/copyToClipboard.js
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,
);

0 comments on commit 6e9ebf3

Please sign in to comment.