Skip to content

Commit

Permalink
added execComand fallback
Browse files Browse the repository at this point in the history
  • Loading branch information
EduardZaydler committed Sep 12, 2024
1 parent 164097d commit 33d4abb
Showing 1 changed file with 28 additions and 8 deletions.
36 changes: 28 additions & 8 deletions src/Components/TriggerEditForm/Components/CopyButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,34 @@ interface IProps {
className?: string;
}

const copy = async (text: string) => {
if (!navigator?.clipboard) {
return;
}
try {
await navigator.clipboard.writeText(text);
} catch (error) {
console.log(error);
const copy = (text: string) => {
if (navigator.clipboard) {
// If clipboard api is supported
navigator.clipboard
.writeText(text)
.then(() => {
console.log("Text copied to clipboard");
})
.catch((err) => {
console.error("Failed to copy text", err);
});
} else {
// Fallback on execCommand
const textarea = Object.assign(document.createElement("textarea"), {
value: text,
style: { position: "absolute", left: "-9999px" },
});
document.body.appendChild(textarea);
textarea.select();

try {
document.execCommand("copy");
console.log("Text copied to clipboard");
} catch (err) {
console.error("Failed to copy text", err);
}

document.body.removeChild(textarea);
}
};

Expand Down

0 comments on commit 33d4abb

Please sign in to comment.