On this page
useHighlightedCode
Highlights code as React nodes for each line.
Overview
Copy the source into your project. Import the parts that you need. Styles use local StyleX declarations and shared theme tokens. You can change the source without a wrapper or replacement package.
Preview
Syntax highlighting with editable source and language selection
Installation
Run the init command from your project root first. The CLI configures StyleX and installs shared files and dependencies.
Run from your project root. The CLI installs required components, shared files, and packages. Previews may need additional components. The CLI keeps existing files.
Usage
Pass source text and a language to useHighlightedCode. It returns one React node per line. Render each node inside CodeblockLine.
The hook supports server rendering and updates when the source or language changes. Unknown languages return plain text. One trailing newline is removed.
During streaming, completed lines keep their grammar state. New lines continue from that state. Source replacement or language changes clear the cached lines.
The highlighter uses the JavaScript regular expression engine. It does not need a WebAssembly file. Static language imports keep the copied source compatible with the application bundler.
Install codeblock separately when you use its display components. Pass the original source to CodeblockCopyButton so copied text excludes line numbers and change markers.
Import from the local file installed by the CLI.
API reference
The generator reads the component TypeScript source. Only component and Base UI props appear below. The Required column shows whether you must supply a property. It does not mean you must use the component.