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.
Each entry of codes gets a tab. onInstanceChange is called with the index of the tab the user picks.
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.
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 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.
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.
- 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).