Skip to content

CodeBlock

Framed code with filename header and copy button.

codeBlock() renders a framed code figure with an optional filename header and a ghost copy button. The copy button is wired by the runtime ([data-cpd-copy]).

Filename header
src/lib/ui/tokens.ts
export const LAYOUT = {
  sidebarWidth: 256,
  tocWidth: 256,
  pageMaxWidth: 800,
} as const;
codeBlock({
filename: 'src/lib/ui/tokens.ts',
lang: 'ts',
code: 'export const LAYOUT = {\n sidebarWidth: 256,\n tocWidth: 256,\n pageMaxWidth: 800,\n} as const;\n',
});
Bare code
Bash
bun install
bun run dev
codeBlock({
lang: 'bash',
code: 'bun install\nbun run dev\n',
});

Pass tabs to render a multi-file snippet: the header shows one tab per file; the copy button copies the active tab.

Snippet with tabs
export function renderDrawer(options) {
  // ...
}
export function initDrawers(root) {
  // ...
}
codeBlock({
lang: 'ts',
tabs: [
{ label: 'drawer.ts', code: 'export function renderDrawer(options) {\n // ...\n}\n' },
{ label: 'runtime.ts', code: 'export function initDrawers(root) {\n // ...\n}\n' },
],
});
OptionTypeDefaultDescription
codestringCode text (required when tabs is omitted).
filenamestring''Filename shown in the header — also enables the copy button.
langstring''Language hint class (language-<lang>), no syntax highlighting.
tabsCodeBlockTab[]Multi-file snippet: { label, code } per file; header renders tabs and the copy button copies the active tab.
  • The builder escapes code — raw markup is never executed.
  • No syntax highlighting ships with the kit; the real docs pages use astro-expressive-code blocks instead (styled to match).
  • Copy uses navigator.clipboard with a fallback execCommand('copy').