Components/CODING

CodeSection

A code display and editing component with copy, download, and customization options

Live Preview
Open in
Code
Loading...

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

PropTypeDefaultDescription
heightstring"90vh"Height of the editor
maxHeightstring"80vh"Maximum height of the editor
minHeightstring"200px"Minimum height of the editor
defaultLanguagestring"javascript"Code language (for syntax highlighting)
defaultValuestring"// some comment"Default displayed code
classNamestring""Additional CSS classes
isEditablebooleantrueAllows code editing
variant"default" | "outline" | "ghost""default"Visual style of the container
processLoadingbooleanfalseShows a loading/progressive writing effect
progressiveModebooleanfalseEnables progressive code writing animation
onComplete() => voidundefinedCallback when progressive writing is complete
autoHeightbooleanfalseAutomatically 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 and processLoading 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