CodeBlock
import { CodeBlock } from "@cherry-design-system/react";
const source = `import { Button } from "@cherry-design-system/react";
export function Save() {
return <Button>Save</Button>;
}`;
export function Example() {
return <CodeBlock filename="save.tsx" language="tsx" code={source} />;
}CodeBlock shows a multi-line code listing in a frame, with a header (icon, file name, language, copy button) and a line-number gutter. Line numbers come from CSS, so selecting and copying the text never picks them up. For a one-line command, use Snippet; for code inside a sentence, use InlineCode.
Examples
Highlighted lines
highlightLines takes 1-based line numbers.
import { CodeBlock } from "@cherry-design-system/react";
const source = `const a = 1;
const b = 2;
const sum = a + b;`;
export function Highlighted() {
return <CodeBlock language="ts" highlightLines={[3]} code={source} />;
}Without line numbers or copy button
import { CodeBlock } from "@cherry-design-system/react";
export function Plain() {
return (
<CodeBlock
lineNumbers={false}
copyable={false}
code={"NODE_ENV=production\nPORT=3000"}
/>
);
}Custom header icon and labels
import { CodeBlock } from "@cherry-design-system/react";
import { FileJson } from "lucide-react";
export function Config() {
return (
<CodeBlock
icon={<FileJson />}
filename="package.json"
language="json"
copyLabel="Copy file"
copiedLabel="File copied"
code={'{\n "name": "my-app"\n}'}
/>
);
}CodeBlock has no syntax highlighter. code is rendered as plain text, so never pass HTML in it.
Properties
The code, as plain text (no highlighting, no HTML).
File name shown in the header.
Language tag shown in the header, such as "tsx".
Line numbers in the gutter. Defaults to true.
1-based lines to highlight.
Copy button in the header. Defaults to true.
Accessible name of the copy button. Defaults to "Copy code".
Name and announcement after a copy. Defaults to "Copied".
Header icon. Defaults to a terminal glyph; null hides it.
CodeBlock renders a <div>, forwards its ref to it and passes every other <div> attribute through (it takes no children). It is a client component.
Styling
| Hook | Values |
|---|---|
.cherry-code-block | Root element |
data-line-numbers | Present when line numbers show |
data-highlighted | On a highlighted .cherry-code-block__line |
data-copied | On the copy button after a copy |
| Parts | .cherry-code-block__header, __icon, __filename, __language, __copy, __glyph, __pre, __code, __line |
The frame reads --cherry-bg-subtle, --cherry-border-default and --cherry-radius-lg; code uses --cherry-font-mono with the mono-sm text tokens; highlighted lines read --cherry-accent-subtle and --cherry-accent-default.
Accessibility
- The copy button is labelled (
copyLabel) and a successful copy is announced through a polite live region (copiedLabel). - Code wider than the block can be focused and scrolled with the keyboard, and the whole block shows the focus ring.