MCPcopy Create free account
hub / github.com/THU-MAIC/OpenMAIC / CodeBlockCopyButton

Function CodeBlockCopyButton

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

Source from the content-addressed store, hash-verified

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

Callers

nothing calls this directly

Calls 1

cnFunction · 0.90

Tested by

no test coverage detected