Code Block
A block of code in a box, with an optional header for a filename and an action.
import { CodeBlock } from "antlitz";
API
| Prop | Type | Default |
|---|---|---|
| action | ReactNode | — |
| radius | "none" | "sm" | "md" | "lg" | "full" | none |
| size | "sm" | "md" | "lg" | md |
| title | ReactNode | — |
| variant | "outline" | "soft" | "plain" | outline |
className styles the box; every other prop reaches the pre.
Usage
export function Save() {
return <Button>Save</Button>;
}export function Basic() {
return <CodeBlock>{SOURCE}</CodeBlock>;
}CodeBlock renders the pre itself. Preserved whitespace is what makes a code
block a code block, so leaving that element to the caller would mean the
component breaks whenever someone forgets it.
Header
export function Save() {
return <Button>Save</Button>;
}export function Title() {
return <CodeBlock title="save.tsx">{SOURCE}</CodeBlock>;
}The header appears only when there is a title or an action.
Action
export function Save() {
return <Button>Save</Button>;
}export function Action() {
return (
<CodeBlock
title="save.tsx"
action={
<button type="button" className="link text-sm">
Copy
</button>
}
>
{SOURCE}
</CodeBlock>
);
}action is a slot at the end of the header, which is where a copy button
usually goes. Copying needs the clipboard and a piece of state, so the button
belongs to the app rather than the design system — this way CodeBlock renders
on the server and ships no JavaScript.
Variants
export function Save() {
return <Button>Save</Button>;
}export function Save() {
return <Button>Save</Button>;
}export function Save() {
return <Button>Save</Button>;
}export function Variants() {
return VARIANTS.map((variant) => (
<CodeBlock key={variant} variant={variant} title={variant}>
{SOURCE}
</CodeBlock>
));
}Sizes
export function Save() {
return <Button>Save</Button>;
}export function Save() {
return <Button>Save</Button>;
}export function Save() {
return <Button>Save</Button>;
}export function Sizes() {
return SIZES.map((size) => (
<CodeBlock key={size} size={size} title={size}>
{SOURCE}
</CodeBlock>
));
}Radius
export function Save() {
return <Button>Save</Button>;
}export function Save() {
return <Button>Save</Button>;
}export function Save() {
return <Button>Save</Button>;
}export function Save() {
return <Button>Save</Button>;
}export function Save() {
return <Button>Save</Button>;
}export function Radius() {
return RADII.map((radius) => (
<CodeBlock key={radius} radius={radius} title={radius}>
{SOURCE}
</CodeBlock>
));
}Highlighting
export function Save() { ... }export function Highlighted() {
return (
<CodeBlock title="save.tsx">
<code>
<span className="text-info-soft-foreground">export</span>
<span> function </span>
<span className="text-success-soft-foreground">Save</span>
<span>() { ... }</span>
</code>
</CodeBlock>
);
}CodeBlock takes code that is already highlighted and does not highlight anything itself. A syntax highlighter is a large dependency, its output is tied to a theme, and the good ones run at build time — none of which belongs in a component. Pass a string for plain code, or the markup a highlighter produced for coloured code.
Nested code elements are reset to inherit the block's font and size, so
highlighter output keeps its own colours without picking up inline Code
styling.