Code Block

A syntax-highlighted code block component with copy functionality and customizable layout

Preview

JavaScript

Installation

Usage

Examples

Floating Copy Button

Display code without a header using a floating copy button that appears on hover.

With Filename

Display a filename in the header to provide context for the code snippet.

TypeScript
types/user.ts

With Tabs

Show multiple code variants using tabs, commonly used for different package managers or language implementations.

Switch between package managers using the tabs in the header.

With Line Numbers

Add line numbers to help users reference specific lines of code.

JavaScript

Multiple Languages

The component automatically detects and highlights syntax for different programming languages.

TypeScript

TypeScript

Python

Python

Bash

Custom Icon

Override the default language icon with a custom icon of your choice.

Custom Height

Control the maximum height and scrolling behavior with custom classes.

Custom max height with scrolling (max-h-[200px])

TypeScript
config/theme.ts

Server-Side Highlighting

Pre-highlight code on the server using the highlight function for instant rendering with zero client-side delay.

TypeScript
lib/api.ts

Highlighted Lines

Emphasize specific lines with background highlighting and a left border accent.

Highlight specific lines (lines 2, 5)

JavaScript
utils/cart.js

Highlight range using string syntax (lines 1-3, 7)

JavaScript
utils/cart.js

Diffs

Show code changes with added, removed, and modified line indicators. Diff markers are automatically stripped from the displayed code.

Diff view with added (+), removed (-), and modified (!) lines

JavaScript
utils/helpers.js

Using comment-style markers for TypeScript/JSX compatibility

TypeScript
types/user.ts

Focus Mode

Blur non-focused lines to draw attention to specific parts of the code.

Focus on specific lines (lines 4-5) - other lines are dimmed

JavaScript
api/users.js

Focus on a range using string syntax (lines 1-2, 8)

JavaScript
api/users.js

API Reference

The CodeBlock component is a custom implementation built with Shiki for syntax highlighting. It does not extend Base UI.

Props

CodeBlock

The main component that manages state and provides context to sub-components automatically via React Context. Sub-components can access code, language, nodes, and lines without explicit prop passing.

Prop

CodeBlockHeader

Smart header component that auto-arranges language icons, filenames, tabs, and copy button based on props. Automatically consumes language and code from CodeBlock context.

Prop

CodeBlockPre

The pre-formatted text container that wraps the code content. Automatically consumes lines from CodeBlock context for line numbers. Includes group and relative classes for floating button support. Uses ScrollArea internally for styled scrollbars and fade edges.

Prop

CodeBlockCode

The actual syntax-highlighted code element. Automatically consumes code and nodes (highlighted JSX) from CodeBlock context.

Prop