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.
export default {
style: "precision",
accent: "#5b5bd6",
mode: "system",
};
<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>export default {
style: "precision",
accent: "#5b5bd6",
mode: "system",
};import { CodeBlock } from "@/components/vin/code-block";
const code = `export default {
style: "precision",
accent: "#5b5bd6",
mode: "system",
};`;
export function CodeBlockBasic() {
return <CodeBlock code={code} filename="vinui.config.js" language="js" style={{ maxWidth: 520 }} />;
}The Vue version renders the same markup as the HTML tab, so it looks and works the same.
<script setup lang="ts">
import CodeBlock from "@/components/vin/code-block.vue";
const code = `export default {
style: "precision",
accent: "#5b5bd6",
mode: "system",
};`;
</script>
<template>
<CodeBlock :code="code" filename="vinui.config.js" language="js" style="max-width: 520px" />
</template>The Svelte version renders the same markup as the HTML tab, so it looks and works the same.
<script lang="ts">
import CodeBlock from "$lib/components/vin/code-block.svelte";
const code = `export default {
style: "precision",
accent: "#5b5bd6",
mode: "system",
};`;
</script>
<CodeBlock {code} filename="vinui.config.js" language="js" style="max-width: 520px" />Line numbers and highlights
Numbered lines, two highlighted lines and pre-highlighted tokens. Long lines scroll sideways instead of wrapping.
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>;}
<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> <Button disabled={saving} onClick={() => save({ retries: <span class="vin-code-block__number">3</span>, timeout: <span class="vin-code-block__number">8000</span> })}>Save</Button>;</span><span class="vin-code-block__line">}</span></code></pre>
<span class="vin-code-block__status" role="status"></span>
</div>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>;}import { CodeBlock } from "@/components/vin/code-block";
// Already highlighted (by hand here; Shiki or Prism output works the same way).
const k = (s: string) => `<span class="vin-code-block__keyword">${s}</span>`;
const str = (s: string) => `<span class="vin-code-block__string">${s}</span>`;
const num = (s: string) => `<span class="vin-code-block__number">${s}</span>`;
const comment = (s: string) => `<span class="vin-code-block__comment">${s}</span>`;
const html = [
`${k("import")} { Button } ${k("from")} ${str('"@/components/vin/button"')};`,
``,
`${k("export function")} SaveButton({ saving }: { saving: boolean }) {`,
` ${comment("// Disabled while the request is in flight.")}`,
` ${k("return")} <Button disabled={saving} onClick={() => save({ retries: ${num("3")}, timeout: ${num("8000")} })}>Save</Button>;`,
`}`,
].join("\n");
export function CodeBlockLines() {
return <CodeBlock html={html} filename="save-button.tsx" language="tsx" lineNumbers highlight={[4, 5]} style={{ maxWidth: 520 }} />;
}The Vue version renders the same markup as the HTML tab, so it looks and works the same.
<script setup lang="ts">
import CodeBlock from "@/components/vin/code-block.vue";
// Already highlighted (by hand here; Shiki or Prism output works the same way).
const k = (s: string) => `<span class="vin-code-block__keyword">${s}</span>`;
const str = (s: string) => `<span class="vin-code-block__string">${s}</span>`;
const num = (s: string) => `<span class="vin-code-block__number">${s}</span>`;
const comment = (s: string) => `<span class="vin-code-block__comment">${s}</span>`;
const html = [
`${k("import")} { Button } ${k("from")} ${str('"@/components/vin/button"')};`,
``,
`${k("export function")} SaveButton({ saving }: { saving: boolean }) {`,
` ${comment("// Disabled while the request is in flight.")}`,
` ${k("return")} <Button disabled={saving} onClick={() => save({ retries: ${num("3")}, timeout: ${num("8000")} })}>Save</Button>;`,
`}`,
].join("\n");
</script>
<template>
<CodeBlock :html="html" filename="save-button.tsx" language="tsx" line-numbers :highlight="[4, 5]" style="max-width: 520px" />
</template>The Svelte version renders the same markup as the HTML tab, so it looks and works the same.
<script lang="ts">
import CodeBlock from "$lib/components/vin/code-block.svelte";
// Already highlighted (by hand here; Shiki or Prism output works the same way).
const k = (s: string) => `<span class="vin-code-block__keyword">${s}</span>`;
const str = (s: string) => `<span class="vin-code-block__string">${s}</span>`;
const num = (s: string) => `<span class="vin-code-block__number">${s}</span>`;
const comment = (s: string) => `<span class="vin-code-block__comment">${s}</span>`;
const html = [
`${k("import")} { Button } ${k("from")} ${str('"$lib/components/vin/button"')};`,
``,
`${k("export function")} SaveButton({ saving }: { saving: boolean }) {`,
` ${comment("// Disabled while the request is in flight.")}`,
` ${k("return")} <Button disabled={saving} onClick={() => save({ retries: ${num("3")}, timeout: ${num("8000")} })}>Save</Button>;`,
`}`,
].join("\n");
</script>
<CodeBlock {html} filename="save-button.tsx" language="tsx" lineNumbers highlight={[4, 5]} style="max-width: 520px" />Without a header
A one-line command. The copy button sits in the corner.
npx vinui@latest add code-block
<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>npx vinui@latest add code-blockimport { CodeBlock } from "@/components/vin/code-block";
export function CodeBlockPlain() {
return <CodeBlock code="npx vinui@latest add code-block" copyLabel="Copy command" style={{ maxWidth: 520 }} />;
}The Vue version renders the same markup as the HTML tab, so it looks and works the same.
<script setup lang="ts">
import CodeBlock from "@/components/vin/code-block.vue";
</script>
<template>
<CodeBlock code="npx vinui@latest add code-block" copy-label="Copy command" style="max-width: 520px" />
</template>The Svelte version renders the same markup as the HTML tab, so it looks and works the same.
<script lang="ts">
import CodeBlock from "$lib/components/vin/code-block.svelte";
</script>
<CodeBlock code="npx vinui@latest add code-block" copyLabel="Copy command" style="max-width: 520px" />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:
npx @vinish/ui add code-blockimport { CodeBlock } from "@/components/vin/code-block";import CodeBlock, { copyText } from "@/components/vin/code-block.vue";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.
| Prop | Values | Default | In HTML |
|---|---|---|---|
codeThe code, as plain text. It is escaped for you. | string | — | the text inside pre.vin-code-block__pre > code |
htmlCode 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 |
filenameShown in the header, with the copy button. | ReactNode | — | .vin-code-block__header > .vin-code-block__title |
languageA short label in the header, such as "tsx" or "bash". | string | — | .vin-code-block__lang |
lineNumbersNumber the lines. The numbers are drawn by CSS, so they are never copied or read out. | boolean | false | vin-code-block--numbered |
highlightLine numbers to highlight, starting at 1. | number[] | — | span.vin-code-block__line.vin-code-block__line--highlight |
copyLabelThe copy button's name. | string | Copy code | aria-label on button.vin-code-block__copy[data-vin-copy] |
token classesOptional 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, Svelte | HTML 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.
Customize
Saved in this browser. Style and accent are in the header.