Skip to content
EspañolEnglish
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.
  • title for what it is ("LineaDeCarrito.tsx", "Response", "Terminal") and language as a small label next to it.
  • syntax colours the code according to language: 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.
  • numberLines and highlight (line numbers, starting at 1) when specific lines are going to be discussed.
  • withoutCopy removes the copy button.
  • theme puts the block in a colour theme: github-light, github-dark, dracula, nord, solarized-light, monokai, one registered with registerCodeTheme or a whole CodeTheme object. The built-in ones reach 4.5:1 contrast on everything. Without a theme the block follows the page colours. With syntax it 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) and registerCodeLanguage registers 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

Install
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.

PropTypeWhat it does
coderequiredstring—
classNamestringMerged with the library classes; it does not replace them.
highlightreadonly number[]Lines (starting at 1) that are highlighted.
languagestringThe label next to the title and, with `syntax`, the grammar used for colouring.
numberLinesboolean—
size"sm" | "md" | "lg"—
syntaxbooleanColours 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.
themeCodeThemeName | (string & Record<never, never>) | CodeThemeThe 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.
titlestringWhat appears in the header: a file name, "Response", "Terminal".
withoutCopybooleanRemoves 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: