Typography
CodeBlock & CopyButton
Copyable code in a titled, keyboard-scrollable block — bring your own highlighter — and a CopyButton for anything people need to paste elsewhere: links, keys, IDs.
import { CodeBlock, CopyButton } from '@reactloom/ui';Examples
Highlighted
highlight receives the code and returns nodes; ReactLoom ships no highlighter.
import { Button } from '@reactloom/ui';
export function Save({ onSave }: { onSave: () => void }) {
return <Button onClick={onSave}>Save changes</Button>;
}import { CodeBlock } from '@reactloom/ui';
import { highlight } from 'sugar-high';
const code = `import { Button } from '@reactloom/ui';
export function Save({ onSave }: { onSave: () => void }) {
return <Button onClick={onSave}>Save changes</Button>;
}`;
export default function Example() {
return (
<div style={{ width: '100%', maxWidth: 640 }}>
<CodeBlock
code={code}
language="tsx"
title="save.tsx"
// Any highlighter works. sugar-high escapes the source and returns HTML of <span> tokens,
// so injecting it is safe; never inject HTML from a highlighter that does not escape.
highlight={(text) => <span dangerouslySetInnerHTML={{ __html: highlight(text) }} />}
/>
</div>
);
}Long code
maxLines collapses behind “Show all N lines”.
export default {
setting1: true,
setting2: 10,
setting3: 20,
setting4: true,
setting5: 40,
setting6: 50,
setting7: true,
setting8: 70,
setting9: 80,
setting10: true,
setting11: 100,
setting12: 110,
setting13: true,
setting14: 130,
setting15: 140,
setting16: true,
setting17: 160,
setting18: 170,
setting19: true,
setting20: 190,
setting21: 200,
setting22: true,
setting23: 220,
setting24: 230,
setting25: true,
setting26: 250,
setting27: 260,
setting28: true,
setting29: 280,
setting30: 290,
};import { CodeBlock } from '@reactloom/ui';
const config = Array.from({ length: 30 }, (_, i) => ` setting${i + 1}: ${i % 3 === 0 ? 'true' : i * 10},`).join('\n');
export default function Example() {
return (
<div style={{ width: '100%', maxWidth: 640 }}>
<CodeBlock code={`export default {\n${config}\n};`} language="ts" title="reactloom.config.ts" maxLines={8} />
</div>
);
}Plain text
Without highlight, code is plain text.
pnpm add @reactloom/ui @reactloom/themeimport { CodeBlock } from '@reactloom/ui';
export default function Example() {
return (
<div style={{ width: '100%', maxWidth: 640 }}>
<CodeBlock code="pnpm add @reactloom/ui @reactloom/theme" language="bash" title="Terminal" />
</div>
);
}CopyButton
With text, or icon-only with a stable name.
import { CopyButton, HStack, Input } from '@reactloom/ui';
export default function Example() {
return (
<HStack gap="2" align="end" wrap style={{ width: '100%', maxWidth: 520 }}>
<div style={{ flex: 1 }}>
<Input label="Invite link" readOnly value="https://acme.test/invite/8f2c" />
</div>
<CopyButton value="https://acme.test/invite/8f2c" label="Copy link" />
<CopyButton iconOnly value="INV-2041" label="Copy invoice number" />
</HStack>
);
}API reference
CodeBlock props
| Prop | Type | Default |
|---|---|---|
coderequiredAlso what Copy copies. | string | — |
languageLabel and data-language. | string | — |
titleCaption; names the figure and the code region. | ReactNode | — |
highlightYour highlighter. | (code, language) => ReactNode | — |
copyable | boolean | true |
maxLinesCollapse beyond this many lines. | number | — |
wrapWrap instead of scrolling. | boolean | false |
labelsTranslate the built-in text. | { copy, copied, expand, collapse, region } | — |
CopyButton props
| Prop | Type | Default |
|---|---|---|
valuerequiredComputed at click time when a function. | string | () => string | Promise<string> | — |
labelText, or the accessible name when iconOnly. | string | 'Copy' |
copiedLabel · errorLabelShown and announced. | string | 'Copied' · 'Couldn’t copy' |
iconOnly | boolean | false |
timeoutms before returning to idle. | number | 2000 |
onCopy · onError | (text) / (error) => void | — |
variant · sizeAs Button. | | — |
Customization
Extracted from the component’s source on every build, so it matches the code. How the layers fit together is explained in Customization.
CodeBlock
State attributes
Target these in your CSS to style states without depending on class names.
| Attribute | Values |
|---|---|
[data-collapsed] | present when on |
[data-wrap] | present when on |
Theme tokens it reads
--pl-color-bg-subtle --pl-color-bg-surface --pl-color-border-default --pl-color-fg-default --pl-color-fg-muted --pl-focus-ring-color --pl-focus-ring-width --pl-font-size-sm --pl-font-size-xs --pl-radius-lg
CopyButton
Renders Button, so its hooks apply too.
CSS variables
| Variable | Default |
|---|---|
--pl-button-radius | var(--pl-radius-md |
State attributes
Target these in your CSS to style states without depending on class names.
| Attribute | Values |
|---|---|
[data-disabled] | present when on |
[data-full-width] | present when on |
[data-loading] | present when on |
[data-radius] | none · sm · md · lg · full |
[data-size] | sm · md · lg |
[data-variant] | primary · secondary · tertiary · outline · ghost · destructive · link |
Theme tokens it reads
--pl-color-accent-fg --pl-color-accent-solid --pl-color-accent-solid-active --pl-color-accent-solid-hover --pl-color-accent-subtle --pl-color-accent-subtle-hover --pl-color-bg-muted --pl-color-bg-subtle --pl-color-bg-surface --pl-color-border-default --pl-color-border-strong --pl-color-danger-on-solid --pl-color-danger-solid --pl-color-danger-solid-hover --pl-color-fg-default --pl-color-fg-disabled --pl-color-fg-link --pl-color-fg-on-accent --pl-control-height-lg --pl-control-height-md --pl-control-height-sm --pl-focus-ring-color --pl-focus-ring-offset --pl-focus-ring-width --pl-font-size-lg --pl-font-size-md --pl-font-size-sm --pl-radius-full --pl-radius-lg --pl-radius-md --pl-radius-none --pl-radius-sm --pl-shadow-xs
Accessibility
- The result is announced politely (“Copied” or “Couldn’t copy”); the button keeps its name, so screen readers do not hear a renamed control.
- Copying falls back to the legacy command where the Clipboard API is unavailable, and reports failure instead of doing nothing.
- The code is focusable and named (a group, not a landmark — pages often have many code blocks), so wide code can be scrolled with the keyboard; it stays left-to-right on right-to-left pages.
- The figure is named by its caption; the expand button names the size (“Show all 30 lines”) and exposes aria-expanded.
| Key | Action |
|---|---|
| Tab | The copy button, then the code region |
| Arrow keys | Scroll the focused code region |
| Enter / Space | Copy; expand or collapse |
Usage guidelines
Do
- Offer a copy button wherever people will paste the value elsewhere (commands, keys, links).
- Keep titles meaningful — a file name or “Terminal”.
Don’t
- Don’t inject HTML from a highlighter unless it escapes the source.
- Don’t use CodeBlock for a word inside a sentence — use Code.