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}!`;
}---
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.
---
const title = "Home";
---
<h1>{title}</h1>---
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---
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'];---
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;
}---
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>---
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
| Prop | Type | Default | Description |
|---|---|---|---|
| code | string | — | Source to highlight and copy, rendered as text. |
| lang | string | 'plaintext' | Shiki language id; unknown ids fall back to plain text. |
| title | string | — | Filename-style caption shown above the code. |
| copyButton | Omit<ButtonProps, 'href'> | false | — | Copy button merged over the default icon button; `false` hides it (default: rendered). |
| copiedMessage | string | 'Copied' | Text announced to screen readers after a successful copy. |
| class | string | '' | Classes added to the root `figure`. |