Components
Code
Read-only code block with line numbers, a highlighted line and lazy-loaded syntax highlighting for five languages.
import { Code } from "@openperf/deft";
<Code.Root code={source} language="typescript" />;When to use
| Need | Component |
|---|---|
| Read-only code, stack traces, queries, raw payloads | Code |
| A single key or a shortcut | Kbd |
| Attributes as keys and values | Key value |
Lines
code is one string; each newline starts a new line of the block.
| Behaviour | Details |
|---|---|
| Numbering | Starts at startLine, so an excerpt keeps the line numbers of its file. Numbers are not copied on selection. |
| Highlight | highlightLine uses the same numbering. The line is marked by color and weight only, so name it in the surrounding text. |
| Long lines | Never wrap: the block scrolls horizontally. |
| Height | Grows with the content. Cap it through className when the content can be long. |
Languages
language turns on syntax highlighting for bash, javascript, json, sql or typescript. Without it the code is plain text.
The highlighter and the grammar are loaded on demand, the first time a block with a language renders. Until they arrive the block shows plain text, and it stays that way if loading fails. Token colors follow the theme and the accent.
Commands
For shell commands, set language="bash", turn the line numbers off and put one command per line.
Copy button
Code renders no copy button. Put a Button next to the block and copy the same string with useCopy:
import { Button, Code, useCopy } from "@openperf/deft";
import { IconCopy } from "@tabler/icons-react";
const copy = useCopy();
<>
<Code.Root code={command} language="bash" lineNumbers={false} />
<Button.Root
onClick={() =>
copy(command, { title: "Command copied", description: "Paste it into your terminal." })
}
size="sm"
variant="ghost"
>
<IconCopy />
Copy
</Button.Root>
</>;API
Prop
Type