Skip to content
Calm motion: animations on this site only fade, with no movement

CodeBlock ​

Copy button, line highlight, tabs per file

A listing the reader will copy from: an install command, an example, a config in two files. For two versions of the same text, use Diff. For a short inline mention, plain <code> is enough.

Basic
ts
import { createApp } from "vue";
import { BlessButton } from "blessing-ui";
import "blessing-ui/tokens.css";

createApp(BlessButton).mount("#app");
installsh
pnpm add blessing-ui vue
  • Pass code (with lang and filename for the header) or files for tabs: [{ name, code, lang }]. Tabs switch with ← →, Home and End. v-model:active is the open one. Copy takes the file you are looking at.
  • lines="2,4-6" marks lines with a bar and a tint (1-based, and an array of numbers works too). line-numbers adds a gutter that is not read aloud and is left out when you select and copy.
  • Long lines scroll sideways. wrap wraps them instead. The scroll area is reachable by keyboard.
  • It does not color syntax, and ships no highlighter. Fill the #code="{ code, lang, lines }" slot with the output of Shiki, Prism or whatever you use. The header, tabs and copy button stay. lines there is [{ n, text, hl }] if you want to keep the line marks.
  • Copy uses the clipboard API and falls back to a hidden textarea. The button says "Copied" for two seconds and screen readers hear it. copy fires with the text, and :copy="false" removes the button.

Usage ​

ts
import { BlessCodeBlock } from "blessing-ui";

API ​

Props

NameTypeDefaultDescription
codestring—
langstring—
filenamestring—
filesBlessCodeFile[]—several files as tabs; wins over `code`
linesstring | number[]—lines to mark, e.g. `"2,4-6"` or `[2, 4, 5, 6]` (1-based)
lineNumbersboolean—
copybooleantrue
wrapboolean—wrap long lines instead of scrolling sideways
labelstring"Code"
labelsPartial<{ copy: string; copied: string; copiedLive: string; }>—
activenumber0

Events

NamePayloadDescription
copy[code: string]
update:active[value: number]

Slots

NameScopeDescription
code{ code: string; lang: string | undefined; lines: { n: number; text: string; hl: boolean; }[]; }

Released under the MIT License. Character names and artwork belong to their owners.