Skip to content

Data display

CodeBlock

tsx
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.

tsx
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

tsx
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

tsx
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

codestringrequired

The code, as plain text (no highlighting, no HTML).

filenameReactNode

File name shown in the header.

languagestring

Language tag shown in the header, such as "tsx".

lineNumbersboolean

Line numbers in the gutter. Defaults to true.

highlightLinesnumber[]

1-based lines to highlight.

copyableboolean

Copy button in the header. Defaults to true.

copyLabelstring

Accessible name of the copy button. Defaults to "Copy code".

copiedLabelstring

Name and announcement after a copy. Defaults to "Copied".

iconReactNode

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

HookValues
.cherry-code-blockRoot element
data-line-numbersPresent when line numbers show
data-highlightedOn a highlighted .cherry-code-block__line
data-copiedOn 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.