CodeBlock
Server OnlyRenders a pre element with syntax highlighting, type information, and type checking.
pre element when
using the Next.js plugin.Styling
The CodeBlock component can be styled using the className and style props to target specific descendant elements.
import { CodeBlock as MdxtsCodeBlock, Tokens } from 'mdxts/components'
import styles from './CodeBlock.module.css'
export function CodeBlock(props: React.ComponentProps<typeof MdxtsCodeBlock>) {
  return (
    <CodeBlock
      source="./counter/useCounter.ts"
      className={{
        container: styles.container,
        token: styles.token,
      }}
      style={{
        // Clear the default styles
        container: {
          boxShadow: undefined,
          borderRadius: undefined,
        },
      }}
    />
  )
}
className and style props. See
the following section for fully customizing the CodeBlock component.Overrides
If you need more customization, the CodeBlock component can be fully overridden by importing it from mdxts/components and extending it:
import { CodeBlock as MdxtsCodeBlock, Tokens } from 'mdxts/components'
export function CodeBlock(props: React.ComponentProps<typeof MdxtsCodeBlock>) {
  return (
    <MdxtsCodeBlock {...props}>
      <Tokens />
    </MdxtsCodeBlock>
  )
}
CodeBlock components
like LineNumbers and Toolbar to render the other parts of the code block.Now import the CodeBlock component in your mdx-components.tsx file and override the pre component to use it instead of the default CodeBlock component:
import { MDXComponents } from 'mdxts/components'
import { CodeBlock } from './CodeBlock'
export function useMDXComponents() {
  return {
    ...MDXComponents,
    pre: CodeBlock,
  } satisfies MDXComponents
}
Examples
API Reference
CodeBlock
View SourceCodeBlockProps
value *
stringSource code to highlight.
source *
stringPath to the source file on disk to highlight.
workingDirectory
stringThe working directory for the source. Added automatically when using mdxts/loader.
filename
stringName or path of the code block. Ordered filenames will be stripped from the name e.g. 01.index.tsx becomes index.tsx.
language
LanguagesLanguage of the source code. When used with source, the file extension will be used by default.
highlightedLines
stringA string of comma separated lines and ranges to highlight e.g. '1, 3-5, 7'.
focusedLines
stringA string of comma separated lines and ranges to focus e.g. '6-8, 12'.
unfocusedLinesOpacity
number= 0.6Opacity of unfocused lines when using focusedLines.
allowCopy
booleanShow or hide a button that copies the source code to the clipboard.
allowErrors
boolean | stringWhether or not to allow errors. Accepts a boolean or comma-separated list of allowed error codes.
showErrors
booleanShow or hide error diagnostics.
showLineNumbers
booleanShow or hide line numbers.
showToolbar
booleanShow or hide the toolbar.
sourcePath
string | falsePath to the source file on disk in development and the git provider source in production.
fixImports
booleanWhether or not to attempt to fix broken imports. Useful for code using imports outside of the project.
className
{ container?: string toolbar?: string lineNumbers?: string token?: string popover?: string }Class names to apply to code block elements. Use the children prop for full control of styling.
container
stringtoolbar
stringlineNumbers
stringtoken
stringpopover
stringstyle
{ container?: React.CSSProperties toolbar?: React.CSSProperties lineNumbers?: React.CSSProperties token?: React.CSSProperties popover?: React.CSSProperties }Styles to apply to code block elements. Use the children prop for full control of styling.
container
React.CSSPropertiestoolbar
React.CSSPropertieslineNumbers
React.CSSPropertiestoken
React.CSSPropertiespopover
React.CSSPropertieschildren
React.ReactNodeOverrides default rendering to allow full control over styles using CodeBlock components like Tokens, LineNumbers, and Toolbar.