-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
CodeBlock.mdx
80 lines (67 loc) · 2.1 KB
/
CodeBlock.mdx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
<Note>
This component is automatically set up to override the `pre` element when
using the [Next.js plugin](/packages/next).
</Note>
## Styling
The `CodeBlock` component can be styled using the `className` and `style` props to target specific descendant elements.
```tsx
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,
},
}}
/>
)
}
```
<Note>
Not every element can be styled using the `className` and `style` props. See
the following section for fully customizing the `CodeBlock` component.
</Note>
## Overrides
If you need more customization, the `CodeBlock` component can be fully overridden by importing it from `mdxts/components` and extending it:
```tsx filename="CodeBlock.tsx"
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>
)
}
```
<Note>
This will only render highlighted tokens. Use the other `CodeBlock` components
like `LineNumbers` and `Toolbar` to render the other parts of the code block.
</Note>
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:
```tsx filename="02.mdx-components.tsx"
import { MDXComponents } from 'mdxts/components'
import { CodeBlock } from './CodeBlock'
export function useMDXComponents() {
return {
...MDXComponents,
pre: CodeBlock,
} satisfies MDXComponents
}
```