Code block
ProSource 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
| Key | Action | Where |
|---|---|---|
| Tab | Moves to the copy button, then to the code itself, which takes focus so it can be scrolled. | |
| Enter or Space | Copies the code.On the copy button | On the copy button |
| Arrow Left or Arrow Right | Scrolls a long line sideways.On the code | On the code |
Screen reader
- Role
- The code is a
regionholding a<pre>; the copy button is abutton. - Accessible name
- The region is named by
title, or bylabels.regionwhen there is none ("tsx code", or "Code" withoutlang). 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
secondaryoutline. - 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
titlewhen the code is a file, so the region has a meaningful name; without one, passlang. - 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
labelsto translate the button names and the region name.