CodeBlock
A block of code with a header (language, filename, app actions, Copy), syntax highlighting, optional line numbers and a height cap.
Overview
CodeBlock shows code under a header with the language, an optional filename, your own actions and a Copy button. Syntax highlighting uses Shiki, which loads on first use. Until then the code shows as plain text in the same layout, so nothing shifts when the colors arrive.
Colors come from the --code-* and --code-token-* tokens rather than a Shiki theme, so light, dark and tenant themes apply without highlighting again. Highlighting also holds while more code streams in.
Import
import { CodeBlock } from '@unflowio/ui/components/CodeBlock';Props
| Prop | Type | Default | Description |
|---|---|---|---|
code | string | — | Required. The code to show |
language | string | — | Fence language, e.g. ts, python, bash. Unsupported values show their name in the header with plain text |
filename | string | — | Shown in the header after the language |
showLineNumbers | boolean | false | Shows a line number gutter |
wrap | boolean | false | Wraps long lines instead of scrolling horizontally |
maxHeight | number | — | Caps the height in px; longer code gets a "Show more" toggle |
actions | ReactNode | — | App actions in the header, before Copy (e.g. "Insert", "Save as snippet") |
Highlighted languages
TypeScript, JavaScript, TSX, JSX, JSON, Python, SQL, Bash, HTML, CSS, YAML and Markdown, plus these aliases: ts, js, mjs, cjs, py, sh, shell, shellscript, zsh, yml and md. Each grammar loads only when a block first uses it. With no language the header says "Text".
Basic usage
{ "event": "post_click", "utm_campaign": "launch" }<CodeBlock
code='{ "event": "post_click", "utm_campaign": "launch" }'
language="json"
filename="tracking plan"
/>Line numbers
export class SeatsService {
constructor(private readonly seats: SeatsRepository) {}
async used(environmentId: string): Promise<number> {
const rows = await this.seats.findBy({ environmentId, active: true });
return rows.length;
}
}<CodeBlock code={source} language="ts" filename="seats.service.ts" showLineNumbers />Header actions
actions renders before the Copy button. Use the text-code-header-fg color so actions stay legible on the header in both themes.
{ "event": "post_click", "utm_campaign": "launch" }<CodeBlock
code={snippet}
language="json"
actions={
<button
type="button"
className="rounded-md px-2 py-1 text-xs font-medium text-code-header-fg hover:bg-code-fg/10"
onClick={() => saveSnippet(snippet)}
>
Save as snippet
</button>
}
/>Long code
maxHeight caps the block and adds a "Show more" / "Show less" toggle. wrap wraps long lines instead of scrolling them horizontally.
SELECT team, count(*) AS seats FROM members WHERE team_id = 1 GROUP BY team;
SELECT team, count(*) AS seats FROM members WHERE team_id = 2 GROUP BY team;
SELECT team, count(*) AS seats FROM members WHERE team_id = 3 GROUP BY team;
SELECT team, count(*) AS seats FROM members WHERE team_id = 4 GROUP BY team;
SELECT team, count(*) AS seats FROM members WHERE team_id = 5 GROUP BY team;
SELECT team, count(*) AS seats FROM members WHERE team_id = 6 GROUP BY team;
SELECT team, count(*) AS seats FROM members WHERE team_id = 7 GROUP BY team;
SELECT team, count(*) AS seats FROM members WHERE team_id = 8 GROUP BY team;
SELECT team, count(*) AS seats FROM members WHERE team_id = 9 GROUP BY team;
SELECT team, count(*) AS seats FROM members WHERE team_id = 10 GROUP BY team;
SELECT team, count(*) AS seats FROM members WHERE team_id = 11 GROUP BY team;
SELECT team, count(*) AS seats FROM members WHERE team_id = 12 GROUP BY team;
SELECT team, count(*) AS seats FROM members WHERE team_id = 13 GROUP BY team;
SELECT team, count(*) AS seats FROM members WHERE team_id = 14 GROUP BY team;
SELECT team, count(*) AS seats FROM members WHERE team_id = 15 GROUP BY team;
SELECT team, count(*) AS seats FROM members WHERE team_id = 16 GROUP BY team;
SELECT team, count(*) AS seats FROM members WHERE team_id = 17 GROUP BY team;
SELECT team, count(*) AS seats FROM members WHERE team_id = 18 GROUP BY team;
SELECT team, count(*) AS seats FROM members WHERE team_id = 19 GROUP BY team;
SELECT team, count(*) AS seats FROM members WHERE team_id = 20 GROUP BY team;
SELECT team, count(*) AS seats FROM members WHERE team_id = 21 GROUP BY team;
SELECT team, count(*) AS seats FROM members WHERE team_id = 22 GROUP BY team;
SELECT team, count(*) AS seats FROM members WHERE team_id = 23 GROUP BY team;
SELECT team, count(*) AS seats FROM members WHERE team_id = 24 GROUP BY team;
SELECT team, count(*) AS seats FROM members WHERE team_id = 25 GROUP BY team;
SELECT team, count(*) AS seats FROM members WHERE team_id = 26 GROUP BY team;
SELECT team, count(*) AS seats FROM members WHERE team_id = 27 GROUP BY team;
SELECT team, count(*) AS seats FROM members WHERE team_id = 28 GROUP BY team;
SELECT team, count(*) AS seats FROM members WHERE team_id = 29 GROUP BY team;
SELECT team, count(*) AS seats FROM members WHERE team_id = 30 GROUP BY team;SELECT team, count(*) AS seats FROM members WHERE team_id = 1 GROUP BY team; SELECT team, count(*) AS seats FROM members WHERE team_id = 2 GROUP BY team; SELECT team, count(*) AS seats FROM members WHERE team_id = 3 GROUP BY team;<CodeBlock code={query} language="sql" maxHeight={200} />
<CodeBlock code={oneLongLine} language="sql" wrap />Customization (slotProps)
| Slot Prop | Props | Description |
|---|---|---|
header | BaseHTMLProps<HTMLDivElement> | The header row |
pre | BaseHTMLProps<HTMLPreElement> | The <pre> element holding the code |
Accessibility
- Copy uses the Clipboard API and falls back to
execCommandwhen it is missing. The result is announced ("Copied" or "Could not copy"), and the button resets afterwards. - Line numbers are hidden from assistive technology, so screen readers read only the code.
- The code area is focusable, so keyboard users can scroll long lines horizontally.
- Use InlineCode for code inside a sentence.