Skip to content

CodeBlock

Source code in a mono block: syntax colouring, optional line numbers, a copy button.

Group: Text · Kind: native · in the lite catalog

example
ts
const answer = 42 // the answer
variant · default
ts
const answer = 42 // the answer
variant · destructive
ts
const answer = 42 // the answer
size · default
ts
const answer = 42 // the answer
size · sm
ts
const answer = 42 // the answer

Open in the playground

The same specimen painted by React, SwiftUI, Compose and gpui.

exponential-ui-code-block on Web
exponential-ui-code-block on iOS
exponential-ui-code-block on Android
exponential-ui-code-block on Desktop
wrap
ts
const answer = 42 // the answer
Prop Type Required Bindable Description
code string yes yes The source text.
language plain | json | js | ts | tsx | rust | swift | kotlin | python | bash | html | css | sql | yaml | markdown | ansi – – The syntax colouring. Default "plain".
title string – – A header line (a file name).
lineNumbers boolean – – Shows a line-number gutter. Default false.
wrap boolean – – Wraps long lines instead of scrolling sideways. Default false.
copyable boolean – – Shows the copy-to-clipboard button. Default true.
highlight [number] – – 1-based line numbers to emphasise.
maxLines number – – Lines shown before the block scrolls.
variant default | destructive – yes destructive = failed output. Default "default".
size default | sm – – sm = compact. Default "default".

None.

None.

none

No basic catalog component maps here.

A native component: every renderer paints it directly.

Role: region

region (code), copy = button

Keys Action
Tab reaches the copy button; Enter Space copy and announce copied

Recipe parts: CodeBlock/root CodeBlock/header CodeBlock/title CodeBlock/copy CodeBlock/gutter CodeBlock/line CodeBlock/lineNumber CodeBlock/token CodeBlock/body CodeBlock/code

Keyed by: lineNumbers wrap kind variant size :hover :pressed :focus :disabled :checked :open :selected :invalid :dragover

Themes · Write a theme

Text · Heading · Markdown · Kbd · Label