Skip to content

Commit

Permalink
Handle Copy for http context
Browse files Browse the repository at this point in the history
  • Loading branch information
benthecarman committed Oct 1, 2023
1 parent 4b23571 commit e82c86b
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/utils/useCopy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,18 @@ export const useCopy = ({ copiedTimeout = 2000 }: UseCopyProps = {}): [
await Clipboard.write({
string: text
});
} else {
} else if (navigator.clipboard && window.isSecureContext) {
await navigator.clipboard.writeText(text);
} else {
// handle if running on http://
let textArea = document.createElement("textarea");
textArea.value = text;
textArea.style.position = "absolute";
textArea.style.opacity = "0";
document.body.appendChild(textArea);
textArea.select();
await document.execCommand("copy");
textArea.remove();
}
setCopied(true);
if (timeout) clearTimeout(timeout);
Expand Down

0 comments on commit e82c86b

Please sign in to comment.