Code block
The <CodeBlock> component displays code examples with syntax highlighting powered by Shiki. Code blocks support line highlighting, focusing, titles, and deep linking to make your code examples more readable and interactive.
Usage
Use three backticks with an optional language identifier.
Variants
Titles
Add a title to your code snippet by adding a title after the language identifier. Alternatively, use a title prop (title="Snippet with title") or filename prop (filename="Snippet with title") to achieve the same result.
Line highlighting
Highlight specific lines in your code snippet by placing a numeric range inside {}
after the language identifier. The range is inclusive and can be a single number, a comma-separated list of numbers, or ranges.
Line focusing
Focus on specific lines by adding a comment [!code focus] or by adding a
focus attribute after the language identifier. The focus attribute works the same way as the highlight attribute.
Start line
Control which line appears first in your code block by adding a startLine attribute after the language identifier. This is useful for longer code snippets where you want to highlight the main logic while still providing the complete context.
Max height
Control the max height of the code block by adding
a maxLines attribute after the language identifier. The
maxLines attribute should be a number representing the maximum
number of lines to display. By default, the code block will display up to 20 lines.
When you use maxLines, an expand button automatically appears on hover in the top-right corner, allowing users to view the full code content in an expanded overlay that displays over the page.
To disable the default 20 lines limit, you can set maxLines to 0.
Custom styling
To hide the expand button or add custom styling, target the .fern-expand-button selector:
Wrap overflow
By default, long lines that exceed the width of the code block become scrollable:
To disable scrolling and wrap overflow onto the next line, use the wordWrap prop:
Deep linking
Make specific text within code blocks clickable by defining a links map. This is useful for linking to documentation, API references, or related resources directly from your code examples.
The links property accepts a map where keys are matching patterns (exact strings or regex) and values are the URLs to link to.
Exact string matching
The links property uses JSON format. Each key in the map is matched exactly against text in the code block, and matching text becomes a clickable link to the corresponding URL.
Regex pattern matching
You can use regex patterns for more flexible matching. This is useful when you want to link multiple variations or patterns of text.
In the example below, the pattern /get\\w+/ matches both getPlant and getGarden, while /Plant(Store|Client)/ matches both PlantStore and PlantClient.
When using regex patterns, remember to escape special characters with double backslashes (e.g., \\w+, \\d+) in the JSON string.
Combining props
You can combine the title, highlight, focus, startLine, maxLines, wordWrap, and links
props to create a code block with a title, highlighted lines, specific starting line,
a maximum height, and clickable links.
Code blocks with tabs
You can display multiple code blocks in a tabbed interface.
Language synchronization
Multiple <CodeBlock> components on a documentation site automatically synchronize. This
means when a user selects a <CodeBlock> with a specific language, all other
<CodeBlock> components across your documentation site with the same language will
automatically sync and switch to match. Language preferences are stored in
client-side local storage and persist across browser sessions.
The example below demonstrates language sync in action – choosing a language in
either set of <CodeBlock> components will automatically update both sets to match:
Tabs and CodeBlocks integration
CodeBlocks automatically synchronize with <Tab> components that have a language property.
Linking to language-specific content
You can link directly to content in a specific language by adding ?language=<some-language> to the end of a URL. This sets which language tab wil be displayed by default when users visit the page.
For example, the following link opens with Java tabs displayed: https://buildwithfern.com/learn/docs/writing-content/components/tabs?language=java
This works with both CodeBlocks and Tab components that have a language property.
Override synchronization
You can override the synchronization of code blocks by setting the for prop.