Download desktop/src/components/shared/CopyButton.tsx from chenbhao/codev: direct link, hf CLI and curl.
- Browser
- Download file 1.2 kB
-
https://huggingface.co/chenbhao/codev/resolve/e40073cc959cd4729bf4009662bca374c5c4e266/desktop/src/components/shared/CopyButton.tsx
- Command line
-
hf download hf://chenbhao/codev@e40073cc959cd4729bf4009662bca374c5c4e266/desktop/src/components/shared/CopyButton.tsx
-
curl -L -o CopyButton.tsx https://huggingface.co/chenbhao/codev/resolve/e40073cc959cd4729bf4009662bca374c5c4e266/desktop/src/components/shared/CopyButton.tsx
1.2 kB
| import { useEffect, useState } from 'react' | |
| import { copyTextToClipboard } from '../chat/clipboard' | |
| type Props = { | |
| text: string | |
| label?: string | |
| copiedLabel?: string | |
| displayLabel?: string | |
| displayCopiedLabel?: string | |
| className?: string | |
| } | |
| export function CopyButton({ | |
| text, | |
| label = 'Copy', | |
| copiedLabel = 'Copied', | |
| displayLabel, | |
| displayCopiedLabel, | |
| className = '', | |
| }: Props) { | |
| const [copied, setCopied] = useState(false) | |
| useEffect(() => { | |
| if (!copied) return | |
| const timer = window.setTimeout(() => setCopied(false), 1500) | |
| return () => window.clearTimeout(timer) | |
| }, [copied]) | |
| const handleCopy = async () => { | |
| try { | |
| const ok = await copyTextToClipboard(text) | |
| if (!ok) { | |
| setCopied(false) | |
| return | |
| } | |
| setCopied(true) | |
| } catch { | |
| setCopied(false) | |
| } | |
| } | |
| const currentLabel = copied ? copiedLabel : label | |
| const buttonText = copied | |
| ? (displayCopiedLabel ?? copiedLabel) | |
| : (displayLabel ?? label) | |
| return ( | |
| <button | |
| type="button" | |
| onClick={handleCopy} | |
| className={className} | |
| aria-label={currentLabel} | |
| title={currentLabel} | |
| > | |
| {buttonText} | |
| </button> | |
| ) | |
| } | |