Primitives

CodeBlock

Highlighted code with a copy button, themed for light and dark.

Basic

Pass the source as code and a Shiki language id as lang. The copy button copies the rendered text, so Windows line endings (\r\n) are copied as \n.

export function greet(name: string): string {
  return `Hello, ${name}!`;
}
basic
---
import CodeBlock from '@alfadormx/blocks/primitives/CodeBlock.astro';

// Must match `BASIC` in tests/browser/codeblock.spec.ts.
const code = 'export function greet(name: string): string {\n  return `Hello, ${name}!`;\n}\n';
---

<CodeBlock lang="ts" code={code} />

Title

A title renders as a filename caption, with the copy button beside it.

src/pages/index.astro
---
const title = "Home";
---

<h1>{title}</h1>
title
---
import CodeBlock from '@alfadormx/blocks/primitives/CodeBlock.astro';

const code = '---\nconst title = "Home";\n---\n\n<h1>{title}</h1>\n';
---

<CodeBlock lang="astro" title="src/pages/index.astro" code={code} />

No copy button

Set copyButton to false to hide the button.

pnpm add astro
no-copy
---
import CodeBlock from '@alfadormx/blocks/primitives/CodeBlock.astro';
---

<CodeBlock lang="sh" code="pnpm add astro" copyButton={false} />

Long lines

Long lines scroll inside the block, never the page. The code area is keyboard-focusable, so the arrow keys scroll it.

export const longLine = ['alpha', 'bravo', 'charlie', 'delta', 'echo', 'foxtrot', 'golf', 'hotel', 'india', 'juliett', 'kilo', 'lima', 'mike', 'november', 'oscar', 'papa', 'quebec', 'romeo', 'sierra', 'tango'];
long-lines
---
import CodeBlock from '@alfadormx/blocks/primitives/CodeBlock.astro';

const code =
  "export const longLine = ['alpha', 'bravo', 'charlie', 'delta', 'echo', 'foxtrot', 'golf', 'hotel', 'india', 'juliett', 'kilo', 'lima', 'mike', 'november', 'oscar', 'papa', 'quebec', 'romeo', 'sierra', 'tango'];\n";
---

<CodeBlock lang="ts" code={code} />

Token colours

The block uses GitHub’s light and dark themes and follows the site theme toggle. MDX fenced code blocks use the same themes.

// Keywords, strings, numbers, types, calls, a regex and a < comparison.
interface Point {
  x: number;
  y: number;
}

const limit = 42;
const pattern = /^[a-z]+$/i;

export async function check(point: Point, label = 'origin'): Promise<boolean> {
  const ok = point.x < limit && pattern.test(label);
  console.log(`checked ${label}`, ok);
  return ok;
}
tokens
---
import CodeBlock from '@alfadormx/blocks/primitives/CodeBlock.astro';

const code =
  "// Keywords, strings, numbers, types, calls, a regex and a < comparison.\ninterface Point {\n  x: number;\n  y: number;\n}\n\nconst limit = 42;\nconst pattern = /^[a-z]+$/i;\n\nexport async function check(point: Point, label = 'origin'): Promise<boolean> {\n  const ok = point.x < limit && pattern.test(label);\n  console.log(`checked ${label}`, ok);\n  return ok;\n}\n";
---

<CodeBlock lang="ts" code={code} />

Escaping

The source renders as text, so markup is shown, not interpreted.

<p>a < b</p>
escape
---
import CodeBlock from '@alfadormx/blocks/primitives/CodeBlock.astro';
---

<CodeBlock lang="html" code="<p>a < b</p>" />

Several files

For several files side by side, compose Tabs with one CodeBlock per panel.

API Reference

PropTypeDefaultDescription
codestring—Source to highlight and copy, rendered as text.
langstring'plaintext'Shiki language id; unknown ids fall back to plain text.
titlestring—Filename-style caption shown above the code.
copyButtonOmit<ButtonProps, 'href'> | false—Copy button merged over the default icon button; `false` hides it (default: rendered).
copiedMessagestring'Copied'Text announced to screen readers after a successful copy.
classstring''Classes added to the root `figure`.