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)

PropDescription
:codeSimple mode: the source string shown in the block.
:languageSimple-mode id passed to `highlight` (default "html"). Not a highlighter by itself.
:copyableShow a copy button in the header (default true).
:html / :css / :js / :shell / :vue / :jsonChrome mode: one tab per non-empty string. Presence of any tab prop selects chrome over simple mode.
:default-openChrome mode: expand the block on load (default false).
:toggle-labelChrome mode: collapsed-row label (default "View Code").
:collapsibleChrome mode: show the toggle and start from `defaultOpen` (default true).
:animatedChrome mode: smooth height expansion on expand/collapse using Fibonacci timing (default true).
:highlightOptional `(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

ClassDescription
.vd-code-snippetRoot. Simple mode is a <figure>; chrome mode is a <div>.
.vd-code-snippet-simple / .vd-code-snippet-singleSimple-mode modifiers. The header is copy-only (no tabs).
.vd-code-snippet-toggleChrome “View Code” row. Hidden when collapsible is false.
.vd-code-snippet-toggle-iconChevron inside the toggle; rotated when expanded.
.vd-code-snippet-contentChrome region for the header and panes. Shown when data-visible is true.
.vd-code-snippet-headerTop row: language tabs (chrome) and the copy button (both modes).
.vd-code-snippet-tabsTablist wrapping one .vd-code-snippet-tab per language.
.vd-code-snippet-tabA language tab. .is-active marks the visible pane.
.vd-code-snippet-copyCopy-to-clipboard control, always in the header (top-right).
.vd-code-snippet-bodyChrome container for the code panes.
.vd-code-snippet-paneChrome <pre> per language (data-lang). .is-active shows it.
.vd-code-snippet-preSimple-mode <pre> wrapping the code.