Composables
ComposableOriginal

Code Block

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

The code block renders syntax-highlighted code with optional headers, filenames, tabs, line numbers, diffs, and a copy button. Use it for documentation snippets, install commands, and any multi-line code sample.

To copy a single string from an inline icon button, use CopyButton instead.

Preview

JavaScript

Installation

Usage

Composition

Examples

Floating Copy Button

Set floatingCopy on CodeBlock to show a copy button that appears on hover, without a header.

With Filename

Pass filename to CodeBlockHeader to label the snippet.

TypeScript
types/user.ts

With Tabs

Pass tabs to CodeBlockHeader to switch between code variants such as package managers or languages.

Switch between package managers using the tabs in the header.

With Line Numbers

Set lineNumbers on CodeBlockPre to number each line.

JavaScript

Multiple Languages

Set language on CodeBlock to highlight any Shiki-supported language.

TypeScript

TypeScript

Python

Python

Bash

Custom Icon

Pass customIcon to CodeBlockHeader to replace the default language icon.

Custom Height

Set a max-h-* class on CodeBlockPre to cap the height and scroll overflow.

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

TypeScript
config/theme.ts

Server-Side Highlighting

Pre-highlight on the server with the highlight function and pass the result as initial for instant rendering.

TypeScript
lib/api.ts

Highlighted Lines

Pass highlightLines to CodeBlock to emphasize lines with a background and left border.

Highlight specific lines (lines 2, 5)

JavaScript
utils/cart.js

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

JavaScript
utils/cart.js

Diffs

Set showDiff to mark added, removed, and modified lines; the diff markers are 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

Pass focusLines to CodeBlock to blur every line except the ones you name.

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