CodeBlock
A terminal-style code block with syntax-friendly display, line numbers, a copy button, and horizontal scroll for long lines.
CodeBlock
Design lines
1const x = 1;1<Tabs defaultValue="tuix">2 <TabsList>3 <Tab value="tuix">TUIX</Tab>4 <Tab value="rubik">Rubik</Tab>5 </TabsList>6 <TabPanel value="tuix">7 <CodeBlock code="const x = 1;" language="ts" design="tuix" />8 </TabPanel>9 <TabPanel value="rubik">10 <CodeBlock code="const x = 1;" language="ts" design="rubik" />11 </TabPanel>12</Tabs>Demo
Basic (code / language)
Minimal usage: pass code and language, with optional color.
1const x = 1;1function greet(name: string) {2 return `Hello ${name}`;3}docker compose up -d1<CodeBlock code="const x = 1;" language="typescript" />2<CodeBlock :code="'function greet(name: string) {\n return `Hello ${name}`;\n}'" language="typescript" color="success" />3<CodeBlock code="docker compose up -d" language="bash" color="secondary" :showLines="false" />Language (language)
The language tag rendered for JavaScript and bash.
1console.log("hi")1npm run dev1<CodeBlock code='console.log("hi")' language="javascript" />2<CodeBlock code="npm run dev" language="bash" />Color (color)
Three color values for the border accent.
1const x = 1;1const x = 1;1const x = 1;1<CodeBlock code="const x = 1;" language="ts" color="primary" />2<CodeBlock code="const x = 1;" language="ts" color="success" />3<CodeBlock code="const x = 1;" language="ts" color="danger" />Border (border)
The border shown and hidden with border.
1const x = 1;1const x = 1;1<CodeBlock code="const x = 1;" language="ts" border />2<CodeBlock code="const x = 1;" language="ts" :border="false" />Line numbers (showLines)
The newline must be real: with the static attribute code="...\n..." the \n sequence arrives literally and everything collapses into one line. Use a bound :code so \n is an actual line break.
1line 12line 23line 3line 1line 2line 31<CodeBlock :code="'line 1\nline 2\nline 3'" language="ts" showLines />2<CodeBlock :code="'line 1\nline 2\nline 3'" language="ts" :showLines="false" />Max height (maxHeight)
A long block that scrolls past maxHeight.
1<CodeBlock :code="Array.from({ length: 30 }, (_, i) => `line ${i + 1}`).join('\n')" language="ts" maxHeight="200px" />Preload (preload)
Skip the formatter call (and the async load it triggers) by setting preload to false. Useful for short snippets where loading prettier/standalone is overkill. Default: true.
1const x = 1;1const x = 1;1<CodeBlock code="const x = 1;" language="ts" :preload="false" />2<CodeBlock code="const x = 1;" language="ts" :preload="true" />Props
code
Source code string to display.
| Type | Default | Required |
|---|---|---|
string | '' | ❌ |
language
Language label shown as a tag in the header.
| Type | Default | Required |
|---|---|---|
string | '' | ❌ |
color
Border accent color of the code block.
| Type | Default | Required |
|---|---|---|
'primary' | 'secondary' | 'tertiary' | 'success' | 'warning' | 'danger' | 'light' | 'medium' | 'dark' | DEFAULT_COLOR | ❌ |
border
Toggle the outer border. Defaults to true.
| Type | Default | Required |
|---|---|---|
boolean | true | ❌ |
showLines
Toggle line numbers on the left gutter.
| Type | Default | Required |
|---|---|---|
boolean | true | ❌ |
maxHeight
Max height before the block becomes scrollable.
| Type | Default | Required |
|---|---|---|
string | 'none' | ❌ |
formatter
Optional function to format code before syntax highlighting. Useful for auto-formatting with Prettier.
| Type | Default | Required |
|---|---|---|
(code: string, lang: string) => Promise\<string\> | string | — | ❌ |
preload
Run formatter before highlighting. Defaults to true; set to false to skip the formatter entirely (no Prettier load).
| Type | Default | Required |
|---|---|---|
boolean | true | ❌ |
design
Design line (or inherit from data-design).
| Type | Default | Required |
|---|---|---|
'tuix' | 'rubik' | — (inherits from data-design) | ❌ |
Auto-formatting with usePrettierFormatter
usePrettierFormatter() returns a formatting function that uses Prettier to format code according to the language.
First install Prettier (optional, only needed if you use the formatter):
1npm i -D prettierSupported languages
Language tags accepted by language, each mapped to a Prettier parser.
Language (language) | Prettier parser | Required plugin | Built-in |
|---|---|---|---|
javascript / js / jsx / mjs / cjs | babel | — | ✅ |
typescript / ts / tsx / mts / cts | typescript | — | ✅ |
json / jsonc / json5 | json / jsonc / json5 | — | ✅ |
css / scss / less | css / scss / less | — | ✅ |
html / vue / angular | html | — | ✅ |
markdown / md / mdx | markdown | — | ✅ |
yaml / yml | yaml | — | ✅ |
graphql / gql | graphql | — | ✅ |
flow | flow | — | ✅ |
sql | — | prettier-plugin-sql | ❌ |
python | — | @prettier/plugin-python | ❌ |
ruby | — | @prettier/plugin-ruby | ❌ |
java | — | prettier-plugin-java | ❌ |
rust | — | prettier-plugin-rust | ❌ |
toml | — | prettier-plugin-toml | ❌ |
bash / sh / shell / zsh | — | prettier-plugin-sh | ❌ |
csharp / cs | — | prettier-plugin-csharp | ❌ |
xml | — | @prettier/plugin-xml | ❌ |
Languages marked with ❌ require installing the corresponding Prettier plugin:
1npm i -D prettier-plugin-sql prettier-plugin-java prettier-plugin-rustThe composable automatically detects installed plugins. If a parser is unavailable, it returns the original code unformatted.
Slots
| Slot | Description |
|---|---|
default | Alternative to code prop |
CSS Custom Properties
| Token | Default | Description |
|---|---|---|
--font-mono | "JetBrains Mono", ui-monospace, monospace | Monospace font stack for code-like content (key labels, log lines, chips). |
--accent | from theme (--ctp-latte-mauve / --ctp-mocha-mauve) | Accent color, mapped from the active semantic color. |
--bg | from theme (--ctp-latte-base / --ctp-mocha-base) | Base background of the surface. |
--bg-panel | from theme (--ctp-latte-surface1 / --ctp-mocha-surface1) | Background of panel-like raised surfaces (cards, accordions). |
--border | from theme (--ctp-latte-surface0 / --ctp-mocha-surface0) | Outline and divider color for surfaces. |
--text-muted | from theme (--ctp-latte-subtext1 / --ctp-mocha-subtext1) | Secondary text for hints, annotations and disabled state. |
--color-primary | from theme (--ctp-latte-mauve / --ctp-mocha-mauve) | Primary semantic color (mauve in Catppuccin). |
--color-secondary | from theme (--ctp-latte-lavender / --ctp-mocha-lavender) | Secondary semantic color (lavender in Catppuccin). |
--color-tertiary | from theme (--ctp-latte-blue / --ctp-mocha-blue) | Tertiary semantic color (blue in Catppuccin). |
--color-success | from theme (--ctp-latte-green / --ctp-mocha-green) | Success semantic color (green in Catppuccin). |
--color-warning | from theme (--ctp-latte-yellow / --ctp-mocha-yellow) | Warning semantic color (yellow in Catppuccin). |
--color-danger | from theme (--ctp-latte-red / --ctp-mocha-red) | Danger semantic color (red in Catppuccin). |
--color-light | from theme (--ctp-latte-surface1 / --ctp-mocha-rosewater) | Light semantic color (lighter contrast tone). |
--color-medium | from theme (--ctp-latte-overlay0 / --ctp-mocha-overlay2) | Medium semantic color (mid-tone). |
--color-dark | from theme (--ctp-latte-text / --ctp-mocha-subtext1) | Dark semantic color (high contrast text). |
--shadow | none on TUIX, elevated on rubik | Elevation shadow for raised surfaces. |
Ver Theming para la convención completa.