Skip to content

Code block

Pro

Source code for documentation, highlighted at build time, with a copy button and optional line numbers. Not an M3 component (@windmorph/react-pro).

import { CodeBlock } from '@windmorph/react-pro';

Part of Windmorph Pro, paid components on the free core, not on sale yet. See the Pro guide for what it includes and to register interest.

Props

CodeBlock 11 props

  • codetype stringRequired

    Raw source — copied by the copy button and used as a fallback when html is absent.

  • htmltype string

    Build-time Shiki markup rendered inside <pre><code>. Trusted (build-generated); when omitted, code renders as plain text.

  • langtype string

    Language label, exposed on the root as data-lang.

  • titletype string

    Optional title shown in the header, e.g. a file name. It also names the code region.

  • lineNumberstype booleandefault false

    Numbers each line (Shiki's .line spans) in the start margin.

  • copyabletype booleandefault true

    Shows a copy button in the header, which copies code.

  • reftype Ref<HTMLDivElement>

    Ref to the root element. Its other props (id, aria-*, data-*) go to it too.

Text

  • labelstype Partial<CodeBlockLabels>

    The words it shows and announces, merged over the English defaults. A LabelsProvider sets them for the whole app.

Also accepts the root element's HTML attributes (id, data-*, event handlers).

Spec status

Not part of Material Design 3: a Windmorph Pro component, built on M3's colour roles, type scale, shapes and motion. Its variants and configurations each link to a live example.

  • Highlighted at build time (html), with a title

    Built

    Use case · Highlighted

  • Line numbers (lineNumbers)

    Built

    Configuration · Line numbers

  • Plain text, without a title, and without the copy button (copyable)

    Built

    Configuration · Plain

M3 Expressive specs

Shape
rounded-corner-medium

Highlighting

  • html is trusted markup from your own build, rendered as it is inside <pre><code>: never pass text people typed. Without it, code shows as plain text.
  • It expects Shiki's CSS-variables theme (createCssVariablesTheme), whose token spans read --shiki-token-* variables. The block sets them to M3 colour roles (keywords primary, strings tertiary, comments on-surface-variant…), so one highlight follows every theme, mode and contrast level. Set a variable on the block (with className) to change a colour.
  • lineNumbers counts the highlighter's .line spans, so it needs html.

Styling

  • className and style go on the bordered root; classNames styles the header, title, pre, code and copy button.
  • It has no tokens of its own: it is surface-container with an outline-variant border and medium corners, and its root carries data-tokens="wm-code-block". The copy button is the core's standard IconButton, kept at density 0 in its 48px header.
  • labels holds every word it shows or announces: copy, copied and region, the code's name when there is no title.