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.

Keyboard

Code block keyboard
KeyAction
TabMoves to the copy button, then to the code itself, which takes focus so it can be scrolled.
Enter or SpaceCopies the code.On the copy button
Arrow Left or Arrow RightScrolls a long line sideways.On the code

Screen reader

Role
The code is a region holding a <pre>; the copy button is a button.
Accessible name
The region is named by title, or by labels.region when there is none ("tsx code", or "Code" without lang). The copy button is named "Copy" (labels.copy), then "Copied" (labels.copied) for two seconds after a copy.
States announced
None.
  • A copy is announced ("Copied") through a status region that is in the page from the start, so screen readers hear it.
  • The copy button's icons are hidden; its name says what it does. Highlighting is only colour, so the code reads as plain text.

Focus

  • The code region takes focus with Tab (WCAG 2.1.1), so a keyboard can scroll a long line. It shows the M3 focus indicator inside its edges: a 3px secondary outline.
  • The copy button is the core's IconButton, with its focus ring and its tooltip, which shows its name.
  • Copying leaves focus on the copy button.

Touch target

The copy button is a small standard IconButton with a 48 × 48px touch target, in a 48px header. It stays at density 0 in a dense theme.

Motion

Nothing moves apart from the copy button's state layer and ripple, which follow IconButton: under reduced motion the ripple appears at full size without growing.

What you need to do

  • Give the block a title when the code is a file, so the region has a meaningful name; without one, pass lang.
  • Highlight with colours from the M3 roles (the block maps Shiki's CSS variables to them), so every theme and contrast level keeps the text readable.
  • Pass labels to translate the button names and the region name.