CodeBlock

Syntax-highlighted code display with copy functionality, theming, diff view, and collapsible sections

Setup

import { CodeBlock } from '@fragments-sdk/ui';

Examples

Default

Basic code block with syntax highlighting

Loading preview...

With Filename

Code block with filename in header bar

Loading preview...

With Title

Code block with external title label

Loading preview...

With Caption

Code block with footer caption

Loading preview...

With Line Numbers

Shows line numbers for reference

Loading preview...

Custom Start Line

Shows code excerpt starting from a specific line number

Loading preview...

With Highlighted Lines

Emphasizes specific lines of code

Loading preview...

Diff View

Shows added and removed lines in a diff-like format

Loading preview...

GitHub Dark Theme

Using the GitHub Dark theme

Loading preview...

GitHub Light Theme

Using the GitHub Light theme for light backgrounds

Loading preview...

Dracula Theme

Using the popular Dracula theme

Loading preview...

Word Wrap

Long lines wrap instead of scrolling horizontally

Loading preview...

Max Height with Scroll

Constrains height with scrollable content

Loading preview...

Collapsible

Long code that can be expanded/collapsed

Loading preview...

JSON

Configuration file example

Loading preview...

Without Copy Button

Minimal display without copy functionality

Loading preview...

Props

PropTypeDefaultDescription
codeRequiredstringNot setThe code string to display
languageenumtsxtypescripttsjavascriptjsjsxbashshellcssscsssassjsonhtmlxmlmarkdownmdyamlymlpythonpyrubygorustjavakotlinswiftccppcsharpphpsqlgraphqldiffplaintexttexttsxProgramming language for syntax highlighting
themeenumsynthwave-84github-darkgithub-lightone-dark-prodraculanordmonokaivitesse-darkvitesse-lightmin-darkmin-lightone-dark-proSyntax highlighting theme
showCopybooleantrueWhether to show the copy button
titlestringNot setOptional title displayed above the code block (external label)
filenamestringNot setOptional filename shown in header bar inside code block
captionstringNot setOptional caption displayed below the code block
showLineNumbersbooleanfalseWhether to display line numbers
startLineNumbernumber1Starting line number (useful for code excerpts)
highlightLinesarrayNot setLines to highlight (e.g., [1, 3, "5-7"])
addedLinesarrayNot setLines marked as added in diff view (e.g., [2, "4-6"])
removedLinesarrayNot setLines marked as removed in diff view (e.g., [1, 3])
wordWrapbooleanfalseEnable word wrapping for long lines
maxHeightnumberNot setMaximum height in pixels (enables scrolling)
collapsiblebooleanfalseAllow collapsing/expanding the code block
defaultCollapsedbooleanfalseInitial collapsed state (only applies when collapsible is true)
collapsedLinesnumber5Number of lines to show when collapsed
compactbooleanfalseCompact mode with reduced padding
persistentCopybooleanfalseShow a persistent copy button that is always visible
copyPlacementenumautoheaderoverlayautoWhere to place the copy button when not using persistentCopy
bgstringNot setCustom background color for the code block (useful when the content area is pure black or dark gray)
onCopyfunctionNot setCallback fired when the copy button is clicked and copy succeeds

Usage Guidelines

When to use

  • Displaying code examples in documentation
  • Showing installation commands
  • Presenting configuration snippets
  • Teaching programming concepts
  • Showing code diffs or changes
  • Displaying long code files with collapse functionality

When not to use

  • User-editable code (use code editor)
  • Very short inline code (use <code> element)
  • Non-code content (use Text or Card)

Best practices

  • Always specify the correct language for accurate highlighting
  • Use filename prop to show source file name in header bar
  • Use copyPlacement="auto" to render copy in overlay mode when no filename is provided
  • Use title prop for external labels above the code block
  • Enable line numbers for longer code samples
  • Use highlightLines to draw attention to key lines
  • Use addedLines/removedLines for diff highlighting
  • Set maxHeight for very long code blocks to prevent layout issues
  • Use collapsible for code samples that users may want to skim
  • Choose a theme that matches your documentation style
  • Keep code examples concise and focused
  • Use CodeBlock.Tabbed for multi-language snippets; tabs support controlled value/onValueChange and explicit tab values
Accessibility
  • Code is presented in a semantic pre/code structure
  • Copy button has appropriate aria-label
  • Keyboard accessible copy functionality
  • Collapse button has aria-expanded state