Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix/added execComand fallback #537

Merged
merged 4 commits into from
Sep 18, 2024
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 28 additions & 10 deletions src/Components/TriggerEditForm/Components/CopyButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,39 @@ interface IProps {
className?: string;
}

const pusfSuccessfulMessage = () => Toast.push("Text copied to clipboard");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const pusfSuccessfulMessage = () => Toast.push("Text copied to clipboard");
const pushSuccessfulMessage = () => Toast.push("Text copied to clipboard");


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

const isSuccessful = document.execCommand("copy");

isSuccessful
? pusfSuccessfulMessage()
: Toast.push("Failed to copy text, try to launch the app under https");

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

export const CopyButton: React.FC<IProps> = ({ value, className }) => {
const handleCopy = () => {
Toast.push("Target value copied");
copy(value.trim());
const handleCopy = async () => {
await copy(value.trim());
};

return <Button className={className} use="link" icon={<CopyIcon />} onClick={handleCopy} />;
Expand Down
Loading