Code block
On this page
Overview
A code block provides formatted display of code snippets for documentation.
Authors SHOULD provide a <script type="text/..."> or <pre> as slot
content and MUST NOT use executable script types. SHOULD be paired with a
heading so screen reader users understand context. Keyboard users Tab to
action buttons and activate with Enter or Space.
Edit element properties
import '@rhds/elements/rh-code-block/rh-code-block.js';
<meta itemprop="description" content="Basic code block displaying an HTML snippet with line numbers.">
<rh-code-block>
<script type="text/html"><!DOCTYPE html>
<title>Title</title>
<style>body {width: 500px;}</style>
<script type="application/javascript">
function $init() {return true;}
<</script><script type="text/html">/script>
<body>
<p checked class="title" id="title">Title</p>
<!-- here goes the rest of the page -->
</body></script>
</rh-code-block>
import { CodeBlock } from "@rhds/elements/react/rh-code-block/rh-code-block.js";
// NOTE: React 19+ does not require these wrapper imports.
// You can use the custom elements directly as-is.
export const Demo = () => (
<meta itemprop="description" content="Basic code block displaying an HTML snippet with line numbers." />
<CodeBlock>
<script type="text/html"><!DOCTYPE html>
<title>Title</title>
<style>body {width: 500px;}</style>
<script type="application/javascript">
function $init() {return true;}
<</script>
<script type="text/html">/script>
<body>
<p checked class="title" id="title">Title</p>
<!-- here goes the rest of the page -->
</body></script>
</CodeBlock>
);
Space- or comma-separated list of code block action buttons to display, containing either 'copy', 'wrap', or both. 'copy' adds a button that copies the text content to the clipboard. 'wrap' adds a button that toggles line wrap.
To override the default labels, e.g. for purposes of internationalization, use the
action-label-copy and action-label-wrap slots. Each slot may receive two elements,
one for the action's default state (e.g. "Copy to clipboard"),
and one for the actions alternative state, e.g. "Copied!".
The active-state element must have the attributes hidden data-code-block-state="active"
</figure>
When set to "client", <rh-code-block> will automatically highlight the source using Prism.js
When set to "Prerendered", <rh-code-block> will apply supported RHDS styles to children with
prismjs classnames in the element's root.
When set along with highlighting="client", this grammar will be used to highlight source code
When set, the code block displays with compact spacing
When set, the code block source code will be dedented
When set, the code block is resizable
When set, the code block occupies it's full height, without scrolling
When set, lines in the code snippet wrap
When set to hidden, the code block's line numbers are hidden
Status
- Figma library:
-
Ready - RH Elements:
-
Ready - RH Shared Libs:
-
Planned
When to use
- Visually separate and highlight code text from other elements on the page
Status checklist
| Property | Status | Meaning |
|---|---|---|
| Figma library |
|
Component is available in the Figma library |
| RH Elements |
|
Component is available in RH Elements |
| RH Shared Libs |
|
Component will be added to RH Shared Libs |
Other libraries
To learn more about our other libraries, visit this page.
Feedback
To give feedback about anything on this page, contact us.