Skip to content

CodeBlock ​

A terminal-style code block with syntax-friendly display, line numbers, a copy button, and horizontal scroll for long lines.

CodeBlock

Design lines ​

ts
1const x = 1;
vue
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.

typescript
1const x = 1;
typescript
1function greet(name: string) {2  return `Hello ${name}`;3}
bash
docker compose up -d
vue
1<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.

javascript
1console.log("hi")
bash
1npm run dev
vue
1<CodeBlock code='console.log("hi")' language="javascript" />2<CodeBlock code="npm run dev" language="bash" />

Color (color) ​

Three color values for the border accent.

ts
1const x = 1;
ts
1const x = 1;
ts
1const x = 1;
vue
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.

ts
1const x = 1;
ts
1const x = 1;
vue
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.

ts
1line 12line 23line 3
ts
line 1line 2line 3
vue
1<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.

vue
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.

ts
1const x = 1;
ts
1const x = 1;
vue
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.

TypeDefaultRequired
string''❌

language ​

Language label shown as a tag in the header.

TypeDefaultRequired
string''❌

color ​

Border accent color of the code block.

TypeDefaultRequired
'primary' | 'secondary' | 'tertiary' | 'success' | 'warning' | 'danger' | 'light' | 'medium' | 'dark'DEFAULT_COLOR❌

border ​

Toggle the outer border. Defaults to true.

TypeDefaultRequired
booleantrue❌

showLines ​

Toggle line numbers on the left gutter.

TypeDefaultRequired
booleantrue❌

maxHeight ​

Max height before the block becomes scrollable.

TypeDefaultRequired
string'none'❌

formatter ​

Optional function to format code before syntax highlighting. Useful for auto-formatting with Prettier.

TypeDefaultRequired
(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).

TypeDefaultRequired
booleantrue❌

design ​

Design line (or inherit from data-design).

TypeDefaultRequired
'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 prettier

Supported languages ​

Language tags accepted by language, each mapped to a Prettier parser.

Language (language)Prettier parserRequired pluginBuilt-in
javascript / js / jsx / mjs / cjsbabel—✅
typescript / ts / tsx / mts / ctstypescript—✅
json / jsonc / json5json / jsonc / json5—✅
css / scss / lesscss / scss / less—✅
html / vue / angularhtml—✅
markdown / md / mdxmarkdown—✅
yaml / ymlyaml—✅
graphql / gqlgraphql—✅
flowflow—✅
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-rust

The composable automatically detects installed plugins. If a parser is unavailable, it returns the original code unformatted.

Slots ​

SlotDescription
defaultAlternative to code prop

CSS Custom Properties ​

TokenDefaultDescription
--font-mono"JetBrains Mono", ui-monospace, monospaceMonospace font stack for code-like content (key labels, log lines, chips).
--accentfrom theme (--ctp-latte-mauve / --ctp-mocha-mauve)Accent color, mapped from the active semantic color.
--bgfrom theme (--ctp-latte-base / --ctp-mocha-base)Base background of the surface.
--bg-panelfrom theme (--ctp-latte-surface1 / --ctp-mocha-surface1)Background of panel-like raised surfaces (cards, accordions).
--borderfrom theme (--ctp-latte-surface0 / --ctp-mocha-surface0)Outline and divider color for surfaces.
--text-mutedfrom theme (--ctp-latte-subtext1 / --ctp-mocha-subtext1)Secondary text for hints, annotations and disabled state.
--color-primaryfrom theme (--ctp-latte-mauve / --ctp-mocha-mauve)Primary semantic color (mauve in Catppuccin).
--color-secondaryfrom theme (--ctp-latte-lavender / --ctp-mocha-lavender)Secondary semantic color (lavender in Catppuccin).
--color-tertiaryfrom theme (--ctp-latte-blue / --ctp-mocha-blue)Tertiary semantic color (blue in Catppuccin).
--color-successfrom theme (--ctp-latte-green / --ctp-mocha-green)Success semantic color (green in Catppuccin).
--color-warningfrom theme (--ctp-latte-yellow / --ctp-mocha-yellow)Warning semantic color (yellow in Catppuccin).
--color-dangerfrom theme (--ctp-latte-red / --ctp-mocha-red)Danger semantic color (red in Catppuccin).
--color-lightfrom theme (--ctp-latte-surface1 / --ctp-mocha-rosewater)Light semantic color (lighter contrast tone).
--color-mediumfrom theme (--ctp-latte-overlay0 / --ctp-mocha-overlay2)Medium semantic color (mid-tone).
--color-darkfrom theme (--ctp-latte-text / --ctp-mocha-subtext1)Dark semantic color (high contrast text).
--shadownone on TUIX, elevated on rubikElevation shadow for raised surfaces.

Ver Theming para la convención completa.