Code block
A block of code with a copy button.
SourceStyles source@monoline/react 0.5.0Markdown for AI toolsstable
- 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.
Line numbers
Props
CodeBlockProps
| Prop | Type | Default | Description |
|---|---|---|---|
code required | string | – | The text to show and to copy. Rendered as text with coloured spans, never as HTML. |
title | string | – | A file name (`button.tsx`) or label (`bash`) shown in the header. Also names the region and picks the language. |
language | Language | – | Syntax colouring: `tsx`, `ts`, `jsx`, `js`, `css`, `html`, `bash`, `json` or `text`. Default: guessed from `title`, else `text`. |
lineNumbers | boolean | – | Number each line. Use for longer listings, not one-line commands. |
hideCopy | boolean | – | Hide the copy button. |
Data Attributes
State is exposed through attributes. Style it with these selectors, not with class toggles.
| Attribute | Stylesheet |
|---|---|
[data-copied] | code-block.css |
[data-headless] | code-block.css |
[data-lines] | code-block.css |
CSS classes
| Class | Stylesheet |
|---|---|
.code-block | code-block.css |
.code-copy | code-block.css |
.code-head | code-block.css |
.code-line | code-block.css |
.code-title | code-block.css |
.tok-attr | code-block.css |
.tok-comment | code-block.css |
.tok-fn | code-block.css |
.tok-keyword | code-block.css |
.tok-number | code-block.css |
.tok-string | code-block.css |
.tok-tag | code-block.css |
.btn | 02-buttons.css |
.btn-block | 02-buttons.css |
.btn-danger | 02-buttons.css |
.btn-ghost | 02-buttons.css |
.btn-lg | 02-buttons.css |
.btn-line | 02-buttons.css |
.btn-sm | 02-buttons.css |
.btn-solid | 02-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.