Redpanda UIRedpanda UI

Code Block

A customizable code block component with syntax highlighting, copy functionality, and scroll area support.

Made by malinskibeniamin

Powered by

Installation

Loading component...

Usage

The CodeBlock component provides a structured way to display code with syntax highlighting, copy functionality, and scrollable content:

import { CodeBlock, Pre, SimpleCodeBlock } from '@/components/redpanda-ui/code-block';

// Basic usage with syntax highlighting
const sampleCode = `function greetUser(name: string): string {
  return \`Hello, \${name}! Welcome to Redpanda UI.\`;
}`;

export function Example() {
  return (
    <CodeBlock title="greet.ts" icon="📄">
      <Pre>
        <code className="language-typescript">{sampleCode}</code>
      </Pre>
    </CodeBlock>
  );
}

// Simplified interface for quick code blocks
export function SimpleExample() {
  return (
    <SimpleCodeBlock
      code={sampleCode}
      language="language-typescript"
      title="greet.ts"
      icon="📄"
      allowCopy={true}
    />
  );
}

// Different sizes and configurations
export function VariousExamples() {
  return (
    <>
      {/* Small code block */}
      <CodeBlock size="sm" width="sm" maxHeight="sm">
        <Pre>
          <code className="language-bash">npm install @redpanda-ui/core</code>
        </Pre>
      </CodeBlock>

      {/* Full width with no copy button */}
      <CodeBlock 
        title="config.json" 
        width="full" 
        allowCopy={false}
        onCopy={() => console.log('Code copied!')}
      >
        <Pre>
          <code className="language-json">
            {JSON.stringify({ theme: "dark", fontSize: 14 }, null, 2)}
          </code>
        </Pre>
      </CodeBlock>

      {/* Large with unlimited height */}
      <CodeBlock size="lg" maxHeight="none">
        <Pre>
          <code className="language-typescript">
            {/* Long code content here */}
          </code>
        </Pre>
      </CodeBlock>
    </>
  );
}

When to use

Use this decision tree to determine when to use the CodeBlock component and which configuration is most appropriate:

Use Cases

  • Documentation: Code examples, API snippets, configuration files
  • Tutorials: Step-by-step code instructions with file context
  • Error messages: Stack traces, logs, debug output
  • Code sharing: Shareable code snippets with copy functionality
  • Reference: Command line instructions, installation steps

CodeBlock vs Other Components

  • Use CodeBlock when: You need to display formatted code with copy functionality, file context, or scrollable content
  • Use inline <code> when: Short code snippets within text, variable names, or simple commands
  • Use CodeEditor when: Users need to edit code, with syntax checking and interactive features
  • Use Pre when: You need basic preformatted text without the enhanced features

Anatomy

The CodeBlock component is built with composition and provides multiple layers for different functionality:

CodeBlock Component Structure:
┌─────────────────────────────────────────────────┐
│ Figure Container (Variants: size, width, height)│
│ ┌─────────────────────────────────────────────┐ │
│ │ Header (Conditional - if title provided)   │ │
│ │ ┌─────────────────────────────────────────┐ │ │
│ │ │ [📄] Title Text        [Copy Button]   │ │ │
│ │ │ - Icon (string/ReactNode)              │ │ │
│ │ │ - Figcaption with truncation           │ │ │
│ │ │ - CopyButton (conditional)             │ │ │
│ │ └─────────────────────────────────────────┘ │ │
│ └─────────────────────────────────────────────┘ │
│ ┌─────────────────────────────────────────────┐ │
│ │ ScrollArea (Horizontal + Vertical)          │ │
│ │ ┌─────────────────────────────────────────┐ │ │
│ │ │ ScrollViewport (Copy Detection Area)   │ │ │
│ │ │ ┌─────────────────────────────────────┐ │ │ │
│ │ │ │ Pre Element                         │ │ │ │
│ │ │ │ ┌─────────────────────────────────┐ │ │ │ │
│ │ │ │ │ Code Element                    │ │ │ │ │
│ │ │ │ │ - className: language-*         │ │ │ │ │
│ │ │ │ │ - Syntax highlighting ready     │ │ │ │ │
│ │ │ │ │ - Selection styling             │ │ │ │ │
│ │ │ │ └─────────────────────────────────┘ │ │ │ │
│ │ │ └─────────────────────────────────────┘ │ │ │
│ │ └─────────────────────────────────────────┘ │ │
│ │ ┌─────────────────────────────────────────┐ │ │
│ │ │ Horizontal ScrollBar                    │ │ │
│ │ └─────────────────────────────────────────┘ │ │
│ └─────────────────────────────────────────────┘ │
│                                                 │
│ Floating Copy Button (if no title)             │
│ - Positioned absolute top-right                │
│ - Backdrop blur effect                         │
└─────────────────────────────────────────────────┘

SimpleCodeBlock Wrapper:
┌─────────────────────────────────────┐
│ SimpleCodeBlock                     │
│ - Takes: code, language, props      │
│ - Auto-wraps in CodeBlock + Pre     │
│ - Simplified interface              │
│ - Ideal for backend developers      │
└─────────────────────────────────────┘

Component Composition

  • CodeBlock: Main container with variant styling and scroll management
  • Pre: Formatted pretext element with padding and overflow handling
  • Code: Semantic code element with language classes for syntax highlighting
  • ScrollArea: Base UI scroll area for smooth scrolling experience
  • CopyButton: Interactive copy functionality with visual feedback
  • SimpleCodeBlock: Simplified wrapper for common use cases
  • Variant System: Size, width, and height controls using CVA
  • Copy Detection: Smart text extraction excluding ignore classes

Examples

Default

Loading component...

No title

Loading component...

No copy button

Loading component...

Props

CodeBlock

Prop

Type

Pre

Prop

Type

Recent changes

  • patchv1.2.0Pin shipped dependency floors to the version we develop against. Registry items now declare ranges like `^5.1.9` (the actual installed version) instead of collapsing to `^5.0.0`, so consumers start on the known-tested b…#133
  • minorv1.1.0Theme docs refresh, readability pass on semantic foregrounds, and consumer-facing Base UI regression fixes.#121
  • minorv1.0.0Post-Base-UI polish. Public API unchanged.#116
  • majorv1.0.0Migrate every Radix-based primitive to `@base-ui/react@^1.4.0` (Base UI).#114
  • minorv0.3.0Add theme-provider component to the registry with documentation and tests. Includes playground type improvements (export RegistryItem, remove as-const boilerplate) and docs site dark mode border color fix.#109
See full history →
Built by malinskibeniamin. The source code is available on GitHub.

On this page