fix: add copy fallback

This commit is contained in:
David Nguyen
2023-10-06 14:05:29 +11:00
parent 0d42285f5d
commit 4db98bc19c

View File

@ -34,18 +34,23 @@ export function useCopyToClipboard(): [CopiedValue, CopyFn] {
/** /**
* Handle copying values to the clipboard using the ClipboardItem API. * Handle copying values to the clipboard using the ClipboardItem API.
* *
* Allows us to copy async values for Safari. Does not work in FireFox. * Works in all browsers except FireFox.
* *
* https://caniuse.com/mdn-api_clipboarditem * https://caniuse.com/mdn-api_clipboarditem
*/ */
const handleClipboardApiCopy = async (value: CopyValue, blobType = 'text/plain') => { const handleClipboardApiCopy = async (value: CopyValue, blobType = 'text/plain') => {
try {
await navigator.clipboard.write([new ClipboardItem({ [blobType]: value })]); await navigator.clipboard.write([new ClipboardItem({ [blobType]: value })]);
} catch (e) {
// Fallback to attempt.
await handleWriteTextCopy(value);
}
}; };
/** /**
* Handle copying values to the clipboard using `writeText`. * Handle copying values to the clipboard using `writeText`.
* *
* Will not work in Safari for async values. * Works in all browsers except Safari for async values.
*/ */
const handleWriteTextCopy = async (value: CopyValue) => { const handleWriteTextCopy = async (value: CopyValue) => {
await navigator.clipboard.writeText(await value); await navigator.clipboard.writeText(await value);