React Native · Components
CodeBlock
Syntax-highlighted code block.
Live example
The preview runs in the browser with the web component; the native component takes the same props and looks the same on iOS and Android.
Preview
Code
Events
- Events appear here when you interact with the preview.
Comparing two codes: side by side (split), changed words highlighted
Without a title: language name in the corner, copy button on hover (always on touch)
Added / removed lines (automatic in the diff language)
Long code: collapsing with maxLines
Import
CodeBlock
componentSyntax-highlighted code block. The same highlighter, languages and theme colors (theme.code) as web's CodeBlock; looks the same on iOS and Android.
Supports line numbers, highlighted / added / removed lines, horizontal scrolling or wrapping (wrap) for long lines, collapsing long code (maxLines), a diff between two pieces of code (original) and copying to the clipboard.
Examples
loadLanguage
functionLoads a language's rules. The same language always gets the same Promise, marked the way React expects once resolved, so use() reads a loaded language right away without suspending. Resolves with null for an unknown language.
registerLanguage
functionAdds your own language or replaces a built-in one. Rules are ordered; the first rule matching at a position wins. Call it once at app start.
Examples
Line selection: line numbers and ranges. Can be an array ([1, [4, 6]]) or a string ("1,4-6"). Numbers are the line numbers shown on screen (relative to startLine).
CodeBlockProps
interfaceProps of CodeBlock.
| Name | Type | Default | Description |
|---|---|---|---|
ref? | Ref<ViewRef> | Ref of the underlying native element. | |
code | string | Code to show. A single leading and trailing empty line is trimmed. | |
language? | CodeLanguage | Highlighting language: tsx, ts, js, json, html, css, bash, python, sql, md, yaml, diff, go, rust, java, kotlin, c, cpp, csharp, php, ruby, swift, graphql, dockerfile, toml… The same highlighter and languages as web. An unknown name or "text" is shown as plain text. For your own language use registerLanguage. | |
title? | ReactNode | Title shown in the top bar, usually a file name ("app/page.tsx"). | |
icon? | IconProp | Icon to the left of the title: a name or an element. | |
header? | boolean | Show the top bar (title, language name, copy). @defaultValue true if there is a title, icon, language name or copy button | |
lineNumbers? | boolean | false`; `true` in diff mode (when `original` is given) | Show line numbers. |
startLine? | number | 1 | Number of the first line. |
highlightLines? | CodeLineSelection | Lines to highlight: [3, [5, 7]] or "3,5-7". Ignored in diff mode. | |
addedLines? | CodeLineSelection | Lines to show as added (green). With language="diff", + lines are marked automatically. | |
removedLines? | CodeLineSelection | Lines to show as removed (red). With language="diff", - lines are marked automatically. | |
wrap? | boolean | false | Wrap long lines instead of scrolling horizontally. |
maxHeight? | number | Code taller than this height (pixels) scrolls vertically. | |
maxLines? | number | If the code is longer than this many lines, only the first lines are shown with a "Show all" button below. Ignored in diff mode. | |
original? | string | Old code to compare against. When given the block switches to diff mode: code is the new version, removed lines show in red, added ones in green, and words changed within a line are highlighted separately (the same Myers algorithm as web). The header shows +added −removed counts. It is a single-column view for phone screens (web's "unified"). | |
contextLines? | number | false | 3 | Number of unchanged lines shown around changes in diff mode; longer unchanged regions are folded and expand on press. false shows all. |
copyable? | boolean | true | Show the copy button. If the app has no clipboard module (Clipboard) the button does not appear. |
onCopy? | (code: string) => void | Called with the copied text when the code is copied. | |
fontSize? | number | 13 | Font size (pixels). |
style? | StyleProp<ViewStyle> | Container style. | |
codeStyle? | StyleProp<TextStyle> | Style of the code text (e.g. font). |
CodeTokenType
typeHighlighted token kinds. Each kind is colored by a --ui-code-* theme variable (e.g. keyword → --ui-code-keyword).
CodeRule
interfaceA language rule.
| Name | Type | Description |
|---|---|---|
type? | CodeTokenType | Kind of the matched text; plain text when undefined. With inside, text the inner language cannot match gets this kind. |
pattern | RegExp | Pattern to match. All rules combine into one expression with the gm flags (^ and $ are line start/end); the i flag is supported, others are ignored. Do not use back references (\1): numbers shift when combined; capture groups are fine. |
inside? | CodeGrammar | (() => CodeGrammar) | Also highlight the matched text with this language (e.g. ${…} in a template string, <style> in HTML). A function is called on first use; it lets a language refer to itself. |
CodeLanguageName
typeCanonical names of the languages built into the library.
CodeGrammar
typeOrdered rules of a language.
CodeLanguage
typeThe language prop of CodeBlock: a built-in language name or alias (ts, py, sh, yml…), a language added with registerLanguage, or plain text that is not colored ("text").