Installation

Red Hat CDN

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.

NPM

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:

JSPM

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.

Attributes

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

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-surface-darker

Secondary surface (dark theme)

#1f1f1f
--rh-color-surface-lighter

Tertiary surface (light theme)

#f2f2f2
--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-code

Code font family

RedHatMono, "Red Hat Mono", "Courier New", Courier, monospace
--rh-space-lg

16px spacer

16px
--rh-space-xl

24px spacer

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