Code block
Read-only code with syntax highlighting, a copy button and a height cap. The code is static, or read from a route.
CodeBlock shows read-only code in the code editor's look: a head with the title, the language and a copy button, then the code highlighted. Long lines scroll sideways unless wrap is on, and maxLines caps the height. The code is written in the options, or read from a route that answers { code, language? }, such as a generated snippet, a stored configuration or a request body.
CodeBlockUsage
import { CodeBlock } from "@antelopejs/interface-dms/base/code-block";
static preset = CodeBlock({
title: "$media.presets.config",
language: "json",
maxLines: 12,
fetchUrl: "/api/media/presets/{{params.id}}/config",
});
Options
| Option | Type | Default | Description |
|---|---|---|---|
title Content | string | object | – | Head of the block. |
code Content | string | – | The code, verbatim. |
language Content | "json" | "javascript" | "typescript" | "html" | "sql" | "shell" | … | "text" | Highlighting language. |
copy Features | boolean | true | A button copies the code. |
wrap Appearance | boolean | – | Wraps long lines. |
maxLines Layout | number | – | Lines shown before the code scrolls. |
A route may answer a language under a common alias (js, ts, bash, sh or plain), and the block reads it. An unknown one is shown as plain text. While the first answer loads the block draws placeholder lines; a failure shows an error with a retry, and a 404 the not-found state.
Pitfalls
- In a custom component, show code with the
DmsCodeSnippetcomponent rather than a<pre>and a copy button of your own.
