- Docs
- Components
- 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
// 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.
| Prop | Type | Default | What it does |
|---|---|---|---|
code | string | required | The 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. |
title | ReactNode | the language | The header's label. |
lineNumbers | boolean | more than one line | Number the lines; drawn by CSS, so copying never picks them up. |
wrap | boolean | false | Wrap long lines under their own start, instead of scrolling sideways. |
messages | object | the provider's | "Copy code", the region's name. |
03
Parts
Each part carries a data-slot, for tests and placement.
| data-slot | Element | What 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.
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.
import { highlightHtml } from "@qeetrix/react/experimental";
const html = `<pre><code>${highlightHtml(source, "css")}</code></pre>`;