Components/CODING
CodeSection
A code display and editing component with copy, download, and customization options
Example
export default function CodeSectionExample() {
const [codeValue, setCodeValue] = useState(`function fibonacci(n) {
if (n <= 1) return n;
return fibonacci(n - 1) + fibonacci(n - 2);
}
console.log(fibonacci(10));`);
const handleComplete = () => {
console.log("Progressive writing completed");
};
const handleEditorChange = (value: string | undefined) => {
if (value) {
setCodeValue(value);
console.log("Code changed:", value);
}
};
return (
<CodeSection
maxHeight="500px"
defaultLanguage="javascript"
defaultValue={codeValue}
variant="outline"
autoHeight={true}
progressiveMode={true}
processLoading={true}
onComplete={handleComplete}
isEditable={true}
className="w-full max-w-2xl"
/>
);
}
Documentation
Prop | Type | Default | Description |
---|---|---|---|
height | string | "90vh" | Height of the editor |
maxHeight | string | "80vh" | Maximum height of the editor |
minHeight | string | "200px" | Minimum height of the editor |
defaultLanguage | string | "javascript" | Code language (for syntax highlighting) |
defaultValue | string | "// some comment" | Default displayed code |
className | string | "" | Additional CSS classes |
isEditable | boolean | true | Allows code editing |
variant | "default" | "outline" | "ghost" | "default" | Visual style of the container |
processLoading | boolean | false | Shows a loading/progressive writing effect |
progressiveMode | boolean | false | Enables progressive code writing animation |
onComplete | () => void | undefined | Callback when progressive writing is complete |
autoHeight | boolean | false | Automatically adjusts editor height to content |
Types
interface CodeSectionProps {
height?: string;
maxHeight?: string;
minHeight?: string;
defaultLanguage?: string;
defaultValue?: string;
className?: string;
isEditable?: boolean;
variant?: "default" | "outline" | "ghost";
processLoading?: boolean;
progressiveMode?: boolean;
onComplete?: () => void;
autoHeight?: boolean;
}
Features
- Monaco Editor Integration: Full-featured code editor with syntax highlighting and editing capabilities
- Copy & Download: Built-in buttons to copy code to clipboard or download as file with visual feedback
- Progressive Writing: Animated code appearance with
progressiveMode
andprocessLoading
options - Auto Height: Dynamic height adjustment based on content with
autoHeight
prop - Visual Variants: Three styling options - default, outline, and ghost variants
- Responsive Design: Adapts to different screen sizes with proper container sizing
- Dark Mode Support: Full dark mode compatibility with appropriate color schemes
- Accessibility Focused: Keyboard navigation and screen reader friendly interface