apps/www/content/docs/components/code-block.mdx
import { CodeBlock } from "@chakra-ui/react"
<CodeBlock.AdapterProvider>
<CodeBlock.Root>
<CodeBlock.Header>
<CodeBlock.Title />
<CodeBlock.Control>
<CodeBlock.CopyTrigger />
<CodeBlock.CollapseTrigger />
</CodeBlock.Control>
</CodeBlock.Header>
<CodeBlock.Content>
<CodeBlock.Code>
<CodeBlock.CodeText />
</CodeBlock.Code>
</CodeBlock.Content>
</CodeBlock.Root>
</CodeBlock.AdapterProvider>
The CodeBlock component works for Shiki and Highlight.js highlighting engines.
The docs assumes Shiki by default.
To setup the code block component, you need to:
CodeBlock.AdapterProvider at the top level.CodeBlock.Root component within the CodeBlock.AdapterProvider.Install the shiki package.
npm install shiki
Then, create the shiki adapter that dynamically loads the shiki highlighter for the selected languages.
import type { HighlighterGeneric } from "shiki"
import { createShikiAdapter } from "@chakra-ui/react"
const shikiAdapter = createShikiAdapter<HighlighterGeneric<any, any>>({
async load() {
const { createHighlighter } = await import("shiki")
return createHighlighter({
langs: ["tsx", "json"],
themes: ["github-dark", "github-light"],
})
},
})
<CodeBlock.AdapterProvider value={shikiAdapter}>
</CodeBlock.AdapterProvider>
Install the highlight.js package.
npm install highlight.js
Then, create the highlight.js adapter that dynamically loads the selected languages.
import { createHighlightJsAdapter } from "@chakra-ui/react"
import hljs from "highlight.js/lib/core"
const highlightJsAdapter = createHighlightJsAdapter<typeof hljs>({
async load() {
const languages = {
tsx: () => import("highlight.js/lib/languages/typescript"),
html: () => import("highlight.js/lib/languages/xml"),
}
await Promise.all(
Object.entries(languages).map(async ([language, file]) => {
const { default: langModule } = await file()
hljs.registerLanguage(language, langModule)
}),
)
return hljs
},
})
Use the size prop to change the size of the code block component.
Render the CodeBlock.Title component within the CodeBlock.Header component
to add a title to the code block component.
Use the copyButton prop to add a copy button to the code block component.
Line numbers make it easier to reference specific lines of code. Pass the
meta.showLineNumbers prop to show line numbers in the code block component.
Pass the meta.highlightLines prop to the CodeBlock.Root component to
highlight specific lines of code. The prop accepts an array of line numbers.
Pass the meta.focusedLineNumbers prop to the CodeBlock.Root component to
focus specific lines of code. The prop accepts an array of line numbers. The
line numbers.
Diffs are useful for highlighting source code changes. Use the
meta.addedLineNumbers and meta.removedLineNumbers props to add line numbers
to the code block component.
<ExampleTabs name="code-block-with-diff" />The prop accepts an array of line numbers. The line numbers are 1-based.
Use the meta.maxLines prop to limit the number of lines in the code block
component. By default, the code block component will expand to fit the content.
Here's an example that re-creates an API endpoint request component by composing
the CodeBlock and Select components.
Here's an example that adds a floating copy button to the code block component.
<ExampleTabs name="code-block-with-floating-copy-button" />Here's an example that composes the CodeBlock component with the Tabs
component to create a code block with tabs.
Here's an example that automatically syncs all code blocks that share the same storage key. Useful for package manager or framework specific code blocks in a documentation site.
<ExampleTabs name="code-block-with-tabs-sync" />Use the meta.colorScheme prop to add a theme to the code block component. In
this example, the colorScheme is set to color mode from the useColorMode hook.
Use the meta.wordWrap prop to wrap the code block component.
You can combine line numbers with word wrapping by setting both
meta.showLineNumbers and meta.wordWrap to true. The line numbers will
properly align with wrapped text.
Here's an example that uses highlight.js to highlight the code block.
<ExampleTabs name="code-block-with-highlight-js" />The code block falls back to a plain text by default. To create a plain text
code block, remove the use of CodeBlock.AdapterProvider.