@@ -1,4 +1,6 @@
|
||||
function fallbackCopyTextToClipboard(text) {
|
||||
import { toast } from '@redwoodjs/web/toast'
|
||||
|
||||
function fallbackCopyTextToClipboard(text: string) {
|
||||
var textArea = document.createElement('textarea')
|
||||
textArea.value = text
|
||||
|
||||
@@ -21,17 +23,28 @@ function fallbackCopyTextToClipboard(text) {
|
||||
|
||||
document.body.removeChild(textArea)
|
||||
}
|
||||
export function copyTextToClipboard(text) {
|
||||
|
||||
const clipboardSuccessToast = (text: string) => toast.success(() => (
|
||||
<div className="overflow-hidden">
|
||||
<p>link added to clipboard.</p>
|
||||
</div>
|
||||
))
|
||||
|
||||
const makeClipboardCopier = (success: Function) => (text: string) => {
|
||||
if (!navigator.clipboard) {
|
||||
fallbackCopyTextToClipboard(text)
|
||||
success(text)
|
||||
return
|
||||
}
|
||||
navigator.clipboard.writeText(text).then(
|
||||
function () {
|
||||
console.log('Async: Copying to clipboard was successful!')
|
||||
success(text)
|
||||
},
|
||||
function (err) {
|
||||
console.error('Async: Could not copy text: ', err)
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
export const copyTextToClipboard = makeClipboardCopier(clipboardSuccessToast)
|
||||
Reference in New Issue
Block a user