import React, { useRef, useState } from 'react'; import './style.scss'; export interface CodeDisplayProps { /** Code content to display */ code: string; /** Programming language for syntax highlighting */ language?: string; /** Show copy button */ showCopyButton?: boolean; /** Show line numbers */ showLineNumbers?: boolean; /** Maximum height before scrolling */ maxHeight?: string | number; /** Code title/filename */ title?: string; /** Custom className */ className?: string; /** Whether to wrap long lines */ wrapLines?: boolean; /** Copy button position */ copyButtonPosition?: 'top-right' | 'bottom-right'; } export const CodeDisplay: React.FC = ({ code, language = 'typescript', showCopyButton = true, showLineNumbers = false, maxHeight = '500px', title, className = '', wrapLines = false, copyButtonPosition = 'top-right' }) => { const [copied, setCopied] = useState(false); const codeRef = useRef(null); const handleCopy = async () => { try { await navigator.clipboard.writeText(code); setCopied(true); setTimeout(() => setCopied(false), 2000); } catch (err) { console.error('Failed to copy:', err); } }; const renderLineNumbers = () => { if (!showLineNumbers) return null; const lines = code.split('\n'); return (
{lines.map((_, index) => (
{index + 1}
))}
); }; const copyButtonPositionClass = copyButtonPosition === 'bottom-right' ? 'bottom' : 'top'; return (
{title && (
{title} {language}
)}
{showCopyButton && ( )}
{renderLineNumbers()}
                        {code}
                    
); };