Skip to main content Red Hat Design System logo Contribute on Github v1.4.5

Code block

Overview Style Guidelines Code Demos Accessibility

Installation

Usage

The content of code-block snippets must be contained within a non-executable <script> tag. By "non-executable", we mean that the script element must have a type attribute which is not module, importmap javascript, or any of the executable javascript mimetypes. With the exception of javascript, use the correct mime type for your content, e.g. text/html for HTML content.

<rh-code-block>
<script type="text/html">
<!DOCTYPE html>
<title>Title</title>
<style>body {width: 500px;}</style>
<body>
<p checked class="title" id="title">Title</p>
<!-- here goes the rest of the page -->
</body>
</script>
</rh-code-block>

Warning

When slotting HTML content into the code-block, if that HTML content contains a `` end tag, you must escape it.

One approach to escaping script tags that is to close the containing <script type="text/html"> within the sample's closing script tag, then immediately open a new sample script tag containing the rest of the snippet. Another method is to insert a zero-width-joiner (or some other unusual unicode character) in the closing script tag, and use javascript to remove it before copying the content to the clipboard. Each method has benefits and drawbacks.

<rh-code-block>
<script type="text/html">
<p>Script tags in HTML must be escaped</p>
<script>console.log('Success!');<
</script><script type="text/html">/script>
</script>
</rh-code-block>

To add javascript content, use the text/sample-javascript type.

<rh-code-block>
<script type="text/sample-javascript">
import '@rhds/elements/rh-code-block/rh-code-block.js';
</script>
</rh-code-block>

rh-code-block

Slots 6
Slot Name Description

A non-executable script tag containing the sample content. JavaScript samples should use the type text/sample-javascript. HTML samples containing script tags must escape the closing </script> tag. Can also be a <pre> tag.

action-label-copy

tooltip content for the copy action button

action-label-wrap

tooltip content for the wrap action button

show-more

text content for the expandable toggle button when the code block is collapsed.

show-less

text content for the expandable toggle button when the code block is expanded.

legend

<dl> element containing rh-badges in the <dt> and legend text in the <dd> elements

Attributes 5
DOM Property Description Type Default
actions
unknown
new Map([
['wrap', html`
<svg xmlns="http://www.w3.org/2000/svg"
fill="currentColor"
viewBox="0 0 20 20">
<path d="M19 0c.313.039.781-.077 1 .057V20c-.313-.039-.781.077-1-.057V0ZM10.82 4.992C9.877 4.996 8.31 5.57 8.174 6c1.21.03 2.432-.073 3.635.08 2.181.383 3.677 2.796 3.066 4.922-.41 1.753-2.108 2.995-3.877 3.014L11 14H5.207l2.682-2.682-.707-.707L3.293 14.5l3.889 3.889.707-.707L5.207 15h5.736l.004-.008c1.444.005 2.896-.59 3.832-1.722 1.65-1.82 1.612-4.85-.08-6.63A5 5 0 0 0 11 5a1.948 1.948 0 0 0-.18-.008z"/>
<path d="M4 5h7c-.039.313.077.781-.057 1H4V5ZM0 0c.313.039.781-.077 1 .057V20c-.313-.039-.781.077-1-.057V0Z"/>
</svg>
`
],
['wrap-active', html`
<svg xmlns="http://www.w3.org/2000/svg"
fill="none"
viewBox="0 0 21 20">
<path fill="currentColor" d="M12 13h1v7h-1zM12 0h1v7h-1z"/>
<path stroke="currentColor" d="M16.465 6.464 20 10l-3.535 3.536"/>
<path fill="currentColor" d="M3 9.5h17v1H3zM0 0h1v20H0z"/>
</svg>
`
],
['copy', html`
<svg xmlns="http://www.w3.org/2000/svg"
version="1.1"
viewBox="0 0 20 20">
<path fill="currentColor" d="M12 0H2C.9 0 0 .9 0 2v10h1V2c0-.6.4-1 1-1h10V0z"/>
<path fill="currentColor" d="M18 20H8c-1.1 0-2-.9-2-2V8c0-1.1.9-2 2-2h10c1.1 0 2 .9 2 2v10c0 1.1-.9 2-2 2zM8 7c-.6 0-1 .4-1 1v10c0 .6.4 1 1 1h10c.6 0 1-.4 1-1V8c0-.6-.4-1-1-1H8z"/>
</svg>
`
],
])
compact

When set, the code block displays with compact spacing

boolean
false
resizable

When set, the code block is resizable

boolean
false
fullHeight

When set, the code block occupies it's full height, without scrolling

boolean
false
wrap

When set, lines in the code snippet wrap

boolean
false
Methods 0

None

Events 0

None

CSS Shadow Parts 0

None

CSS Custom Properties 0

None

Design Tokens 0

None

© 2021-2024 Red Hat, Inc. Deploys by Netlify