Spaces:
Sleeping
Sleeping
File size: 1,408 Bytes
9e7c650 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 | import React, { useEffect, useState } from 'react'
function fallbackCopyText(text) {
const textarea = document.createElement('textarea')
textarea.value = text
textarea.setAttribute('readonly', 'true')
textarea.style.position = 'fixed'
textarea.style.opacity = '0'
document.body.appendChild(textarea)
textarea.select()
document.execCommand('copy')
document.body.removeChild(textarea)
}
export default function ShareLinkButton({
href = '',
label = 'Copiar link',
copiedLabel = 'Link copiado',
className = 'model-source-back-btn',
disabled = false,
title = '',
}) {
const [copied, setCopied] = useState(false)
useEffect(() => {
if (!copied) return undefined
const timeoutId = window.setTimeout(() => setCopied(false), 1800)
return () => window.clearTimeout(timeoutId)
}, [copied])
async function onCopyClick() {
if (disabled || !href) return
try {
if (navigator.clipboard?.writeText) {
await navigator.clipboard.writeText(href)
} else {
fallbackCopyText(href)
}
setCopied(true)
} catch {
fallbackCopyText(href)
setCopied(true)
}
}
return (
<button
type="button"
className={className}
onClick={() => void onCopyClick()}
disabled={disabled || !href}
title={title || href || 'Copiar link'}
>
{copied ? copiedLabel : label}
</button>
)
}
|