All components (283)
Code block
Data · CodeBlock
What it is and when to use it
A code block with its header (the file name or whatever it is), line numbers if requested, highlighted lines and a copy button. It does not color the syntax, on purpose: that would be a whole highlighter as a dependency.
When to use it
- Documentation, integration examples, API responses, terminal commands.
titlefor what it is ("LineaDeCarrito.tsx", "Response", "Terminal") andlanguageas a small label next to it.syntaxcolours the code according tolanguage: js, ts, jsx, tsx, json, css, html, xml, bash, sql, python, yaml and diff, with own grammars that add no dependencies. With another language it stays uncoloured.numberLinesandhighlight(line numbers, starting at 1) when specific lines are going to be discussed.withoutCopyremoves the copy button.
themeputs the block in a colour theme:github-light,github-dark,dracula,nord,solarized-light,monokai, one registered withregisterCodeThemeor a wholeCodeThemeobject. The built-in ones reach 4.5:1 contrast on everything. Without a theme the block follows the page colours. Withsyntaxit also colours, on demand, go, rust, java, kotlin, swift, csharp, c, cpp, php, ruby, dockerfile, toml, ini, markdown and graphql: each one is downloaded the first time it is asked for (until then the code appears uncoloured) andregisterCodeLanguageregisters your own.
When not to
- A single piece of data to copy (an IBAN, a tracking code):
Copyable. - A single-use code that gets typed in:
CodeField.
Keyboard: Tab reaches the copy button and then the block itself, which is focusable: with focus inside, the arrow keys scroll it horizontally to read long lines.
Accessibility: the block is named by its title ("Code" if there is none) and the button by what it copies ("Copy LineaDeCarrito.tsx"). When copying, it says so with text in a role="status" ("Code copied"), and if the clipboard fails it says that too ("Could not copy") instead of pretending. The line numbers and the language label are not read.
Fine-tuning: CSS variables --amset-block-code-*: -font, -gap, -language, -line, -number, -padding, -radius, -title, and the colours of the code: -syntax-keyword, -syntax-string, -syntax-number, -syntax-comment, -syntax-function, -syntax-type, -syntax-tag, -syntax-attribute. The size changes the code font size, the padding and the gap of the line number.
What it looks like
npm install @amset/uinpx amset-ui theme<CodeBlock title="Install" language="bash" code={'npm install @amset/ui'} />Props
Import it with import { CodeBlock } from '@amset/ui'. Required props are marked.
| Prop | Type | What it does |
|---|---|---|
coderequired | string | — |
className | string | Merged with the library classes; it does not replace them. |
highlight | readonly number[] | Lines (starting at 1) that are highlighted. |
language | string | The label next to the title and, with `syntax`, the grammar used for colouring. |
numberLines | boolean | — |
size | "sm" | "md" | "lg" | — |
syntax | boolean | Colours the code according to `language`. Built in: js, ts, jsx, tsx, json, css, html, xml, bash, sql, python, yaml and diff. On demand (they are downloaded the first time a block asks for them, and until they arrive the code appears uncoloured): go, rust, java, kotlin, swift, csharp, c, cpp, php, ruby, dockerfile, toml, ini, markdown and graphql. And the ones registered with `registerCodeLanguage`. With another language, or without `language`, it stays uncoloured. |
theme | CodeThemeName | (string & Record<never, never>) | CodeTheme | The colour theme: the name of a built-in one (`github-light`, `github-dark`, `dracula`, `nord`, `solarized-light`, `monokai`), the name of one registered with `registerCodeTheme` or a whole `CodeTheme` object. Without a theme, the block follows the colours of the page (and changes with light and dark). A name that does not exist warns in the console and leaves the default theme. |
title | string | What appears in the header: a file name, "Response", "Terminal". |
withoutCopy | boolean | Removes the copy button. |
It also accepts the attributes of the element it renders: className, style, data-*, aria-* and its events.
In the test bench
Every state and variant, to try one at a time, on its page at storybook.amsetui.com:
