TrademarkTrademark
Docs
Ctrl k
Search docs...
Components

CodeBlock

Updated: October 18, 2026
View on GitHub
Soon
Not released yet. This is on main and is not in any published version yet. It will not install today.

CodeBlock shows one or more snippets of code in a surface with a header. Pass them in codes, each with code, language and label. With several snippets the header becomes a row of tabs. The code is set in the code font and scrolls sideways instead of wrapping. A small built-in tokenizer colours keywords, strings, comments and numbers for js, jsx, ts, tsx, css, bash and json, with the same --code-* colours as the web. Other languages show as plain text. See the web CodeBlock page for the web version.

Multiple snippets

Each entry of codes gets a tab. onInstanceChange is called with the index of the tab the user picks.

Copying

The copy button is on by default. Pass onCopy to put the code on the clipboard yourself, for example with expo-clipboard. Without onCopy, CodeBlock uses React Native's own Clipboard. React Native 0.87 still ships it, but marks it deprecated and logs a warning the first time it is used. After a copy the icon turns into a check for five seconds, and screen readers hear "Copied". Set copyButton={false} to hide the button.

Line numbers and highlights

lineNumbers adds a gutter. startLineNumber sets the first number. highlight on an entry marks lines, as "1,3-5", with a brand tint and a brand edge.

Compact, collapsible and preview

compact removes the header and floats the copy button over the code. isCollapsible shows the first maxLines lines and a "View code" button. preview renders a live example above the code, and reloadButton adds a button that mounts it again.

Diffs

Set language to ["diff", "tsx"] for a unified diff. Added lines get a success tint and deleted lines a danger tint. The code after the sign is coloured in the second language.

Differences from the web

  • Colours come from a small built-in tokenizer, not Prism, and there is no prismjs dependency. It knows js, jsx, ts, tsx, css, bash and json, plus the aliases javascript, typescript, mdx, sh, shell, zsh, jsonc and scss. It colours keywords, strings, comments, numbers, booleans, function calls, JSX tags, JSON keys, CSS properties and at-rules, and bash variables. It does not know regular expression literals or CSS selectors. Other languages are plain text.
  • onCopy is new. The web writes to navigator.clipboard. Here, without onCopy, the button uses React Native's deprecated Clipboard.
  • There is no fullscreenButton and no styleButton. The style panel is a web overlay.
  • A diff has one column of line numbers, for the new file. Deleted lines have no number. The web shows the old and new numbers side by side.
  • The collapsed view cuts the code at maxLines and shows the "View code" button under it. There is no fade over the last lines.
  • Long lines scroll sideways and are never wrapped, as white-space: pre on the web. codeHeight adds a vertical scroll past that height, in rem.
  • The tabs are ToggleButtons in a horizontal scroll, announced to screen readers as tabs.
  • The code uses the body-s size with a line height of 1.75, as the web, in fonts.code from ThemeProvider. Without it, the platform monospace font is used (Menlo on iOS, monospace on Android).

API reference

Prop
Type
Default
Description
codesCodeInstance[][]The snippets. Each has code (a string, or { content, error }), language (a string, or ["diff", language]), label, and optional highlight, prefixIcon and startLineNumber.
copyButtonbooleantrueShows the copy button.
onCopy(code: string) => unknownPuts the code on the clipboard. Without it, React Native's deprecated Clipboard is used.
lineNumbersbooleanfalseAdds a line number gutter.
compactbooleanfalseRemoves the header. The copy button floats over the code.
previewReactNodeA live example above the code.
previewPaddingSpacingToken"l"Padding around the preview.
reloadButtonbooleanfalseA button that mounts the preview again.
codeHeightnumberThe code's maximum height in rem. It scrolls past it.
isCollapsiblebooleanfalseShows only maxLines lines until "View code" is pressed.
maxLinesnumber5Lines shown while collapsed.
hideCodebooleanfalseHides the code, leaving the header and preview.
onInstanceChange(index: number) => voidCalled when a tab is picked.
highlightstringDeprecated. Set highlight on each entry of codes instead.
...flexFlexPropsFlex props for the outer surface, such as background, radius and marginTop.
Edit this page on GitHub
TrademarkTrademark
Built with curiosity by Lorant One and the Design Engineers Club