Installation
CDN Prerelease
We are currently working on our CDN, which will be soon moving into beta. This will be the preferred method of installation in the near future. If you are a Red Hat associate and have questions or comments about the CDN or installation process please join us in our Red Hat Design System Google chat.
In the meantime, install this component using npm
The recommended way to load RHDS is via the Red Hat Digital Experience CDN, and using an import map.
If you have full control over the page you are using, add an import map
to the <head>
, pointing to the CDN, or update any existing import map.
If you are not responsible for the page's <head>
, request that the
page owner makes the change on your behalf.
<script type="importmap">
{
"imports": {
"@rhds/elements/": "https://www.redhatstatic.com/dx/v1-alpha/@rhds/elements@1.1.0/elements/",
"@patternfly/elements/": "https://www.redhatstatic.com/dx/v1-alpha/@patternfly/elements@2.2.2/"
}
}
</script>
Once the import map is established, you can load the element with the following module, containing a bare module specifier:
<script type="module">
import '@rhds/elements/rh-code-block/rh-code-block.js';
</script>
Note that Modules may be placed in the <head>
: since they are deferred by default,
they will not block rendering.
Install RHDS using your team's preferred NPM package manager, e.g.
npm install @rhds/elements
Once that's been accomplished, you will need to use a bundler to resolve the bare module specifiers and optionally optimize the package for your site's particular use case and needs. Comprehensive guides to bundling are beyond the scope of this page; read more about bundlers on their websites:
Public CDNs
JSPM and other public CDNs should not be used on corporate domains. Use them for development purposes only!
Add an import map to the <head>
, pointing to the CDN,
or update any existing import map.
<script type="importmap">
{
"imports": {
"@rhds/elements/": "https://jspm.dev/@rhds/elements/",
"@patternfly/elements/": "https://jspm.dev/@patternfly/elements/"
}
}
</script>
Once the import map is established, you can load the element with the following module, containing a bare module specifier:
<script type="module">
import '@rhds/elements/rh-code-block/rh-code-block.js';
</script>
Note that Modules may be placed in the <head>
: since they are deferred by default,
they will not block rendering.
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 </script>
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>
Slots
- Default Slot
-
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
actions
- DOM Property
actions
- Type
-
unknown
- Default
-
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
- DOM Property
compact
- Type
-
boolean
- Default
-
false
resizable
-
When set, the code block is resizable
- DOM Property
resizable
- Type
-
boolean
- Default
-
false
full-height
-
When set, the code block occupies it's full height, without scrolling
- DOM Property
fullHeight
- Type
-
boolean
- Default
-
false
wrap
-
When set, lines in the code snippet wrap
- DOM Property
wrap
- Type
-
boolean
- Default
-
false
Methods
None
Events
None
CSS Shadow Parts
None
CSS Custom Properties
CSS Property | Description | Default |
---|---|---|
--rh-border-radius-default |
3px border radius; Example: Card |
3px |
--rh-border-width-sm |
1px border width; Example: Secondary CTA or Button |
1px |
--rh-color-border-subtle-on-dark |
Subtle border color (dark theme) |
#707070 |
--rh-color-border-subtle-on-light |
Subtle border color (light theme) |
#c7c7c7 |
--rh-color-gray-60 |
Secondary text (light theme) |
#4D4D4D |
--rh-color-icon-secondary-on-dark |
#ffffff |
|
--rh-color-icon-secondary-on-light |
#151515 |
|
--rh-color-surface-dark |
Tertiary surface (dark theme) |
#383838 |
--rh-color-surface-dark-alt |
Alternative tertiary surface (not available for use with context provider) |
#292929 |
--rh-color-surface-light |
Secondary surface (light theme) |
#e0e0e0 |
--rh-color-surface-lighter |
Tertiary surface (light theme) |
#f2f2f2 |
--rh-color-text-primary-on-dark |
Primary text color for dark theme |
#ffffff |
--rh-color-text-primary-on-light |
Primary text color for light theme |
#151515 |
--rh-color-text-secondary-on-dark |
Secondary text color for dark theme |
#c7c7c7 |
--rh-font-family-body-text |
Body text font family |
RedHatText, "Red Hat Text", "Noto Sans Arabic", "Noto Sans Hebrew", "Noto Sans JP", "Noto Sans KR", "Noto Sans Malayalam", "Noto Sans SC", "Noto Sans TC", "Noto Sans Thai", Helvetica, Arial, sans-serif |
--rh-font-family-code |
Code font family |
RedHatMono, "Red Hat Mono", "Courier New", Courier, monospace |
--rh-font-size-body-text-sm |
14px font size |
0.875rem |
--rh-font-size-code-md |
16px font size |
1rem |
--rh-font-weight-body-text-regular |
Regular font weight |
400 |
--rh-font-weight-code-regular |
Regular font weight |
400 |
--rh-length-3xl |
48px length token |
48px |
--rh-line-height-body-text |
Line height for body text |
1.5 |
--rh-size-icon-02 |
24px icon box |
24px |
--rh-size-icon-06 |
64px icon box |
64px |
--rh-space-3xl |
48px spacer |
48px |
--rh-space-4xl |
64px spacer |
64px |
--rh-space-lg |
16px spacer |
16px |
--rh-space-md |
8px spacer |
8px |
--rh-space-sm |
6px spacer |
6px |
--rh-space-xl |
24px spacer |
24px |
Related elements or patterns
Feedback
To give feedback about anything on this page, contact us.
Red Hat legal and privacy links
- About Red Hat
- Jobs
- Events
- Locations
- Contact Red Hat
- Red Hat Blog
- Diversity, equity, and inclusion
- Cool Stuff Store
- Red Hat Summit