Skip to content
ComponentsHeadings & codeCodeBlock

CodeBlock

Hero example

curl https://api.mattermode.com/v1/entities

When to use

Reach for <CodeBlock> whenever rendering literal code or structured request / response payloads. The 5-token syntax palette (<Tok kind>) handles key / string / number / punct / comment without pulling in a syntax highlighter. The translucent-white surface is canonical — never dark slate. For long-form prose code, bare-<pre> mode is correct. For endpoint demos and API recipes, pass a header to render the card-style surface that V1 <CodeCard> carried.

Don't reach for CodeBlock for inline code spans — use Markdown backticks, which fall through to the styled <code> recipe. Don't use it for terminal output where you want a dark-slate aesthetic — Matter's code surface is intentionally light, even in dark theme.

Do — compose the header with V2 primitives: <EndpointBadge method="POST" path="/v1/entities" /> plus a request-time <span>.
Don't — bake a dark theme. The surface is white-translucent by design.

Anatomy

Bare mode renders a single <pre class="code">. Card mode (header set) wraps in <figure class="code-card"> with a 44px .code-card-head row and the .code body underneath — geometry mirrors V1 .matter-code-card exactly so the consumer-migration step (action 9) lands as a pixel-parity swap.

The <Tok> companion component colorizes spans inside the code body:

kindToken typeColor
kkey / identifierPeach
sstringTeal
nnumberPurple
ppunctuation40% black
ccomment45% black, italic

Header slot — V1 CodeCard replacement

// V1 (today)
<CodeCard verb="POST" path="/v1/entities" time="142ms">

</CodeCard>

// V2 (action 9 migration)
<CodeBlock
  header={
    <>
      <EndpointBadge method="POST" path="/v1/entities" />
      <span className="code-time">142ms</span>
    </>
  }
>

</CodeBlock>

The header slot accepts any ReactNode, so consumers can compose it however they need — verb chip, branch indicator, file path, response status, etc. The outer element switches to <figure> when a header is present so consumers may include a <figcaption> for accessible description.

Props

PropTypeDefaultDescription
headerReact.ReactNodeOptional header slot — composes any V2 primitive set.
classNamestring
styleReact.CSSProperties
childrenReact.ReactNode

Also accepts Omit<React.HTMLAttributes<HTMLPreElement>, "children">.

States

CodeBlock is single-state. Header mode and bare mode are configuration, not state.

Density

curl https://api.mattermode.com/v1/entities
curl https://api.mattermode.com/v1/entities

In compact, the code body padding drops from 16 to 12px. Font size and line-height stay constant — code legibility is the floor.

Themes

curl https://api.mattermode.com/v1/entities
curl https://api.mattermode.com/v1/entities
curl https://api.mattermode.com/v1/entities

In dark, the translucent-white surface inverts to a translucent ink (still light against the dark backdrop) and the Tok colors shift to their dark-theme variants. In forced-colors, Tok colors collapse to system foreground; the surface collapses to Canvas with ButtonBorder.

Tokens consumed

No tokens match.
colors5 tokensv2.3.0
#14110D
matter
src ↗
#C04C20
matter
src ↗
#2F7A6E
matter
src ↗
#6E5BBF
matter
src ↗
#18181b
brand
src ↗

Surface reads --code-surface. Header band reads --code-head. Tok colors resolve through --tok-k / --tok-s / --tok-n / --tok-p / --tok-c.

Accessibility

  • Keyboard interactions. None — the <pre> is a static surface. Triple-click selects the block.
  • ARIA roles and properties. <pre> carries no role override. <figure> (when header is set) inherits semantic figure role; consumer may add a <figcaption>.
  • Focus order. N/A.
  • Screen-reader expectations. Code is announced verbatim. Tok spans inherit accessibility — colour is purely visual.
  • Reduced motion. N/A.
  • Forced colors. Tok colors → system foreground; surface → Canvas with ButtonBorder.
  • WIG rules. <pre> for monospace code (semantic-element rule). <figure> + optional <figcaption> for captioned code (semantic-element rule). Comment Tok meets AA — verify when surface is tinted.

Do / Don't

Do — use <Tok kind="k"> for keys, <Tok kind="s"> for strings, etc. The 5-token palette covers most syntactic needs without a full highlighter.
Don't — pull in a syntax highlighter library. CodeBlock's palette is the canonical Matter look; highlighters drift.
Do — wrap card-style code in a <figure> via the header prop. The outer element switches automatically.
Don't — nest CodeBlock inside CodeBlock. Use one with a header instead.
Do — pair with a Copy-to-clipboard button at the consumer layer for long examples. CodeBlock doesn't ship one — keep the surface minimal.
Don't — set inline styles on <pre> to override the surface. Use the style prop on the wrapper if a height cap is needed.

Recipes

This component appears in:

Code example

import { CodeBlock, Tok, EndpointBadge } from "@matter/components";

export function CreateEntityExample() {
  return (
    <CodeBlock
      header={
        <>
          <EndpointBadge method="POST" path="/v1/entities" />
          <span className="code-time">142ms</span>
        </>
      }
    >
      <Tok kind="c">{`// Create a Delaware C-Corp`}</Tok>{"\n"}
      <Tok kind="k">curl</Tok> <Tok kind="s">"https://api.mattermode.com/v1/entities"</Tok>{"\n"}
      {"  -H "}<Tok kind="s">"Authorization: Bearer sk_test_…"</Tok>
    </CodeBlock>
  );
}

Source

packages/components/src/CodeBlock

On this page