PeakUIPro

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 shiki
import {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

VariablePurpose
--code-block-bgPanel background; defaults to var(--color-surface-secondary)
--code-block-fgText colour of plain code; defaults to var(--color-surface-secondary-foreground)
--code-block-radiusCorner radius; defaults to var(--radius-2xl)
--code-block-paddingInset of the code; defaults to 0.75rem
--code-block-font-sizeCode font size; defaults to 0.8125rem
--code-block-header-gapSpace between header items; defaults to 0.5rem
--code-block-token-max-lightnessHighest OKLCH lightness of a token in a light scheme, for contrast; defaults to 0.52
--code-block-token-min-lightnessLowest OKLCH lightness of a token in a dark scheme, for contrast; defaults to 0.72

Interactive States

  • Highlighted: [data-highlighted="true"] on .code-block__pre once colours are applied
  • Copied: [data-copied="true"] on the copy button for two seconds after a copy
  • Focus: :focus-visible on .code-block__pre, the keyboard stop that scrolls long lines
  • Forced colors: @media (forced-colors: active) draws the panel's edge as a CanvasText border and leaves token colours to the system

API Reference

CodeBlock

PropTypeDefaultDescription
childrenReactNode-Header and Code
classNamestring-Additional classes
renderDOMRenderFunction<'div', undefined>-Replaces the rendered <div> with your own element
...restComponentProps<'div'>-Native <div> attributes

CodeBlock.Header

PropTypeDefaultDescription
childrenReactNode-A label and actions
classNamestring-Additional classes
renderDOMRenderFunction<'div', undefined>-Replaces the rendered <div> with your own element
...restComponentProps<'div'>-Native <div> attributes

CodeBlock.Code

PropTypeDefaultDescription
codestring-The code to render
languagestring'plaintext'Highlighter language id or alias; an unknown one renders as plain text
themestring | {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
classNamestring-Additional classes
renderDOMRenderFunction<'div', undefined>-Replaces the rendered <div> with your own element
...restOmit<ComponentProps<'div'>, 'children'>-Native <div> attributes

CodeBlock.CopyButton

PropTypeDefaultDescription
codestring-Text written to the clipboard
aria-labelstring'Copy code', then 'Copied'Accessible label; yours replaces both
childrenReactNodecopy glyph, then a checkReplaces 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
classNamestring | ((values: ButtonRenderProps) => string)-Additional classes
renderDOMRenderFunction<'button', ButtonRenderProps>-Replaces the rendered element; receives the props with data-slot set
...restButtonProps-Every other core Button prop

On this page