CodeBlock

Renders a pre element with syntax highlighting, type information, and type checking.

Style Overrides

The CodeBlock component can be styled using the, css, className, and style props to target specific descendant components.

import {
  type CodeBlockProps,
  CodeBlock as BaseCodeBlock,
  Tokens,
} from 'renoun/components'
import styles from './CodeBlock.module.css'

export function CodeBlock(props: CodeBlockProps) {
  return (
    <BaseCodeBlock
      {...props}
      css={{
        // Clear the default styles
        container: {
          boxShadow: undefined,
          borderRadius: undefined,
        },
        ...props.css,
      }}
      className={{
        container: styles.container,
        token: styles.token,
        ...props.className,
      }}
    />
  )
}

Component Overrides

If you need more customization, the CodeBlock component can be fully overridden by importing it from renoun/components and extending it:

CodeBlock.tsx
import {
  type CodeBlockProps,
  CodeBlock as BaseCodeBlock,
  Tokens,
} from 'renoun/components'

export function CodeBlock(props: CodeBlockProps) {
  return (
    <BaseCodeBlock {...props}>
      <pre
        style={{
          whiteSpace: 'pre',
          wordWrap: 'break-word',
          overflow: 'auto',
        }}
      >
        <Tokens />
      </pre>
    </BaseCodeBlock>
  )
}

Formatting

The CodeBlock source text is formatted using either the TypeScript compiler or using prettier if it is available to the workspace

API Reference

CodeBlock

typeof CodeBlock

Renders a pre element with syntax highlighting, type information, and type checking.

Parameters
CodeBlockProps
Returns
Element

BaseCodeBlockProps

BaseCodeBlockProps
Properties

filename

string | undefined

Name or path of the code block. Ordered filenames will be stripped from the name e.g. 01.index.tsx becomes index.tsx.

language

Languages | undefined

Language of the source code. When used with source, the file extension will be used by default.

highlightedLines

string | undefined

A string of comma separated lines and ranges to highlight e.g. '1, 3-5, 7'.

focusedLines

string | undefined

A string of comma separated lines and ranges to focus e.g. '6-8, 12'.

unfocusedLinesOpacity

number | undefined

Opacity of unfocused lines when using focusedLines.

allowCopy

boolean | undefined

Show or hide a button that copies the source code to the clipboard.

allowErrors

string | boolean | undefined

Whether or not to allow errors. Accepts a boolean or comma-separated list of allowed error codes.

showErrors

boolean | undefined

Show or hide error diagnostics.

showLineNumbers

boolean | undefined

Show or hide line numbers.

showToolbar

boolean | undefined

Show or hide the toolbar.

shouldFormat

boolean | undefined

Whether or not to format the source code using prettier if installed.

css

{ container?: CSSObject; toolbar?: CSSObject; lineNumbers?: CSSObject; token?: CSSObject; popover?: CSSObject; } | undefined

CSS styles to apply to code block elements.

className

{ container?: string; toolbar?: string; lineNumbers?: string; token?: string; popover?: string; } | undefined

Class names to apply to code block elements.

style

{ container?: React.CSSProperties; toolbar?: React.CSSProperties; lineNumbers?: React.CSSProperties; token?: React.CSSProperties; popover?: React.CSSProperties; } | undefined

Styles to apply to code block elements.

children

React.ReactNode

Overrides default rendering to allow full control over styles using CodeBlock components like Tokens, LineNumbers, and Toolbar.

CodeBlockProps

CodeBlockProps
Properties

value *

string

Source code to highlight.

BaseCodeBlockProps
Properties

source *

string

Path to the source file on disk to highlight.

workingDirectory

string | undefined

The working directory for the source.

BaseCodeBlockProps