Playground

Code block

A block of code with a copy button.

  • Built on native elements
  • The code is a pre/code element; the region has an accessible name.
  • The copy button is announced as "Copy code" and confirms with a polite live message.
  • Styles in one file: code-block.css

Import

import '@monoline/css';
import {CodeBlock} from '@monoline/react';

Not installed yet? Pick a route.

Copies the source and its styles into your project.

npx monoline add code-block

Usage

import {CodeBlock} from '@monoline/react';

const tsx = `import {Button} from '@monoline/react';

export function Save() {
  // One solid button per view.
  return <Button variant="solid" onClick={() => save(42)}>Save</Button>;
}`;

<CodeBlock title="save.tsx" code={tsx} />

Examples

With a file name

The copy button sits in the header; the language comes from the file name.

Without a title

The copy button floats in the top corner, inside the panel.

Line numbers

Props

CodeBlockProps

PropTypeDefaultDescription
code requiredstring–The text to show and to copy. Rendered as text with coloured spans, never as HTML.
titlestring–A file name (`button.tsx`) or label (`bash`) shown in the header. Also names the region and picks the language.
languageLanguage–Syntax colouring: `tsx`, `ts`, `jsx`, `js`, `css`, `html`, `bash`, `json` or `text`. Default: guessed from `title`, else `text`.
lineNumbersboolean–Number each line. Use for longer listings, not one-line commands.
hideCopyboolean–Hide the copy button.

Data Attributes

State is exposed through attributes. Style it with these selectors, not with class toggles.

AttributeStylesheet
[data-copied]code-block.css
[data-headless]code-block.css
[data-lines]code-block.css

CSS classes

ClassStylesheet
.code-blockcode-block.css
.code-copycode-block.css
.code-headcode-block.css
.code-linecode-block.css
.code-titlecode-block.css
.tok-attrcode-block.css
.tok-commentcode-block.css
.tok-fncode-block.css
.tok-keywordcode-block.css
.tok-numbercode-block.css
.tok-stringcode-block.css
.tok-tagcode-block.css
.btn02-buttons.css
.btn-block02-buttons.css
.btn-danger02-buttons.css
.btn-ghost02-buttons.css
.btn-lg02-buttons.css
.btn-line02-buttons.css
.btn-sm02-buttons.css
.btn-solid02-buttons.css

Customization

Style it with the kit's tokens in your own CSS, loaded after @monoline/css. Prefer the attributes above over extra classes. See Theming for every token.

app.css
.code-block {
  border-color: var(--accent);
}
.code-block[data-copied] {
  background: var(--wash);
}

Try the props

Change a prop and the component above updates; the code below is what you would write. To edit the code itself, use the Playground.

app.tsx
const total = items.reduce((sum, i) => sum + i.price, 0);
console.log(total);
example.tsx
<CodeBlock title="app.tsx" language="tsx" code="const total = items.reduce((sum, i) => sum + i.price, 0);
console.log(total);" />

Accessibility

  • The code is a pre/code element; the region has an accessible name.
  • The copy button is announced as "Copy code" and confirms with a polite live message.