MCPcopy Create free account
hub / github.com/AntiHub-Project/AntiHub / CodeBlockCopyButton

Function CodeBlockCopyButton

components/ai-elements/code-block.tsx:138–178  ·  view source on GitHub ↗
({
  onCopy,
  onError,
  timeout = 2000,
  children,
  className,
  ...props
}: CodeBlockCopyButtonProps)

Source from the content-addressed store, hash-verified

136};
137
138export const CodeBlockCopyButton = ({
139 onCopy,
140 onError,
141 timeout = 2000,
142 children,
143 className,
144 ...props
145}: CodeBlockCopyButtonProps) => {
146 const [isCopied, setIsCopied] = useState(false);
147 const { code } = useContext(CodeBlockContext);
148
149 const copyToClipboard = async () => {
150 if (typeof window === "undefined" || !navigator?.clipboard?.writeText) {
151 onError?.(new Error("Clipboard API not available"));
152 return;
153 }
154
155 try {
156 await navigator.clipboard.writeText(code);
157 setIsCopied(true);
158 onCopy?.();
159 setTimeout(() => setIsCopied(false), timeout);
160 } catch (error) {
161 onError?.(error as Error);
162 }
163 };
164
165 const Icon = isCopied ? CheckIcon : CopyIcon;
166
167 return (
168 <Button
169 className={cn("shrink-0", className)}
170 onClick={copyToClipboard}
171 size="icon"
172 variant="ghost"
173 {...props}
174 >
175 {children ?? <Icon size={14} />}
176 </Button>
177 );
178};

Callers

nothing calls this directly

Calls 1

cnFunction · 0.90

Tested by

no test coverage detected