-
Notifications
You must be signed in to change notification settings - Fork 632
Expand file tree
/
Copy pathCopyButton.tsx
More file actions
52 lines (47 loc) · 1.73 KB
/
Copy pathCopyButton.tsx
File metadata and controls
52 lines (47 loc) · 1.73 KB
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
import React from 'react';
import { CopyIcon, CheckIcon } from './icons/copyIcons';
import { useCopyToClipboard } from '../hooks/useCopyToClipboard';
interface CopyButtonProps {
text: string;
className?: string;
/** "overlay" (default): absolute-positioned, hover-reveal (parent needs group relative).
* "inline": normal flow, always visible, compact. */
variant?: 'overlay' | 'inline';
/** Optional label shown next to the icon (inline variant only). */
label?: string;
}
/** Copy-to-clipboard button with "Copied" flash. */
export const CopyButton: React.FC<CopyButtonProps> = ({ text, className = '', variant = 'overlay', label }) => {
const { copied, copy } = useCopyToClipboard();
const handleCopy = async (e: React.MouseEvent) => {
e.stopPropagation();
await copy(text);
};
if (variant === 'inline') {
return (
<button
type="button"
onClick={handleCopy}
className={`flex items-center gap-1 p-0.5 rounded text-muted-foreground hover:text-foreground hover:bg-muted transition-colors ${className}`}
title={copied ? 'Copied!' : 'Copy'}
>
{copied ? <CheckIcon className="w-3 h-3 text-success" /> : <CopyIcon />}
{label && (
<span className="text-[10px]">
{copied ? 'Copied' : label}
</span>
)}
</button>
);
}
return (
<button
type="button"
onClick={handleCopy}
className={`absolute top-1.5 right-1.5 p-1 rounded-md opacity-0 group-hover:opacity-100 transition-opacity text-muted-foreground hover:text-foreground hover:bg-muted ${className}`}
title={copied ? 'Copied!' : 'Copy'}
>
{copied ? <CheckIcon className="w-3 h-3 text-success" /> : <CopyIcon />}
</button>
);
};