-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
36808b4
commit e55d3fc
Showing
4 changed files
with
63 additions
and
6 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { Layerr } from "layerr"; | ||
|
||
export async function copyTextToClipboard(text: string): Promise<void> { | ||
// Navigator clipboard api needs a secure context (https) | ||
if (navigator.clipboard && window.isSecureContext) { | ||
await navigator.clipboard.writeText(text); | ||
} else { | ||
// Use the 'out of viewport hidden text area' trick | ||
const textArea = document.createElement("textarea"); | ||
textArea.value = text; | ||
// Move textarea out of the viewport so it's not visible | ||
textArea.style.position = "absolute"; | ||
textArea.style.left = "-999999px"; | ||
document.body.prepend(textArea); | ||
textArea.select(); | ||
try { | ||
document.execCommand("copy"); | ||
} catch (error) { | ||
throw new Layerr(error, "Failed copying text"); | ||
} finally { | ||
textArea.remove(); | ||
} | ||
} | ||
} |
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