CodeBlock
Highlighted code with a language label and copy button
Usage
CodeBlock is imported from its own subpath and needs the optional shiki peer.
npm install shikiimport {CodeBlock} from "@peakui-pro/react/code-block";Anatomy
import {CodeBlock} from "@peakui-pro/react/code-block";
export default () => (
<CodeBlock>
{/* Optional: a label at the start, actions at the end. */}
<CodeBlock.Header>
<span>typescript</span>
<CodeBlock.CopyButton code={code} />
</CodeBlock.Header>
{/* Plain text on the server and the first render; colours arrive after mount. */}
<CodeBlock.Code code={code} language="typescript" />
</CodeBlock>
);Examples
Languages
language takes any id or alias the highlighter bundles; anything else renders as plain text.
Without Header
Long Lines
Streaming
Pass the growing string; earlier colours stay while the new tail is highlighted.
Themes
Customization
Tailwind CSS
<CodeBlock className="[--code-block-radius:0.5rem] [--code-block-font-size:0.75rem]">
<CodeBlock.Code code={code} language="typescript" />
</CodeBlock>Global CSS
@layer components {
.code-block {
--code-block-bg: var(--color-surface-tertiary);
--code-block-padding: 1rem;
}
}Styling Reference
PeakUI follows the BEM methodology, so variants and states are plain class names you can target.
CSS Classes
The CodeBlock component uses these CSS classes (view source styles):
Base Classes
.code-block- Root panel
Element Classes
.code-block__header- Label and actions row.code-block__copy-button- Copy button, beside the core.button.code-block__code- Code region wrapper.code-block__pre- The scrolling<pre>.code-block__line- One source line.code-block__token- One highlighted token
Slots
[data-slot="code-block"][data-slot="code-block-header"][data-slot="code-block-copy-button"][data-slot="code-block-code"][data-slot="code-block-pre"][data-slot="code-block-line"][data-slot="code-block-token"]
CSS Variables
| Variable | Purpose |
|---|---|
--code-block-bg | Panel background; defaults to var(--color-surface-secondary) |
--code-block-fg | Text colour of plain code; defaults to var(--color-surface-secondary-foreground) |
--code-block-radius | Corner radius; defaults to var(--radius-2xl) |
--code-block-padding | Inset of the code; defaults to 0.75rem |
--code-block-font-size | Code font size; defaults to 0.8125rem |
--code-block-header-gap | Space between header items; defaults to 0.5rem |
--code-block-token-max-lightness | Highest OKLCH lightness of a token in a light scheme, for contrast; defaults to 0.52 |
--code-block-token-min-lightness | Lowest OKLCH lightness of a token in a dark scheme, for contrast; defaults to 0.72 |
Interactive States
- Highlighted:
[data-highlighted="true"]on.code-block__preonce colours are applied - Copied:
[data-copied="true"]on the copy button for two seconds after a copy - Focus:
:focus-visibleon.code-block__pre, the keyboard stop that scrolls long lines - Forced colors:
@media (forced-colors: active)draws the panel's edge as aCanvasTextborder and leaves token colours to the system
API Reference
CodeBlock
| Prop | Type | Default | Description |
|---|---|---|---|
children | ReactNode | - | Header and Code |
className | string | - | Additional classes |
render | DOMRenderFunction<'div', undefined> | - | Replaces the rendered <div> with your own element |
...rest | ComponentProps<'div'> | - | Native <div> attributes |
CodeBlock.Header
| Prop | Type | Default | Description |
|---|---|---|---|
children | ReactNode | - | A label and actions |
className | string | - | Additional classes |
render | DOMRenderFunction<'div', undefined> | - | Replaces the rendered <div> with your own element |
...rest | ComponentProps<'div'> | - | Native <div> attributes |
CodeBlock.Code
| Prop | Type | Default | Description |
|---|---|---|---|
code | string | - | The code to render |
language | string | 'plaintext' | Highlighter language id or alias; an unknown one renders as plain text |
theme | string | {light: string; dark: string} | {light: 'github-light', dark: 'github-dark'} | Highlighter theme for both colour schemes, or one per scheme; the scheme is the theme's color-scheme |
className | string | - | Additional classes |
render | DOMRenderFunction<'div', undefined> | - | Replaces the rendered <div> with your own element |
...rest | Omit<ComponentProps<'div'>, 'children'> | - | Native <div> attributes |
CodeBlock.CopyButton
| Prop | Type | Default | Description |
|---|---|---|---|
code | string | - | Text written to the clipboard |
aria-label | string | 'Copy code', then 'Copied' | Accessible label; yours replaces both |
children | ReactNode | copy glyph, then a check | Replaces the glyph |
variant | 'primary' | 'secondary' | 'tertiary' | 'outline' | 'ghost' | 'danger' | 'danger-soft' | 'ghost' | Core button style |
size | 'sm' | 'md' | 'lg' | 'sm' | Core button size |
onPress | (e: PressEvent) => void | - | Called after the copy starts; a refused clipboard changes nothing |
className | string | ((values: ButtonRenderProps) => string) | - | Additional classes |
render | DOMRenderFunction<'button', ButtonRenderProps> | - | Replaces the rendered element; receives the props with data-slot set |
...rest | ButtonProps | - | Every other core Button prop |