MCPcopy Create free account
hub / github.com/instantdb/instant / CodeBlockCopyButton

Function CodeBlockCopyButton

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

Source from the content-addressed store, hash-verified

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

Callers

nothing calls this directly

Calls 1

cnFunction · 0.90

Tested by

no test coverage detected