Skip to content

Commit c557f97

Browse files
committed
feat(ui): add CopyableCode component
1 parent 7c3e9bb commit c557f97

1 file changed

Lines changed: 17 additions & 0 deletions

File tree

src/components/ui/CopyableCode.tsx

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import React, { useState } from 'react';
2+
import { FaCopy, FaCheck } from 'react-icons/fa';
3+
interface Props { code: string; language?: string; }
4+
const CopyableCode: React.FC<Props> = ({ code, language = '' }) => {
5+
const [copied, setCopied] = useState(false);
6+
const handleCopy = async () => { await navigator.clipboard.writeText(code); setCopied(true); setTimeout(() => setCopied(false), 2000); };
7+
return (
8+
<div className='relative group'>
9+
<pre className='bg-gray-900 text-gray-100 rounded-xl p-4 overflow-x-auto text-sm'><code className={'language-' + language}>{code}</code></pre>
10+
<button onClick={handleCopy}
11+
className='absolute top-2 right-2 p-2 bg-gray-700 hover:bg-gray-600 rounded-lg text-gray-300 opacity-0 group-hover:opacity-100 transition-opacity'>
12+
{copied ? <FaCheck className='text-green-400' /> : <FaCopy />}
13+
</button>
14+
</div>
15+
);
16+
};
17+
export default CopyableCode;

0 commit comments

Comments
 (0)