Skip to main content
  1. Docs
  2. Components
  3. Data display

Code block

Code to read and copy: a command, a snippet, a JSON event or XML metadata, coloured, with one Copy button.

Import
@qeetrix/react/experimental
Status
ExperimentalSince 0.0.6
Renders
<pre>Server-rendered
Tested
WCAG 2.2 AAaxe on every pull request

01

Import

code-block.tsx
// Experimental: from the package's experimental entry, until it's stable.
import { CodeBlock } from "@qeetrix/react/experimental";

02

Props

And every native attribute of the element it renders.

PropTypeDefaultWhat it does
codestringrequiredThe code, as it should be copied.
language"shell" | "javascript" | "typescript" | "tsx" | "json" | "xml" | "html" | "css" | "kotlin" | "swift" | "text""text"How to colour it; text has no colours.
titleReactNodethe languageThe header's label.
lineNumbersbooleanmore than one lineNumber the lines; drawn by CSS, so copying never picks them up.
wrapbooleanfalseWrap long lines under their own start, instead of scrolling sideways.
messagesobjectthe provider's"Copy code", the region's name.

03

Parts

Each part carries a data-slot, for tests and placement.

data-slotElementWhat it is
code-block<div>The block.
code-block-header<div>The label and Copy.

04

On the server

Server-safe: the code is highlighted where it renders; only Copy hydrates.

install.tsx
import { CodeBlock } from "@qeetrix/react/experimental";

<CodeBlock language="shell" code="bun add @qeetrix/react @qeetrix/tokens" />

05

Without React

highlightHtml: the same colours as HTML, for a static page's build.

build.ts
import { highlightHtml } from "@qeetrix/react/experimental";

const html = `<pre><code>${highlightHtml(source, "css")}</code></pre>`;
Next — Code blockAccessibility