suiss/ui · Component
Code Block
A surface for code, terminal output and diffs, with copy, line numbers, line states and streaming syntax highlighting.
Code Block is an open-source component from suiss/ui for React, React Native, SwiftUI and Jetpack Compose. The @esuiss/ui CLI copies its source into your project, so you own the code.
CodeBlock shows code and command output in a rounded surface. Put the code in CodeBlockContent and, optionally, a CodeBlockHeader with a title and actions above it and a CodeBlockFooter below it.
Pass a string to CodeBlockContent for plain output, or a list of CodeBlockLine when lines need a state or highlighted tokens. useHighlightedCode highlights with Shiki and keeps working while the code streams in.
Installation
Works in any project with a components.json. See Installation for the one-line registry setup. Or use the suiss/ui CLI:
- Copy and paste the following code into your project.
- For syntax highlighting, copy the highlighter into
lib/highlighter.tsx:
- Copy the hook into
hooks/use-highlighted-code.ts:
- Update the import paths to match your project setup.
Usage
Composition
Use the following composition to build a CodeBlock:
Examples
Basic
A string passed straight to CodeBlockContent.
Header
A CodeBlockHeader with a title and a copy button. Long titles truncate.
Actions
Extra buttons next to the copy button. The wrap toggle keeps its own state and passes it to wrap.
Floating actions
Without a header, place CodeBlockActions directly in CodeBlock. The actions float at the top end and appear on hover or focus, and always on touch screens.
Line numbers
Set lineNumbers on CodeBlockContent. A string child is split into lines.
Start line
Set startLine to number a snippet from the middle of a file. The gutter widens to fit the largest number.
Wrap
Lines scroll by default. Set wrap for tool output and error messages.
Highlighted lines
Render each line as a CodeBlockLine and set variant="highlighted" on the ones to call out.
Diff
variant="added" and variant="removed" tint a line and add a + or − marker.
Height
A max-h-* class on CodeBlockContent caps the block. The content scrolls inside and its edges fade with scroll-fade.
Footer
A CodeBlockFooter with secondary text below the code.
Show more
Cap the content while collapsed and toggle it from a ghost button in CodeBlockFooter.
Show more with max height
Raise the cap instead of removing it, so a long expanded block still scrolls inside the page.
Size
size="sm" tightens the padding and uses text-xs, for tool and trace output.
Destructive
variant="destructive" for errors and failed output.
Trace
A small, wrapping CodeBlock inside a collapsible trace step for terminal output.
Tabs
Several files in Tabs, one CodeBlock per panel.
Message
A CodeBlock between paragraphs of a Message.
Syntax highlighting
useHighlightedCode returns one React node per line, ready for CodeBlockLine.
Shiki runs on its JavaScript regex engine, so there is no WASM to load.
Languages
Each language loads on first use. The list lives in lib/highlighter.tsx and ships with bash, css, diff, html, javascript, json, jsx, markdown, python, sql, tsx, typescript and yaml, plus aliases such as ts, js, sh and yml. Any other language renders as plain text.
Terminal output
Pass "ansi" as the language to turn escape codes into colors. Colors do not carry over from one line to the next.
Streaming
While code streams in, the hook tokenizes only the new lines. Unchanged lines keep their nodes, so React skips them.
Server rendering
highlightCode is async and runs in a Server Component, which keeps Shiki out of the client bundle.
Accessibility
CodeBlockContentrenders aprewith acodeelement and always setsdir="ltr", so code reads left to right on a right-to-left page.- The content has
tabIndex={0}so keyboard users can focus and scroll it. PasstabIndex={-1}to remove the tab stop. - Floating actions appear when focus is inside the block, not only on hover.
- Line numbers are hidden from screen readers.
- Added and removed lines carry a
+or−marker as well as a tint, so the change does not rely on color. CodeBlockCopyButtonis labelled "Copy" and announces "Copied" after a successful copy.- Give a wrap toggle
aria-pressed, and a Show more buttonaria-expandedwitharia-controlspointing at the contentid.
API Reference
CodeBlock
The surface that holds the header, the content and the footer.
| Prop | Type | Default |
|---|---|---|
variant | "default" | "destructive" | "default" |
size | "default" | "sm" | "default" |
Also accepts the props of React.ComponentProps<"div">.
size="sm" uses tighter padding and text-xs, for tool output. The root sets data-variant and data-size.
CodeBlockHeader
A row above the content for a title and actions.
Also accepts the props of React.ComponentProps<"div">.
CodeBlockTitle
A filename or label with an optional icon. It truncates.
Also accepts the props of React.ComponentProps<"div">.
CodeBlockActions
Buttons at the end of the header. As a direct child of CodeBlock, it floats over the content.
Also accepts the props of React.ComponentProps<"div">.
CodeBlockCopyButton
A ghost icon button that copies value and announces "Copied" in a polite live region.
| Prop | Type | Default |
|---|---|---|
value | string | - |
Also accepts the props of Button.
value is required.
CodeBlockContent
The pre and code pair that scrolls or wraps.
| Prop | Type | Default |
|---|---|---|
children | React.ReactNode | - |
wrap | boolean | false |
lineNumbers | boolean | false |
startLine | number | 1 |
tabIndex | number | 0 |
Also accepts the props of React.ComponentProps<"pre">.
Pass a string or a list of CodeBlockLine. A string child renders as a single text node, so streamed output stays cheap; with lineNumbers it is split into lines. startLine is the number of the first line when lineNumbers is set. tabIndex keeps the content focusable so it scrolls by keyboard. Line numbers and diff markers are left out of a selection, and the content always reads left to right, while the header follows the page direction.
CodeBlockLine
One line of code.
| Prop | Type | Default |
|---|---|---|
variant | "default" | "added" | "removed" | "highlighted" | "default" |
Also accepts the props of React.ComponentProps<"span">.
Each line sets data-variant.
CodeBlockFooter
A row below the content, such as for a Show more button.
Also accepts the props of React.ComponentProps<"div">.
useHighlightedCode
Returns one React node per line. Until the first pass finishes, and for a language outside the list, it returns plain lines.
| Argument | Type | Description |
|---|---|---|
code | string | The code to highlight. A trailing newline is ignored. |
language | string | A language or alias from lib/highlighter.tsx, or "ansi". |
Each part sets a data-slot, such as code-block-content or code-block-line, for styling.
Web preview
A CodeBlock shows code in a rounded, muted surface. Put a CodeBlockTitle and CodeBlockActions with a CodeBlockCopyButton in a CodeBlockHeader, the code in CodeBlockContent, and secondary text in a CodeBlockFooter.
CodeBlockContent takes a string or CodeBlockLine children. Long lines scroll sideways unless you set wrap. Pass language to color keywords, strings, numbers and comments with a small built-in highlighter that uses the theme colors, so it follows light and dark mode and every style.
Installation
-
Target iOS 17 or later.
-
Add the Swift package to your app:
- Copy the theme files every view uses:
- Copy the source:
- Add the files to your app target.
- Apply the Compose compiler plugin, enable Compose, set
compileSdkto 37 or higher andminSdkto 24 or higher, and add the dependencies:
- Copy the theme files every view uses:
- Copy the source and every file it uses:
- Update the
com.esuiss.uipackage names to match your project.
Usage
Examples
Basic
A single line with no header.
Web preview
Line numbers
Numbers every line with lineNumbers.
Web preview
Start line
Starts the numbering at startLine for an excerpt of a longer file.
Web preview
Wrap
Wraps long lines instead of scrolling sideways.
Web preview
Highlighted lines
CodeBlockLine with variant="highlighted" tints the lines that matter.
Web preview
Diff
added and removed lines get a tint and a + or − marker.
Web preview
Size
The sm size for code inside a message or a trace.
Web preview
Destructive
The destructive variant for errors.
Web preview
Languages
The built-in highlighter colors TypeScript, JSON, shell, Swift and Kotlin with the chart colors of the theme.
Web preview
Footer
A CodeBlockFooter with secondary text below the code.
Web preview
Accessibility
- The copy button has the label Copy, and Copied after a copy.
- Line numbers and diff markers are hidden from assistive technology.
API Reference
CodeBlock
The surface that holds the header, code and footer.
| Prop | Type | Default |
|---|---|---|
variant | "default" | "destructive" | "default" |
size | "default" | "sm" | "default" |
Also accepts the props of ViewProps.
variant switches between the muted surface and a destructive tint for errors. size sets the text size, padding and corner radius.
CodeBlockHeader
A row above the code for an icon, a title and actions.
CodeBlockTitle
One line of text that truncates, usually a file name.
CodeBlockActions
Holds buttons at the end of the header.
CodeBlockCopyButton
Copies value and shows a check for two seconds.
| Prop | Type | Default |
|---|---|---|
value | string | - |
The copy button uses expo-clipboard.
CodeBlockContent
The code, from a string or CodeBlockLine children.
| Prop | Type | Default |
|---|---|---|
language | string | - |
wrap | boolean | false |
lineNumbers | boolean | false |
startLine | number | 1 |
language colors the code: ts, tsx, js, json, bash, python, swift, kotlin and their aliases.
CodeBlockLine
One line of code.
| Prop | Type | Default |
|---|---|---|
variant | "default" | "added" | "removed" | "highlighted" | "default" |
added and removed tint the line and add a + or − marker.
CodeBlockFooter
Secondary text below the code.
CodeBlockVariant
Values: .default, .destructive.
CodeBlockSize
Values: .default, .sm.
CodeBlockLineVariant
Values: .default, .added, .removed, .highlighted.
CodeBlockLine
| Parameter | Type | Default |
|---|---|---|
_Â text | String | - |
variant | CodeBlockLineVariant: .default | .added | .removed | .highlighted | .default |
| Property | Type |
|---|---|
id | UUID |
text | String |
variant | CodeBlockLineVariant |
CodeBlock
The surface that holds the header, code and footer.
| Parameter | Type | Default |
|---|---|---|
_Â variant | CodeBlockVariant: .default | .destructive | .default |
size | CodeBlockSize: .default | .sm | .default |
content | () -> Content | - |
CodeBlockHeader
A row above the code for an icon, a title and actions.
| Parameter | Type | Default |
|---|---|---|
content | () -> Content | - |
CodeBlockTitle
One line of text that truncates, usually a file name.
| Parameter | Type | Default |
|---|---|---|
_Â title | LocalizedStringKey | - |
verbatim | String | - |
Overloads: init(_:), init(verbatim:).
CodeBlockActions
Holds buttons at the end of the header.
| Parameter | Type | Default |
|---|---|---|
content | () -> Content | - |
CodeBlockCopyButton
Copies value and shows a check for two seconds.
| Parameter | Type | Default |
|---|---|---|
value | String | - |
CodeBlockContent
The code, from a string or CodeBlockLine children.
| Parameter | Type | Default |
|---|---|---|
_Â code | String | - |
language | String? | nil |
wrap | Bool | false |
lineNumbers | Bool | false |
startLine | Int | 1 |
lines | [CodeBlockLine] | - |
Overloads: init(_:language:wrap:lineNumbers:startLine:), init(lines:language:wrap:lineNumbers:startLine:).
CodeBlockFooter
Secondary text below the code.
| Parameter | Type | Default |
|---|---|---|
content | () -> Content | - |
CodeBlockVariant
Values: Default, Destructive.
CodeBlockSize
Values: Default, Sm.
CodeBlockLineVariant
Values: Default, Added, Removed, Highlighted.
CodeBlockLine
| Parameter | Type | Default |
|---|---|---|
text | String | - |
variant | CodeBlockLineVariant: Default | Added | Removed | Highlighted | CodeBlockLineVariant.Default |
CodeBlock
The surface that holds the header, code and footer.
| Parameter | Type | Default |
|---|---|---|
modifier | Modifier | Modifier |
variant | CodeBlockVariant: Default | Destructive | CodeBlockVariant.Default |
size | CodeBlockSize: Default | Sm | CodeBlockSize.Default |
content | @Composable ColumnScope.() -> Unit | - |
CodeBlockHeader
A row above the code for an icon, a title and actions.
| Parameter | Type | Default |
|---|---|---|
modifier | Modifier | Modifier |
content | @Composable RowScope.() -> Unit | - |
CodeBlockTitle
One line of text that truncates, usually a file name.
Receiver: RowScope.
| Parameter | Type | Default |
|---|---|---|
text | String | - |
modifier | Modifier | Modifier |
CodeBlockActions
Holds buttons at the end of the header.
Receiver: RowScope.
| Parameter | Type | Default |
|---|---|---|
modifier | Modifier | Modifier |
content | @Composable RowScope.() -> Unit | - |
CodeBlockCopyButton
Copies value and shows a check for two seconds.
| Parameter | Type | Default |
|---|---|---|
value | String | - |
modifier | Modifier | Modifier |
CodeBlockContent
The code, from a string or CodeBlockLine children.
| Parameter | Type | Default |
|---|---|---|
code | String | - |
modifier | Modifier | Modifier |
language | String? | null |
wrap | Boolean | false |
lineNumbers | Boolean | false |
startLine | Int | 1 |
lines | List<CodeBlockLine> | - |
Overloads: CodeBlockContent(code), CodeBlockContent(lines).
CodeBlockFooter
Secondary text below the code.
| Parameter | Type | Default |
|---|---|---|
modifier | Modifier | Modifier |
content | @Composable RowScope.() -> Unit | - |