Code Block

Use Code Block to present readable source code with syntax highlighting and a built-in copy action. It supports filenames, line numbers, highlighted ranges, focused lines, and diff states without requiring a separate rendering pipeline in the consumer.

Preview

Reduce motion:Off
Inspect
TypeScriptgreeting.tsx
1
2
3

Examples

Highlighted lines

Reduce motion:Off
Inspect
TypeScriptbutton.tsx
1
2
3

Diff

Reduce motion:Off
Inspect
TypeScriptconfig.ts

Focused lines

Reduce motion:Off
Inspect
TypeScriptbutton.tsx

Installation

Usage

Props

PropertyTypeDefaultDescription
codestringrequiredSource code to render.
languagestring"tsx"Language identifier used for syntax highlighting.
filenamestringundefinedOptional filename shown in the header.
showLineNumbersbooleanfalseShows a numbered gutter.
highlightLinesnumber[] | stringundefinedLines to emphasize, as numbers or a comma-separated range.
showDiffbooleanfalseRemoves leading diff markers and styles added or removed lines.
focusLinesnumber[] | stringundefinedLines to keep visually focused.
classNamestringundefinedAdditional classes for the code block.

Accessibility

The copy action is a labeled native button. Keep the source text complete and understandable without relying only on syntax color, highlighted backgrounds, or diff color.

When showing a diff, retain the surrounding explanation so added and removed lines have context.

Styling

The component owns scrolling, code spacing, syntax colors, and the optional header. Use className to adjust its outer surface or fit it into a wider documentation layout.

Was this component useful?

Your feedback helps us improve the documentation.