Created
September 8, 2020 09:57
-
-
Save wilsonpage/6a87d8d87ab650f25c8792603e29d6bd to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import { isIos } from '~/lib/device/utils'; | |
const fallback = (text: string) => { | |
const textarea = document.createElement('textarea'); | |
// create textarea | |
textarea.value = text; | |
// ios will zoom in on the input if the font-size is < 16px | |
textarea.style.fontSize = '20px'; | |
document.body.appendChild(textarea); | |
// select text | |
if (isIos()) { | |
const range = document.createRange(); | |
range.selectNodeContents(textarea); | |
const selection = window.getSelection() as Selection; | |
selection.removeAllRanges(); | |
selection.addRange(range); | |
textarea.setSelectionRange(0, 999999); | |
} else { | |
textarea.select(); | |
} | |
// copy selection | |
document.execCommand('copy'); | |
// cleanup | |
document.body.removeChild(textarea); | |
}; | |
export default async (text: string) => { | |
try { | |
// this will throw if the api doesn't exist or if it's | |
// been blocked by security policy (inside facebook/ig webview) | |
await navigator.clipboard.writeText(text); | |
} catch (error) { | |
fallback(text); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment