Skip to content

CodeBlock ​

Captioned code block, with a copy button and optional line numbers. It ships no syntax highlighting: plug in the one you prefer (Shiki, Prism) through the default slot.

Usage ​

Title, language and copy ​

vue
<SuCodeBlock :code="code" title="main.ts" language="ts" />

Line numbers ​

vue
<SuCodeBlock :code="code" language="ts" line-numbers />

Numbers are drawn with CSS: they are not selected, not copied and not read by screen readers.

Without caption ​

vue
<SuCodeBlock code="npm install @surgeui/vue" />

Highlighted code ​

The default slot replaces the display; code is still the copied text.

vue
<script setup>
import { codeToHtml } from 'shiki'

const html = await codeToHtml(code, { lang: 'ts', theme: 'github-light' })
</script>

<template>
  <SuCodeBlock :code="code" language="ts">
    <span v-html="html" />
  </SuCodeBlock>
</template>

API ​

Props ​

PropTypeDefaultDescription
codestring-Displayed and copied code
languagestring-Language: shown, set as data-language and as a language-* class
titlestring-Title (file name), as the caption
copyablebooleantrueCopy button
lineNumbersbooleanfalseLine numbers
maxHeightstring-Maximum height; scrolls beyond
copyLabelstring'Copy code'Copy button name

Slots ​

SlotDescription
defaultAlready highlighted code (replaces the display of code)

Accessibility ​

  • <figure> captioned by <figcaption> (title and language).
  • The block only becomes focusable (tabindex="0") when it scrolls: its content can then be read with the keyboard.
  • Copy button named "Copy code", result announced (CopyButton).
  • Code stays left to right in an RTL page.

Publié sous licence MIT.