<CodeBlock>
ComponentA formatted code block for displaying lovely lines of code.
<CodeBlock language="shell-session" code={`<span class="token command"><span class="token shell-symbol important">$</span> <span class="token bash language-bash"><span class="token builtin class-name">echo</span> <span class="token string">"hello world!"</span></span></span>`} />
Name | Description |
---|---|
className string | A className string which will be added to the outer element of this component. |
code* string | A string of highlighted HTML or React elements. These elements will be rendered into a <pre><code> container.A plain string can be passed, but it will not be highlighted. |
language string | Used to set a global "language-*" className on both the pre and code element, for compatibility with language-specific highlight styles. This value should be identical to the language used to generate the highlighted code . |
theme string | Sets the color theme for the code block. Intended to match light and dark system appearance, for example through CSS @media (prefers-color-scheme) . |
hasBarAbove boolean | Intended for automatic use in CodeTabs, not meant as a consumer-facing prop. Set to true to remove border rounding from the top of the CodeBlock. |
onCopyCallback function | Optional callback that is called when copy success state changes to true or false . When code is successfully copied using the "Copy" button, it changes to true . If there is an error when copying code, it changes to false . |
options object | Additional options that enable supplementary code-block features.Object contains nested props, see below: |
options.showChrome boolean | Set to true to display a window chrome bar UI above the code block. |
options.highlight string | Specify line numbers to highlight. Supports a comma-separate list of numbers and number ranges, where number ranges are dash-separated pairs of numbers. For example: "5" highlights line 5; "2,5" highlights lines 2 and 5; "2-5" highlights lines 2, 3, 4, and 5; "2,6-8,11" highlights line 2, 6, 7, 8 and 11. |
options.lineNumbers boolean | Set to true to display line numbers on the left edge of the code block. |
options.showClipboard boolean | Set to true to show the copy-to-clipboard prompt and functionality. |
options.wrapCode boolean | Set to true to make long lines of code wrap rather than overflow. |
To support richer configuration of code blocks in MDX, we've created a CodeBlockConfig
component, which is available in Learn and Docs MDX contexts.
CodeBlockConfig
is a wrapper component, similar to the ImageConfig
component in our learn
repo. CodeBlockConfig
can be used to wrap and configure an individual fenced code block. It supports all of the properties of CodeBlock
's options
props, as detailed in the props table earlier on this page, with a few notable exceptions:
showClipboard
is not supported, as the Copy
button is shown by default in MDX contexts. Instead, a hideClipboard
option is supported.showChrome
is not supported, as we currently would like to limit such ornamentation to JSX contexts, such as feature callouts on marketing pages.Longer lines of code may take up more space than the available content width. In these cases, code blocks will allow horizontal scrolling to view overflowing code.
Source
Result
Note this also works with line numbers and line highlighting.
1234
In cases where wrapping code to new lines is preferred over horizontal scrolling, the options.wrapCode
prop can be set to true
. Note that this option is not yet supported in MDX contexts.
Source
Result
A line that goes on for a very long time so that it overflows the container in which it is located, which might be a pretty wide container.
This is a second line of code.
And a third line.
And another line, this is the fourth line.
Note this also works with line numbers and line highlighting.
1A line that goes on for a very long time so that it overflows the container in which it is located, which might be a pretty wide container.
2This is a second line of code.
3And a third line.
4And another line, this is the fourth line.
Automatic syntax highlighting is supported in MDX contexts such as Learn and Docs. Syntax highlighting is set through the code fence's infostring
, which is the string directly after the opening code fence. You can view a full list of supported syntax slugs and aliases in the refractor
README.
Source
Result
The copy to clipboard button is useful to our practioners when they need to copy and paste content. In most use-cases the copy to clipboard button is necessary, so this feature is enabled by default on all Learn and Docs pages.
Source
Result
In some cases, such as when showing diffs, we might want to hide the copy-to-clipboard functionality. In MDX use cases, use the <CodeBlockConfig />
wrapper component with the hideClipboard
prop, as shown below.
Source
Result
Our copy-to-clipboard functionality automatically tries to detect shell snippets, and ensure only commands are copied, rather than all characters rendered in a shell session.
However, there are limitations to what is possible with this feature. To start, detection is fairly primitive - any code starting with a $
character on the first line will be interpreted as a shell snippet when copied. This is intended to allow the display of control characters such as $
and shell output in the code block, while ensuring that when copying the snippet, only the executable part of the snippet is copied to the clipboard.
As a basic example, the following snippet's Copy
button should yield echo "hello world"
rather than the full contents of the code block:
Source
Result
The following multi-line command will be copied in similar way. Note that when using multi-line commands, subsequent lines such as output or additional commands should be placed in a separate code block, as they will not be copied correctly if they are placed directly after a multi-line command.
Source
Result
The following multi-command snippet should yield two copied lines, both without the leading $
shell symbol. As well, selecting the code should not copy the shell symbol, or the space after it.
Source
Result
Further examples:
Scenario | Supported | What gets copied | Example |
---|---|---|---|
Single line commands | β | The command will be copied without the leading $ . | π |
Multi-line commands | β | The multi-line command will be copied, without the leading $ . Do not include lines after the multi-line command, such as output or additional commands, as they will not be processed correctly. | π |
Commands with output | β | The command will be copied without the leading $ or the output. | π |
Non-shell Snippets | β | The entire snippet will be copied. | π |
Single-line commands across multiple lines | β | Commands will be copied without their leading $ . Lines that do not start in $ will be omitted from the copied snippet. | β³ (none yet, relatively new feature) |
Multi-line with multiple commands | β | Not supported. Only the first command will be copied. | π |
Use line numbers for long code blocks, or when you are calling out specific lines via line highlight.
Source
Result
1234
Use line highlights to visually call out specific lines. The lineNumbers
option should typically be used as well. Note that non-highlighted lines will be dimmed, and will not have syntax highlighting colors applied.
Source
Result
123456
Code block headings should use a few actionable words and phrases to ensure efficient scanning and identification. Headings can start to stack and become hard to read at smaller viewports if they are too many characters. Aim for headings with 50 or fewer characters.
Source
Result
Add a filename to be shown above the snippet. Filenames should always correspond with an actual curated file that our users will be using or referencing to avoid confusion.
Source
Result
CodeTabs
The CodeTabs
component, which is available in Learn and Docs MDX contexts, allows authors to create multi-snippet tabbed code blocks. <CodeTabs>
is a wrapper component around child code blocks. Each child must be either a fenced code block, or must be a <CodeBlockConfig>
which wraps a fenced code block.
By default, the tab options are shown at the left of the component. When space is limited, tabs will automatically collapse into a dropdown menu. When a heading
is provided to CodeTabs
, the heading will be shown at left, and the tabs (or dropdown menu) will be shown at right.
CodeTabs
Playground<CodeTabs theme="dark"><CodeBlockoptions={{ showClipboard: true, filename: 'hello-world.js' }}language="javascript"code={`<span class="token console class-name">console</span><span class="token punctuation">.</span><span class="token method function property-access">log</span><span class="token punctuation">(</span><span class="token string">'hello world'</span><span class="token punctuation">)</span>`}/><CodeBlockoptions={{ showClipboard: true }}language="go"code={`<span class="token keyword">package</span> main{/* */}{/* */}<span class="token keyword">import</span> <span class="token string">"fmt"</span>{/* */}<span class="token keyword">func</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>{/* */} fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token string">"hello world"</span><span class="token punctuation">)</span><span class="token punctuation">}</span>`}/></CodeTabs>
CodeTabs
Tabs should only be used when switching between equivalent approaches to a particular task or workflow. Common examples include alternate configuration type, programming, languages, or operating systems:
CodeTabs
canΒ be used directly with fenced code children.
Source
Result
CodeBlockConfig
child tabsCodeTabs
can be used with fenced code children wrapped with CodeBlockConfig
configuration. Note as well that tabs can be mixed - some tabs can be plain fenced code and others can use CodeBlockConfig
.
Source
Result
1 2 3 4 5 6 7 8 9 1011
By default, CodeTabs
will automatically set tab headings based on the language used for syntax highlighting in each child tab.
In some cases, we may want to set custom tab headings. This can be done by passing an array of heading strings to CodeTab's tabs
prop.
Source
Result
The CodeBlock
component supports a few extra features in JSX that are not used in MDX contexts. These features are detailed below.
The options.showChrome
prop, which is only available in JSX contexts, allows consumers to display a bar of window-like UI chrome above the code block. This feature is most often used on feature overviews on marketing sites.
In JSX contexts, the consumer has the option to set the theme
of the code block. Note however in the future, the theme
prop may be ignored in favour of user preferences for light or dark colour schemes, for example via the @media (prefers-color-scheme)
CSS media query.
"dark"
theme
1 2 3 4 5 6 7 8 9 101112
"light"
theme
1 2 3 4 5 6 7 8 9 101112
When rendering highlighted code tokens, the CodeBlock
component's code
prop can accept either HTML or JSX that represents those tokens. This allows the component to function both in MDX contexts (where JSX tokens are passed) and in JSX contexts (where consumers will likely pass highlighted HTML).
With html
code
With jsx
code