MCPcopy Create free account
hub / github.com/adobe/react-spectrum / CopyButton

Function CopyButton

packages/dev/s2-docs/src/CopyButton.tsx:23–69  ·  view source on GitHub ↗
({
  text,
  getText,
  ariaLabel = 'Copy',
  tooltip = 'Copy',
  isQuiet = false,
  size = 'S'
}: CopyButtonProps)

Source from the content-addressed store, hash-verified

21}
22
23export function CopyButton({
24 text,
25 getText,
26 ariaLabel = 'Copy',
27 tooltip = 'Copy',
28 isQuiet = false,
29 size = 'S'
30}: CopyButtonProps) {
31 let [isCopied, setIsCopied] = useState(false);
32 let timeout = useRef<ReturnType<typeof setTimeout> | null>(null);
33
34 useEffect(() => {
35 return () => {
36 if (timeout.current) {
37 clearTimeout(timeout.current);
38 }
39 };
40 }, []);
41
42 let handleCopy = () => {
43 if (timeout.current) {
44 clearTimeout(timeout.current);
45 }
46 let value = text ?? getText?.();
47 if (!value) {
48 return;
49 }
50 navigator.clipboard
51 .writeText(value)
52 .then(() => {
53 setIsCopied(true);
54 timeout.current = setTimeout(() => setIsCopied(false), 2000);
55 })
56 .catch(() => {
57 ToastQueue.negative('Failed to copy.');
58 });
59 };
60
61 return (
62 <TooltipTrigger placement="end">
63 <ActionButton aria-label={ariaLabel} isQuiet={isQuiet} size={size} onPress={handleCopy}>
64 {isCopied ? <CheckmarkCircle /> : <Copy />}
65 </ActionButton>
66 <Tooltip>{tooltip}</Tooltip>
67 </TooltipTrigger>
68 );
69}
70
71export default CopyButton;

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected