MCPcopy Create free account
hub / github.com/antvis/AVA / CopyCode

Function CopyCode

site/components/docs/CopyCode.tsx:7–53  ·  view source on GitHub ↗
({
  label,
  text,
  className = 'docs-copy',
}: {
  label?: string;
  text?: string;
  className?: string;
})

Source from the content-addressed store, hash-verified

5import { useRef, useState, useEffect } from 'react';
6
7export default function CopyCode({
8 label,
9 text,
10 className = 'docs-copy',
11}: {
12 label?: string;
13 text?: string;
14 className?: string;
15}) {
16 const t = useTranslations('CopyCode');
17 const button = useRef<HTMLButtonElement>(null);
18 const timer = useRef<ReturnType<typeof setTimeout> | undefined>(undefined);
19 const [status, setStatus] = useState('');
20 useEffect(() => () => clearTimeout(timer.current), []);
21
22 async function copy() {
23 const code = button.current?.closest('.docs-code-block')?.querySelector('code');
24 const value = text ?? code?.textContent;
25 if (value == null) return;
26 try {
27 await navigator.clipboard.writeText(value);
28 setStatus(t('copied'));
29 } catch {
30 setStatus(text === undefined ? t('selectCodeToCopy') : t('copyFailedTryAgain'));
31 }
32 clearTimeout(timer.current);
33 timer.current = setTimeout(() => setStatus(''), 2500);
34 }
35
36 return (
37 <button ref={button} type="button" className={className} onClick={copy} aria-label={label || t('copyCode')}>
38 <span aria-live="polite">{status || label || t('copy2')}</span>
39 <svg
40 width="14"
41 height="14"
42 viewBox="0 0 24 24"
43 fill="none"
44 stroke="currentColor"
45 strokeWidth="1.5"
46 aria-hidden="true"
47 >
48 <rect x="8" y="8" width="12" height="12" rx="2" />
49 <path d="M16 8V4a2 2 0 0 0-2-2H4a2 2 0 0 0-2 2v10a2 2 0 0 0 2 2h4" />
50 </svg>
51 </button>
52 );
53}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected