Mosmox UI
Current version
GitHubnpmjs.com
Content & Media

CodeBlock

Formatted, copyable code blocks for documentation and developer interfaces

CodeBlock presents source code in a readable, accessible block. It supports an optional header, language label, copy action, line numbers, highlighted lines, wrapping, themes, sizes, and constrained scrolling.

Basic usage

import { CodeBlock } from '@mosmox/ui';

<CodeBlock
  code={'const answer = 42;'}
  language="typescript"
  title="example.ts"
  showLineNumbers
/>

The component renders the supplied string inside a semantic pre/code structure. It never executes the supplied code. Basic syntax coloring is enabled by default and can be disabled with syntaxHighlight={false}.

Header and copy action

The header is shown by default whenever there is a title, language, or copy action. Set showHeader={false} for a compact code-only presentation. Copying uses the browser Clipboard API and calls onCopy after a successful copy.

<CodeBlock code={source} language="tsx" title="Button.tsx" onCopy={() => track('code-copied')} />
<CodeBlock code={source} copyable={false} showHeader={false} />

Customize accessible button labels with copyLabel and copiedLabel.

Lines, wrapping, and scrolling

<CodeBlock
  code={source}
  showLineNumbers
  highlightLines={[3, 4]}
  wrap
  maxHeight={240}
  tabSize={4}
/>
  • showLineNumbers adds non-selectable line numbers.
  • highlightLines accepts one-based line numbers and adds a visual emphasis background.
  • wrap allows long lines to wrap instead of scrolling horizontally.
  • maxHeight constrains the code viewport and enables vertical scrolling.
  • tabSize controls the visual width of tab characters.

Use lineNumberStart when the displayed snippet comes from a larger file. onLineClick makes each rendered line keyboard- and pointer-activatable and receives the displayed line number and source text.

<CodeBlock
  code={'const ready = true;\nreturn ready;'}
  showLineNumbers
  lineNumberStart={42}
  onLineClick={(lineNumber, sourceLine) => console.log(lineNumber, sourceLine)}
/>

Themes and sizes

theme accepts ten built-in themes: light, dark, black, github, solarized, dracula, nord, monokai, ocean, and high-contrast. size accepts sm, default, or lg and changes code typography. syntaxHighlight controls the built-in keyword, string, number, literal, and comment coloring.

<CodeBlock code="const compact = true;" size="sm" theme="light" />
<CodeBlock code="const readable = true;" size="lg" theme="dark" />

radius accepts a CSS length or a number interpreted as pixels.

ConfigProvider

Set defaults for a subtree with components.codeBlock. Instance props take precedence over provider values.

<ConfigProvider
  components={{
    codeBlock: {
      theme: 'github',
      copyable: false,
      fontSize: '14px',
      radius: '12px',
    },
  }}
>
  <CodeBlock code="export const ready = true;" language="tsx" />
</ConfigProvider>
SettingTypeDefaultDescription
copyablebooleantrueShows the copy button by default.
fontSizestringcomponent defaultBase code font size.
radiusstring8pxDefault block radius.
themeCodeBlockThemedarkDefault color theme.

API Reference

PropTypeDefaultDescription
codestringrequiredSource text to display.
languagestring—Optional language label in the header.
titleReactNode—Optional file name or custom header title.
showHeaderbooleantrueShows the header when header content exists.
showLineNumbersbooleanfalseShows one-based line numbers.
lineNumberStartnumber1Number shown for the first rendered line.
onLineClick(lineNumber, code) => void—Called when a line is clicked or activated with Enter/Space.
highlightLinesnumber[][]Lines to highlight.
wrapbooleanfalseWraps long lines.
copyablebooleanprovider value or trueEnables the copy action.
copyLabelstringCopy codeAccessible label for the copy button.
copiedLabelstringCopiedTemporary label after copying.
onCopy(code: string) => void—Callback after a successful copy.
sizesm | default | lgdefaultCode text size.
themeCodeBlockThemeprovider value or darkColor theme.
syntaxHighlightbooleantrueApplies built-in syntax token colors.
radiusnumber | stringprovider value or 8pxBlock radius; numbers are pixels.
maxHeightnumber | string—Scroll viewport maximum height.
tabSizenumber2Visual tab width.
classNamestring—Additional block class.
styleCSSProperties—Additional styles and CSS custom properties.

All standard HTMLAttributes<HTMLDivElement> are also supported, including id, data-*, and event handlers.

Accessibility

The copy control has an accessible label and changes its label after a successful copy. Line numbers are marked aria-hidden so screen readers read the source text without announcing duplicated numbering. The code is kept as text, preserving keyboard selection and copy behavior.