CodeBlock
SSR-safe component for rendering syntax-highlighted code blocks with proper whitespace preservation.
Props
| Prop | Type | Required | Default | Description |
|---|---|---|---|---|
code | string | Yes | - | The code content to display |
language | string | No | 'text' | Programming language for syntax highlighting |
className | string | No | - | Additional CSS classes |
highlightLines | number[] | No | - | Array of line numbers to highlight |
Usage
import { CodeBlock } from '@/components/atoms/CodeBlock';
// Basic usage
<CodeBlock code="const x = 1;" language="typescript" />
// With highlighted lines
<CodeBlock
code={codeSnippet}
language="javascript"
highlightLines={[3, 5, 7]}
/>Features
- SSR-safe: Can be used in server components without hydration issues
- Syntax highlighting: Uses Prism via
react-syntax-highlighter - Line numbers: Shows line numbers by default
- Line wrapping: Wraps long lines for better readability
- Custom theme: Uses Contentstack design system colors
- Line highlighting: Optional support for highlighting specific lines
- Whitespace preservation: Maintains exact spacing and newlines
Styling
The component uses a custom theme (contentstackCodeTheme) that matches the Contentstack design system:
- Background:
#1A1919(lighter than default dark background) - Text colors: Muted accent colors for syntax highlighting
- Line numbers: Subtle gray color with proper spacing
- Border radius:
8pxwith subtle border
Notes
- This is a pure rendering component with no client-side interactivity
- For copy-to-clipboard functionality, use
CodeBlockWithCopyinstead - Line numbers start at 1 by default
- Highlighted lines use a subtle background overlay