Code Block

Provides syntax highlighting, line numbers, and copy to clipboard functionality for code blocks.

The CodeBlock component provides syntax highlighting, line numbers, and copy to clipboard functionality for code blocks. It's fully composable, allowing you to customize the header, actions, and content.

Install using CLI

AI Elements Vue
shadcn-vue CLI
npx ai-elements-vue@latest add code-block

Install Manually

Copy and paste the following files into the same folder.

CodeBlock.vue
CodeBlockContainer.vue
CodeBlockContent.vue
CodeBlockHeader.vue
CodeBlockTitle.vue
CodeBlockFilename.vue
CodeBlockActions.vue
CodeBlockCopyButton.vue
CodeBlockLanguageSelector.vue
CodeBlockLanguageSelectorTrigger.vue
CodeBlockLanguageSelectorValue.vue
CodeBlockLanguageSelectorContent.vue
CodeBlockLanguageSelectorItem.vue
context.ts
utils.ts
index.ts
<script setup lang="ts">
import type { BundledLanguage } from 'shiki'
import type { HTMLAttributes } from 'vue'
import { computed, provide } from 'vue'
import CodeBlockContainer from './CodeBlockContainer.vue'
import CodeBlockContent from './CodeBlockContent.vue'
import { CodeBlockKey } from './context'

const props = withDefaults(
  defineProps<{
    code: string
    language: BundledLanguage
    showLineNumbers?: boolean
    class?: HTMLAttributes['class']
  }>(),
  {
    showLineNumbers: false,
  },
)

provide(CodeBlockKey, {
  code: computed(() => props.code),
})
</script>

<template>
  <CodeBlockContainer :class="props.class" :language="props.language">
    <slot />
    <CodeBlockContent
      :code="props.code"
      :language="props.language"
      :show-line-numbers="props.showLineNumbers"
    />
  </CodeBlockContainer>
</template>

Usage

The CodeBlock is fully composable. Here's a basic example:

<script setup lang="ts">
import { FileIcon } from 'lucide-vue-next'
import {
  CodeBlock,
  CodeBlockActions,
  CodeBlockCopyButton,
  CodeBlockFilename,
  CodeBlockHeader,
  CodeBlockTitle,
} from '@/components/ai-elements/code-block'

const code = `console.log("Hello, World!")`
</script>

<template>
  <CodeBlock :code="code" language="typescript">
    <CodeBlockHeader>
      <CodeBlockTitle>
        <FileIcon :size="14" />
        <CodeBlockFilename>example.ts</CodeBlockFilename>
      </CodeBlockTitle>
      <CodeBlockActions>
        <CodeBlockCopyButton />
      </CodeBlockActions>
    </CodeBlockHeader>
  </CodeBlock>
</template>

Features

  • Syntax highlighting with Shiki
  • Line numbers (optional)
  • Copy to clipboard functionality
  • Automatic light/dark theme switching via CSS variables
  • Language selector for multi-language examples
  • Fully composable architecture
  • Accessible design

Examples

Dark Mode

To use the CodeBlock component in dark mode, wrap it in a div with the dark class.

Language Selector

Add a language selector to switch between different code implementations:

Props

<CodeBlock />

coderequiredstring
The code content to display.
languagerequiredBundledLanguage
The programming language for syntax highlighting.
showLineNumbersboolean
Whether to show line numbers.
classstring
Additional CSS classes to apply to the root container.

<CodeBlockHeader />

Container for the header row. Uses flexbox with justify-between.

classstring
Additional CSS classes.

<CodeBlockTitle />

Left-aligned container for icon and filename. Uses flexbox with gap-2.

classstring
Additional CSS classes.

<CodeBlockFilename />

Displays the filename in monospace font.

classstring
Additional CSS classes.

<CodeBlockActions />

Right-aligned container for action buttons. Uses flexbox with gap-2.

classstring
Additional CSS classes.

<CodeBlockCopyButton />

timeoutnumber
How long to show the copied state (ms).
classstring
Additional CSS classes to apply to the button.
@copy() => void
Callback fired after a successful copy.
@error(error: Error) => void
Callback fired if copying fails.

<CodeBlockLanguageSelector />

Wrapper for the language selector. Extends shadcn/ui Select.

v-modelstring
The currently selected language.

<CodeBlockLanguageSelectorTrigger />

Trigger button for the language selector dropdown. Pre-styled for code block header.

<CodeBlockLanguageSelectorValue />

Displays the selected language value.

<CodeBlockLanguageSelectorContent />

Dropdown content container. Defaults to align="end".

<CodeBlockLanguageSelectorItem />

Individual language option in the dropdown.

valuerequiredstring
The language value.

<CodeBlockContainer />

Low-level container component with performance optimizations (contentVisibility). Used internally by CodeBlock.

<CodeBlockContent />

Low-level component that handles syntax highlighting. Used internally by CodeBlock, but can be used directly for custom layouts.

coderequiredstring
The code content to display.
languagerequiredBundledLanguage
The programming language for syntax highlighting.
showLineNumbersboolean
Whether to show line numbers.