Code Snippet
VdCodeSnippet is a copyable source block with two modes. Pass :code and :language for a simple figure. Pass any tab prop (html, css, js, shell, vue, json) for collapsible multi-tab chrome — the same “View Code” control used throughout these docs via DocCodeSnippet. The component does not color tokens unless you pass highlight. These pages pass the cbun tokenizer (highlightCode).
Simple mode (live VdCodeSnippet)
Figure + header Copy (top-right). Color comes from :highlight (cbun), not from the component.
const greet = (name: string) => `hello, ${name}`; Same source without highlight — plain text.
const greet = (name: string) => `hello, ${name}`;Collapsible (single language)
The “View Code” toggle stays visible. Expand to reveal HTML and Copy in the header.
Multiple language tabs
More than one tab prop yields HTML / CSS / JavaScript tabs. Copy sends the active tab’s raw source.
Expanded by default
default-open renders the chrome open on load.
import { VdCodeSnippet } from "@vanduo-oss/vd3";
const code = "const x = 1;";
// <VdCodeSnippet :code="code" language="js" :highlight="highlightCode" />API Reference
Usage
<script setup lang="ts">
import { VdCodeSnippet } from "@vanduo-oss/vd3";
import { highlightCode } from "@vanduo-oss/vd3/highlight";
const code = "const x = 1;";
</script>
<template>
<VdCodeSnippet :code="code" language="js" :highlight="highlightCode" />
</template>Component API (Vue 3)
| Prop | Description |
|---|---|
:code | Simple mode: the source string shown in the block. |
:language | Simple-mode id passed to `highlight` (default "html"). Not a highlighter by itself. |
:copyable | Show a copy button in the header (default true). |
:html / :css / :js / :shell / :vue / :json | Chrome mode: one tab per non-empty string. Presence of any tab prop selects chrome over simple mode. |
:default-open | Chrome mode: expand the block on load (default false). |
:toggle-label | Chrome mode: collapsed-row label (default "View Code"). |
:collapsible | Chrome mode: show the toggle and start from `defaultOpen` (default true). |
:animated | Chrome mode: smooth height expansion on expand/collapse using Fibonacci timing (default true). |
:highlight | Optional `(code, language) => escaped HTML`. These docs pass `@vanduo-oss/vd3/highlight`; without it the snippet is plain text. Copy always uses the raw source. |
CSS Classes
| Class | Description |
|---|---|
.vd-code-snippet | Root. Simple mode is a <figure>; chrome mode is a <div>. |
.vd-code-snippet-simple / .vd-code-snippet-single | Simple-mode modifiers. The header is copy-only (no tabs). |
.vd-code-snippet-toggle | Chrome “View Code” row. Hidden when collapsible is false. |
.vd-code-snippet-toggle-icon | Chevron inside the toggle; rotated when expanded. |
.vd-code-snippet-content | Chrome region for the header and panes. Shown when data-visible is true. |
.vd-code-snippet-header | Top row: language tabs (chrome) and the copy button (both modes). |
.vd-code-snippet-tabs | Tablist wrapping one .vd-code-snippet-tab per language. |
.vd-code-snippet-tab | A language tab. .is-active marks the visible pane. |
.vd-code-snippet-copy | Copy-to-clipboard control, always in the header (top-right). |
.vd-code-snippet-body | Chrome container for the code panes. |
.vd-code-snippet-pane | Chrome <pre> per language (data-lang). .is-active shows it. |
.vd-code-snippet-pre | Simple-mode <pre> wrapping the code. |