MazCodeHighlight
MazCodeHighlight displays source code with syntax highlighting powered by shiki. It auto-detects dark/light mode, ships with a copy-to-clipboard button, a language badge, and supports lazy loading.
Before use it, you should install the MazUi plugin in your project, follow instructions in Getting Started
Installation requirement
Install shiki in your project to enable highlighting:
pnpm add shikinpm install shikiyarn add shikiIf shiki is not installed, the component falls back to a plain <pre><code> block.
shiki is loaded on the first highlight, with its JavaScript regex engine: no WebAssembly is downloaded, only the grammar of each language and the themes used by the page. The engine relies on the v flag of regular expressions (Chrome 112, Safari 17, Firefox 116 and later).
Basic usage
const greeting = 'Hello, maz-ui!'<template>
<MazCodeHighlight code="const greeting = 'Hello, maz-ui!'" language="ts" />
</template>
<script lang="ts" setup>
import MazCodeHighlight from 'maz-ui/components/MazCodeHighlight'
</script>With slot
The default slot text is used as source code when the code prop is not provided. This is useful for plain text blocks.
npm install maz-ui<template>
<MazCodeHighlight language="bash">npm install maz-ui</MazCodeHighlight>
</template>
<script lang="ts" setup>
import MazCodeHighlight from 'maz-ui/components/MazCodeHighlight'
</script>Languages
Pass any valid shiki language identifier to the language prop. The chosen language is also displayed as a small badge in the top-right corner (the badge fades out on hover when the copy button appears).
<script lang="ts" setup>
import { ref } from 'vue'
const count = ref(0)
</script>
<template>
<button @click="count++">Count: {{ count }}</button>
</template><template>
<MazCodeHighlight language="vue" :code="vueExample" />
</template>
<script lang="ts" setup>
import MazCodeHighlight from 'maz-ui/components/MazCodeHighlight'
const vueExample = `<script lang="ts" setup>
import { ref } from 'vue'
const count = ref(0)
<\/script>
<template>
<button @click="count++">Count: {{ count }}</button>
</template>`
</script>Custom theme
The block uses github-light in light mode and tokyo-night in dark mode. Change them with light-theme and dark-theme (any shiki built-in theme), or force one theme whatever the color mode with theme.
console.log('one-light or github-dark-default')console.log('dracula theme')<template>
<!-- One theme per color mode -->
<MazCodeHighlight
code="console.log('one-light or github-dark-default')"
language="js"
light-theme="one-light"
dark-theme="github-dark-default"
/>
<!-- The same theme in both modes -->
<MazCodeHighlight code="console.log('dracula theme')" language="js" theme="dracula" />
</template>
<script lang="ts" setup>
import MazCodeHighlight from 'maz-ui/components/MazCodeHighlight'
</script>Color mode
By default (color-mode="auto") the block follows the color mode of your app. The mode is resolved in this order:
- the
color-modeprop, ordefaults.MazCodeHighlight.colorModeof the MazUi plugin, when it is notauto - the closest
.darkancestor (including<html>), observed for changes - the color mode of
@maz-ui/themeswhen the theme plugin is installed (known at render time, so it is consistent with SSR and Nuxt) - the
darkclass of<html>
The resolved mode picks light-theme or dark-theme; the theme prop (shiki theme name) always wins over both.
Force the color mode
Set color-mode to light or dark to force the mode of a single block. The dark or light class is added on the root element, so the language badge, the copy button and the tooltip follow the forced mode.
const mode = 'dark'const mode = 'light'<template>
<MazCodeHighlight code="const mode = 'dark'" language="ts" color-mode="dark" />
<MazCodeHighlight code="const mode = 'light'" language="ts" color-mode="light" />
</template>Force the color mode for the whole app
Set colorMode in the defaults of the MazUi plugin (or mazUi.defaults of the Nuxt module) to force the mode of every code block, e.g. dark blocks for logs and terminal output in a light app. A color-mode prop on a block still wins.
app.use(MazUi, {
theme: { preset: mazUiPreset },
defaults: {
MazCodeHighlight: { colorMode: 'dark' },
},
})Dark ancestor
A .dark class on any ancestor is enough, without any prop:
<template>
<div class="dark">
<MazCodeHighlight code="const dark = true" language="ts" />
</div>
</template>Nuxt and SSR
With @maz-ui/nuxt (or the MazUi plugin / MazUiProvider), the block reads the color mode from useTheme(), so the server and the client render the same theme. Without a theme plugin, the component falls back to the dark class of <html> on the client and renders the light theme on the server.
Copy to clipboard
A copy button is displayed in the top-right corner on hover (or when focused via keyboard). Clicking it writes the resolved code to the clipboard and shows a check icon for ~1.5s.
Disable the copy button
Set :copyable="false" to hide the button entirely.
const noCopyButton = true<template>
<MazCodeHighlight code="const noCopyButton = true" language="ts" :copyable="false" />
</template>Copy a different value than the displayed code
Use copyValue when the displayed source differs from what should be copied — for example to strip a leading $ prompt when rendering a shell command. This is the mechanism MazWindowMockup uses internally to keep the terminal prompt out of the clipboard.
$ pnpm add maz-ui<template>
<MazCodeHighlight
code="$ pnpm add maz-ui"
copy-value="pnpm add maz-ui"
language="bash"
/>
</template>Disable rounded corners
Set :rounded="false" to render the highlighted block with squared corners — useful when embedding inside another container that already has its own border-radius (e.g. inside MazWindowMockup).
// squared corners<template>
<MazCodeHighlight code="// squared corners" language="ts" :rounded="false" />
</template>Sizes
size sets the size of the code text and of the padding around it: mini, xs, sm, md (default), lg or xl. Small sizes fit dense content like logs or terminal output.
const size = 'mini'const size = 'xs'const size = 'sm'const size = 'md'const size = 'lg'const size = 'xl'<template>
<MazCodeHighlight code="const size = 'mini'" language="ts" size="mini" />
<MazCodeHighlight code="const size = 'xs'" language="ts" size="xs" />
<MazCodeHighlight code="const size = 'sm'" language="ts" size="sm" />
<MazCodeHighlight code="const size = 'md'" language="ts" />
<MazCodeHighlight code="const size = 'lg'" language="ts" size="lg" />
<MazCodeHighlight code="const size = 'xl'" language="ts" size="xl" />
</template>Long code: max height and line wrap
max-height (any CSS length) caps the height of the block: longer code scrolls inside it. wrap breaks long lines instead of scrolling horizontally, and hide-language removes the language badge. Once the user scrolls up in a long block, a button scrolls it back to its last line (with or without follow).
2026-11-13T09:20:00.000Z [build] INFO packages/core/src/module-0.ts compiled in 40ms, output written to dist/module-0.js
2026-11-13T09:21:00.000Z [build] INFO packages/core/src/module-1.ts compiled in 47ms, output written to dist/module-1.js
2026-11-13T09:22:00.000Z [build] INFO packages/core/src/module-2.ts compiled in 54ms, output written to dist/module-2.js
2026-11-13T09:23:00.000Z [build] WARN packages/core/src/module-3.ts compiled in 61ms, output written to dist/module-3.js
2026-11-13T09:24:00.000Z [build] INFO packages/core/src/module-4.ts compiled in 68ms, output written to dist/module-4.js
2026-11-13T09:25:00.000Z [build] INFO packages/core/src/module-5.ts compiled in 75ms, output written to dist/module-5.js
2026-11-13T09:26:00.000Z [build] INFO packages/core/src/module-6.ts compiled in 82ms, output written to dist/module-6.js
2026-11-13T09:27:00.000Z [build] WARN packages/core/src/module-7.ts compiled in 89ms, output written to dist/module-7.js
2026-11-13T09:28:00.000Z [build] INFO packages/core/src/module-8.ts compiled in 96ms, output written to dist/module-8.js
2026-11-13T09:29:00.000Z [build] INFO packages/core/src/module-9.ts compiled in 103ms, output written to dist/module-9.js
2026-11-13T09:30:00.000Z [build] INFO packages/core/src/module-10.ts compiled in 110ms, output written to dist/module-10.js
2026-11-13T09:31:00.000Z [build] WARN packages/core/src/module-11.ts compiled in 117ms, output written to dist/module-11.js<template>
<MazCodeHighlight
:code="longLog"
language="log"
size="xs"
max-height="8rem"
wrap
hide-language
/>
</template>Follow streamed code (logs)
For code that grows while it is displayed (logs, build or terminal output), set follow with a max-height:
- the block stays scrolled to its last line as code is appended;
- scrolling up stops following, scrolling back to the bottom follows again;
- appended lines are highlighted alone, from the grammar state of the previous ones, instead of the whole code again: the cost of an update depends on the new lines, not on the size of the log.
The code must grow by appending (the new code starts with the previous one); any other change highlights it again from the start. The exposed scrollToBottom() method scrolls to the last line and follows again, for a "Follow" button for example.
<template>
<MazCodeHighlight
ref="log"
:code="lines.join('\n')"
language="log"
size="xs"
max-height="10rem"
follow
hide-language
/>
<MazBtn size="sm" @click="log?.scrollToBottom()">Follow</MazBtn>
</template>
<script lang="ts" setup>
import { ref, useTemplateRef } from 'vue'
import MazBtn from 'maz-ui/components/MazBtn'
import MazCodeHighlight from 'maz-ui/components/MazCodeHighlight'
const log = useTemplateRef('log')
// Push the lines of your process as they arrive
const lines = ref<string[]>([])
</script>Defaults for the whole app
Set the light and dark themes, the color mode, the size or the display props once for every code block with the defaults of the MazUi plugin (or the mazUi.defaults of the Nuxt module). A prop set on an instance still wins.
app.use(MazUi, {
theme: { preset: mazUiPreset },
defaults: {
MazCodeHighlight: {
lightTheme: 'one-light',
darkTheme: 'github-dark-default',
size: 'sm',
},
},
})To keep every code block dark whatever the color mode of the app (logs, terminal output), force the color mode too:
app.use(MazUi, {
theme: { preset: mazUiPreset },
defaults: {
MazCodeHighlight: { colorMode: 'dark', darkTheme: 'github-dark-default' },
},
})Globalizable props: lightTheme, darkTheme, theme (one theme in both modes), colorMode, size (also read from defaults.global.size), rounded, copyable, wrap and hideLanguage.
Custom translations
The labels and tooltips of the copy button and of the scroll-to-bottom button come from the @maz-ui/translations package (codeHighlight.copyToClipboard, codeHighlight.copiedToClipboard, codeHighlight.scrollToBottom). You can override any key per-instance with the translations prop without touching the global locale:
const hello = 'world'<template>
<MazCodeHighlight
code="const hello = 'world'"
language="ts"
:translations="{
copyToClipboard: 'Copier le code',
copiedToClipboard: 'Copié !',
}"
/>
</template>For app-wide translations, set up the maz-ui translations plugin and the component will pick up your locale automatically.
Props
| Name | Description | Type | Required | Default | Possible values |
|---|---|---|---|---|---|
| code | Source code to highlight | string | No | - | - |
| language | Language identifier for syntax highlighting (e.g. 'vue', 'ts', 'bash') | string | No | "text" | - |
| theme | Shiki theme of the block whatever the color mode: takes priority over lightTheme and darkTheme. | string | No | - | - |
| light-theme | Shiki theme used in light mode. Can be set for the whole app with defaults.MazCodeHighlight.lightTheme of the MazUi plugin. | string | No | 'github-light' | - |
| dark-theme | Shiki theme used in dark mode. Can be set for the whole app with defaults.MazCodeHighlight.darkTheme of the MazUi plugin. | string | No | 'tokyo-night' | - |
| color-mode | Color mode of the block. - auto: follows the closest .dark ancestor, then the @maz-ui/themes color mode when the theme plugin is installed, then the dark class of <html> - light / dark: forces the mode of this block, whatever the app color mode | "auto" | "light" | "dark" | No | 'auto' | 'auto' | 'light' | 'dark' |
| rounded | Apply rounded corners on the highlighted code block. | boolean | No | true | - |
| copyable | Show the copy-to-clipboard button on hover. | boolean | No | true | - |
| copy-value | Value copied to the clipboard. Falls back to the resolved code when omitted. Useful when the displayed code differs from what should be copied (e.g. terminal prompt prefix). | string | No | - | - |
| translations | Custom translations for the component. Override either key independently. | { copyToClipboard?: string | undefined; copiedToClipboard?: string | undefined; scrollToBottom?: string | undefined; } | No | - | - |
| size | Size of the code text (and of the padding around it). Can be set for the whole app with defaults.MazCodeHighlight.size or defaults.global.size of the MazUi plugin. | "xs" | "sm" | "md" | "lg" | "xl" | "mini" | No | 'md' | 'xs' | 'sm' | 'md' | 'lg' | 'xl' | 'mini' |
| max-height | Maximum height of the block (any CSS length, e.g. 20rem): longer code scrolls inside the block. | string | No | - | - |
| follow | Follow streamed code (logs, build output): the block stays scrolled to the bottom as code is appended, until the user scrolls up; scrolling back to the bottom follows again. Appended lines are highlighted alone, not the whole code again. Use it with maxHeight. | boolean | No | false | - |
| wrap | Wrap long lines instead of scrolling horizontally. | boolean | No | false | - |
| hide-language | Hide the language badge. | boolean | No | false | - |
Expose
scrollToBottom
Scrolls the block to its last line and follows the next ones again (with
follow).