Skip to main content
Our new developer certification is live!

CodeBlockWithCopy

Client component wrapper that adds copy-to-clipboard functionality to code blocks.

Props

PropTypeRequiredDefaultDescription
codestringYes-The code content to display
languagestringNo'text'Programming language for syntax highlighting
classNamestringNo-Additional CSS classes
highlightLinesnumber[]No-Array of line numbers to highlight

Usage

import { CodeBlockWithCopy } from '@/components/atoms/CodeBlockWithCopy';

<CodeBlockWithCopy 
  code={codeSnippet} 
  language="typescript"
  highlightLines={[2, 4]}
/>

Features

  • Copy button: Adds a copy-to-clipboard button in the top-right corner
  • All CodeBlock features: Inherits all features from CodeBlock
  • Client-side interactivity: Uses React hooks for copy functionality
  • Accessible: Button includes proper ARIA labels

Architecture

This component is a composition of:

  • CodeBlock: SSR-safe syntax highlighting component
  • CopyButton: Client-side copy-to-clipboard button

Notes

  • This is a client component ("use client") due to the copy button functionality
  • The copy button appears in the top-right corner with a subtle dark background
  • Shows "Copied!" feedback for 2 seconds after copying
  • Falls back to document.execCommand for older browsers