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,
},
}}
/>
)
}
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}>
<pre
style={{
whiteSpace: 'pre',
wordWrap: 'break-word',
overflow: 'auto',
}}
>
<Tokens />
</pre>
</MdxtsCodeBlock>
)
}
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
}