Code Block

A syntax-highlighted code block with a built-in theme switcher and one-click copy.

The CodeBlock component renders syntax-highlighted code using the project's built-in code editor, with a color-mode theme switcher and a copy-to-clipboard button built right in.

Example

Learn how to use the Nuxt Charts CLI to get premium components.

Props

PropTypeDefaultDescription
codestringThe source code to display.
languagestring'text'Language for syntax highlighting (e.g. vue, typescript, bash).
filenamestringundefinedFilename shown in the header. Falls back to language.
showLineNumbersbooleanfalseDisplay line numbers.
darkThemestring'github-dark'Shiki theme used in dark color mode.
lightThemestring'github-light'Shiki theme used in light color mode.
maxHeightstring | numberundefinedMax height of the code area (e.g. 400 or '50vh'). Scrollable when exceeded.
loadingbooleanfalseShows a loading spinner instead of code.

Theme Support

By default the component automatically switches between github-light and github-dark based on the user's color mode. Override per-instance with theme, or set separate darkTheme / lightTheme props:

<template>
  <!-- Fixed theme -->
  <CodeBlock theme="nord" language="typescript" :code="tsCode" />

  <!-- Per color mode -->
  <CodeBlock dark-theme="one-dark-pro" light-theme="github-light" language="vue" :code="code" />
</template>

Implementation

The CodeBlock component is optimized for Nuxt projects using Shiki, but it also supports standard Vue projects via the lowlight engine.

<template>
  <!-- Uses Shiki (default) -->
  <CodeBlock 
    :code="code" 
    language="vue" 
  />
</template>

Requirements

Nuxt Projects

This component requires the nuxt-shiki module. Add it to nuxt.config.ts:

nuxt.config.ts
export default defineNuxtConfig({
  modules: ['nuxt-shiki'],
  shiki: {
    bundledThemes: ['github-dark', 'github-light'],
    bundledLangs: ['vue', 'typescript', 'javascript', 'css', 'json', 'bash'],
  }
})

Standard Vue Projects

If you are using this in a non-Nuxt environment, ensure you have the following dependencies installed:

pnpm add lowlight hast-util-to-html highlight.js