CodeBlock
Syntax-highlighted code display with single-file and multi-tab modes, a copy button, line highlighting, wrap and collapse โ on a self-contained neutral plate that can follow or lock its theme.
Playground
Installation
pnpm add @tessinaui/ui react-syntax-highlighter @types/react-syntax-highlighterUsage
import { CodeBlock } from "@tessinaui/ui";<CodeBlock
language="typescript"
filename="utils.ts"
code={`export const add = (a: number, b: number) => a + b;`}
/>When to use โ and when not to
CodeBlock displays static code: a snippet in docs, an install command, an API
example, a code answer in an AI chat. It highlights, copies, wraps, collapses and
tabs between files, and it paints its own neutral plate so it reads on any surface.
- Live terminal / log streams โ a scrolling console with timestamps and filters is a log viewer, not a code block. Render the stream in its own component.
- Diffs โ added/removed rows with dual gutters and split/unified views are their
own control; a
CodeBlockshows a snapshot, not a change. - Inline code inside a sentence โ use a
<code>element (or an inlineCodecomponent);CodeBlockis always a block.
Examples
Default
A single-file code block with a filename and copy button.
Command
Hide the header with showHeader={false} for a bare snippet โ an install command or
a one-liner โ with the copy control floating over the code. This is the dominant
shape in developer docs.
Multi-tab
Provide tabs to switch between multiple files, each with its own language and copy
state. The tab bar is one keyboard tab stop; arrow keys move between files.
Package managers
The tabs pattern is how a docs page offers the same command for npm, pnpm, yarn and bun.
Line highlighting
Highlight specific lines with highlightLines, paired with showLineNumbers for
clarity.
Collapsible
Cap the height with maxHeight to reveal an expand/collapse toggle for long blocks.
showMoreLabel / showLessLabel relabel the toggle.
Wrap
By default a long line scrolls horizontally. Set showLineWrap to wrap it instead โ
useful for prose-like commands where horizontal scrolling hides the tail.
Sizes
Three sizes โ sm, md, and lg โ scale the whole block: code padding, the code
type scale, and the chrome around it (header height, filename, copy control, tab
bar, expand row). Type follows the shared data-display ladder, with the header one
step below the code it labels.
Header actions
Pass any controls to headerActions โ a download button, an "open in playground"
link, a language Select โ and they sit in the header before the copy button.
Theme
The plate is self-contained. theme="auto" (default) follows the page; theme="dark"
or theme="light" locks it โ the dark-code-on-a-light-docs-page convention.
Loading
CodeBlockSkeleton renders a placeholder that matches the block's box while code is
being fetched.
Language identifiers
language accepts any identifier supported by Prism. Common values:
typescript ยท tsx ยท javascript ยท jsx ยท bash ยท shell ยท python ยท css ยท scss ยท json ยท yaml ยท markdown ยท html ยท sql ยท rust ยท go
Pass "text" or omit language in a tab to display plain text with no highlighting.
API Reference
CodeBlock
Either code + language (single-file) or tabs (multi-file) is required. The
root forwards className and any other div attributes (data-*, id, aria-*).
| Prop | Type | Default | Description |
|---|---|---|---|
code | string | โ | Code string โ single-file mode |
language | string | โ | Prism language identifier |
filename | string | โ | Shown in the header; when omitted the language badge is used |
highlightLines | number[] | [] | 1-indexed line numbers to highlight |
tabs | CodeBlockTab[] | โ | Multi-tab mode โ replaces code, language, filename |
theme | "auto" | "light" | "dark" | "auto" | auto follows the page; light / dark lock the plate |
size | "sm" | "md" | "lg" | "md" | Padding, code type scale, and header/tab/expand chrome |
rounded | "none" | "sm" | "md" | "lg" | "full" | "lg" | Corner radius โ full caps at the panel radius |
showLineNumbers | boolean | false | Renders the line-number gutter |
showLineWrap | boolean | false | Wrap long lines instead of scrolling horizontally |
showHeader | boolean | true | Single-code mode only โ when false the copy control floats |
showLangBadge | boolean | true | Show the language badge when no filename is set |
headerActions | ReactNode | โ | Extra controls in the header, before the copy button |
maxHeight | string | number | โ | Caps block height and adds an expand/collapse toggle |
showMoreLabel | string | "Show more" | Expand-control label when collapsed |
showLessLabel | string | "Show less" | Expand-control label when expanded |
defaultCollapsed | boolean | true | Initial collapsed state when maxHeight is set |
collapsed | boolean | โ | Controlled collapsed state |
onCollapsedChange | (collapsed: boolean) => void | โ | Fires when the expand/collapse button is clicked |
CodeBlockSkeleton
Mirrors the block's box (ยง2b). Takes size, rounded, theme, plus
lines (default 6), showHeader, showFilename, showLangBadge,
showLineNumbers and maxHeight, and forwards className + div attributes.
CodeBlockTab
interface CodeBlockTab {
name: string; // Label shown in the tab button
code: string; // Code content for this tab
language?: string; // Prism language (defaults to "text")
highlightLines?: number[]; // 1-indexed lines to highlight in this tab
}