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 *
string
Source code to highlight.
source *
string
Path to the source file on disk to highlight.
workingDirectory
string
The working directory for the source
. Added automatically when using mdxts/loader
.
filename
string
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
Language of the source code. When used with source
, the file extension will be used by default.
highlightedLines
string
A string of comma separated lines and ranges to highlight e.g. '1, 3-5, 7'
.
focusedLines
string
A string of comma separated lines and ranges to focus e.g. '6-8, 12'
.
unfocusedLinesOpacity
number
= 0.6
Opacity of unfocused lines when using focusedLines
.
allowCopy
boolean
Show or hide a button that copies the source code to the clipboard.
allowErrors
boolean | string
Whether or not to allow errors. Accepts a boolean or comma-separated list of allowed error codes.
showErrors
boolean
Show or hide error diagnostics.
showLineNumbers
boolean
Show or hide line numbers.
showToolbar
boolean
Show or hide the toolbar.
sourcePath
string | false
Path to the source file on disk in development and the git provider source in production.
fixImports
boolean
Whether 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
string
toolbar
string
lineNumbers
string
token
string
popover
string
style
{ 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.CSSProperties
toolbar
React.CSSProperties
lineNumbers
React.CSSProperties
token
React.CSSProperties
popover
React.CSSProperties
children
React.ReactNode
Overrides default rendering to allow full control over styles using CodeBlock
components like Tokens
, LineNumbers
, and Toolbar
.