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.

Purpose

A code block shows source code in documentation, a guide or a settings page, with a button that copies it. It is not an M3 component, so it comes from @windmorph/react-pro; this site shows every sample in one. Use it for code people read or copy; for a word of code inside a sentence, use inline code instead.

Highlight the code at build time and pass the markup as html, with the raw text as code, which the copy button copies. The component ships no highlighter, so nothing heavy reaches the browser.

Behaviour

  • The copy button copies code, turns into a check mark named "Copied" for two seconds, and announces it.
  • Long lines scroll sideways inside the block, never the page. The code always reads left to right, also on a right-to-left page.
  • title shows in the header (a file name, usually) and names the code. Without a title and with copyable={false} there is no header.

When not to use

For a word of code inside a sentence, use inline code.

See also

  • Props table: A component's props as a table: name, type, default and description, for documentation. Not an M3 component (@windmorph/react-pro).