Skip to main content

Code

Marks a short fragment of inline computer code, such as a function name, a variable, a CLI flag, or a key.

When to use

  • Mark code, a file path, an environment variable, or a key inside a sentence.
  • Set a technical term apart from the running text.
  • Prefer CodeBlock for code that spans several lines or needs syntax highlighting.
  • Prefer Kbd for a keyboard key or a shortcut.
  • Prefer TruncatedText for a long opaque value, such as a URL or an ID, that can overflow its row.

When not to use

  • For arbitrary monospace text that isn't code (use a plain monospace utility class).

Polymorphism

Code renders a native <code> element with mantle's monospace styling. Pass asChild to render Code styling on a different element (for example, a link wrapping a code-styled label).

Translation

The <code> element carries translate="no" by default, so a browser translation engine skips the subtree. A translated CLI flag, env var, or YAML key is wrong, and the reader copies it anyway.

The attribute also protects the render itself: Google Translate replaces each text node with a <font> wrapper, which detaches the text nodes React holds, so React either writes updates into a node the DOM no longer shows, or throws NotFoundError from removeChild and tears down the root.

Code also styles terms that are not code, so the default is overridable. Pass translate="yes" when the content is prose:

Kbd and CodeBlock.Code take no such override: a key and a block of code are never translatable.

Browser translation covers what an engine does to a React DOM, and the rule your own components need.

API Reference

Code

All props from code, plus:

PropTypeDefaultDescription
asChild?booleanfalseUse the asChild prop to compose the Code styling onto alternative element types or your own React components.
childrenReactNodeThe content to be rendered inside the inline code element.
className?stringAdditional CSS classes to apply to the inline code element.
translate?"yes" | "no""no"Whether a browser translation engine may translate the content. Pass "yes" when the content is prose.