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
| Prop | Type | Default | Description |
|---|---|---|---|
code | string | - | Displayed and copied code |
language | string | - | Language: shown, set as data-language and as a language-* class |
title | string | - | Title (file name), as the caption |
copyable | boolean | true | Copy button |
lineNumbers | boolean | false | Line numbers |
maxHeight | string | - | Maximum height; scrolls beyond |
copyLabel | string | 'Copy code' | Copy button name |
Slots
| Slot | Description |
|---|---|
default | Already 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.