Skip to content
VinUIComponentsCode Block

Code Block

Code with a file name, a copy button and optional line numbers and highlighted lines. Same markup and props in every style: switch the style in the header to see it change.

Basic

A file name, the language and a copy button. In HTML, add data-vin-copy to the button and include vinui.js.

vinui.config.js js
export default {
  style: "precision",
  accent: "#5b5bd6",
  mode: "system",
};
code-block-basic.html
<div class="vin-code-block" style="max-width: 520px">
  <div class="vin-code-block__header">
    <span class="vin-code-block__title">vinui.config.js</span>
    <span class="vin-code-block__lang">js</span>
    <button type="button" class="vin-btn vin-btn--ghost vin-btn--icon vin-btn--sm vin-code-block__copy" aria-label="Copy code" data-vin-copy><svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><rect x="9" y="9" width="12" height="12" rx="2"/><path d="M5 15V5a2 2 0 0 1 2-2h10"/></svg><svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M20 6 9 17l-5-5"/></svg></button>
  </div>
  <pre class="vin-code-block__pre" tabindex="0"><code>export default {
  style: "precision",
  accent: "#5b5bd6",
  mode: "system",
};</code></pre>
  <span class="vin-code-block__status" role="status"></span>
</div>

Line numbers and highlights

Numbered lines, two highlighted lines and pre-highlighted tokens. Long lines scroll sideways instead of wrapping.

save-button.tsx tsx
import { Button } from "@/components/vin/button";export function SaveButton({ saving }: { saving: boolean }) {  // Disabled while the request is in flight.  return <Button disabled={saving} onClick={() => save({ retries: 3, timeout: 8000 })}>Save</Button>;}
code-block-lines.html
<div class="vin-code-block vin-code-block--numbered" style="max-width: 520px">
  <div class="vin-code-block__header">
    <span class="vin-code-block__title">save-button.tsx</span>
    <span class="vin-code-block__lang">tsx</span>
    <button type="button" class="vin-btn vin-btn--ghost vin-btn--icon vin-btn--sm vin-code-block__copy" aria-label="Copy code" data-vin-copy><svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><rect x="9" y="9" width="12" height="12" rx="2"/><path d="M5 15V5a2 2 0 0 1 2-2h10"/></svg><svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M20 6 9 17l-5-5"/></svg></button>
  </div>
  <pre class="vin-code-block__pre" tabindex="0"><code><span class="vin-code-block__line"><span class="vin-code-block__keyword">import</span> { Button } <span class="vin-code-block__keyword">from</span> <span class="vin-code-block__string">"@/components/vin/button"</span>;</span><span class="vin-code-block__line"></span><span class="vin-code-block__line"><span class="vin-code-block__keyword">export function</span> SaveButton({ saving }: { saving: boolean }) {</span><span class="vin-code-block__line vin-code-block__line--highlight">  <span class="vin-code-block__comment">// Disabled while the request is in flight.</span></span><span class="vin-code-block__line vin-code-block__line--highlight">  <span class="vin-code-block__keyword">return</span> &lt;Button disabled={saving} onClick={() =&gt; save({ retries: <span class="vin-code-block__number">3</span>, timeout: <span class="vin-code-block__number">8000</span> })}&gt;Save&lt;/Button&gt;;</span><span class="vin-code-block__line">}</span></code></pre>
  <span class="vin-code-block__status" role="status"></span>
</div>

Without a header

A one-line command. The copy button sits in the corner.

npx vinui@latest add code-block
code-block-plain.html
<div class="vin-code-block" style="max-width: 520px">
  <button type="button" class="vin-btn vin-btn--ghost vin-btn--icon vin-btn--sm vin-code-block__copy" aria-label="Copy command" data-vin-copy><svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><rect x="9" y="9" width="12" height="12" rx="2"/><path d="M5 15V5a2 2 0 0 1 2-2h10"/></svg><svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M20 6 9 17l-5-5"/></svg></button>
  <pre class="vin-code-block__pre" tabindex="0"><code>npx vinui@latest add code-block</code></pre>
  <span class="vin-code-block__status" role="status"></span>
</div>

Installation

React, Vue and Svelte: add the component to your project with the CLI. It reads your framework from vin.json and also copies any component this one needs. Then import it:

Terminal
npx @vinish/ui add code-block
React
import { CodeBlock } from "@/components/vin/code-block";
Vue
import CodeBlock, { copyText } from "@/components/vin/code-block.vue";
Svelte
import CodeBlock, { copyText } from "$lib/components/vin/code-block.svelte";

The import paths assume the default folders: @/components/vin (React and Vue, with the usual @ → src alias) and $lib/components/vin (SvelteKit). Use your own if you chose another folder in init. How the Vue and Svelte versions pass values and content is in Frameworks.

Plain HTML: the classes come with the style's stylesheet. This component also needs vinui.js for its keyboard and pointer behaviour.

API

From spec/code-block.json, which every framework version is tested against.

PropValuesDefaultIn HTML
code
The code, as plain text. It is escaped for you.
string—the text inside pre.vin-code-block__pre > code
html
Code that is already highlighted (by Shiki, Prism or the token classes below). Used instead of code; trust only HTML you made. With lineNumbers or highlight, keep every token on one line.
string—your highlighted HTML inside the code element
filename
Shown in the header, with the copy button.
ReactNode—.vin-code-block__header > .vin-code-block__title
language
A short label in the header, such as "tsx" or "bash".
string—.vin-code-block__lang
lineNumbers
Number the lines. The numbers are drawn by CSS, so they are never copied or read out.
booleanfalsevin-code-block--numbered
highlight
Line numbers to highlight, starting at 1.
number[]—span.vin-code-block__line.vin-code-block__line--highlight
copyLabel
The copy button's name.
stringCopy codearia-label on button.vin-code-block__copy[data-vin-copy]
token classes
Optional colours for hand-written highlighting, drawn from the theme: keyword, string, number, comment.
class—span.vin-code-block__{keyword|string|number|comment}

Parts

React, Vue, SvelteHTML classes
<CodeBlock>.vin-code-block.vin-code-block--numbered.vin-code-block__header.vin-code-block__title.vin-code-block__lang.vin-code-block__copy.vin-code-block__pre.vin-code-block__line.vin-code-block__line--highlight.vin-code-block__status.vin-code-block__keyword.vin-code-block__string.vin-code-block__number.vin-code-block__comment

Accessibility

  • The copy button has a name ("Copy code") and a polite live region (role="status") says "Copied" when it works.
  • Line numbers are CSS counters with empty alternative text, so they are not copied with the code and screen readers skip them.
  • The code can be scrolled sideways with the keyboard: the pre has tabindex="0" and a focus ring.
  • Highlighted lines are a visual aid only; say in the text around the block why they matter.
  • Token colours come from the theme and meet 4.5:1 contrast in every style, light and dark.