Code block
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 `` 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
Slot Name | Description |
---|---|
|
A non-executable script tag containing the sample content. JavaScript samples should use the type |
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 |
|
DOM Property | Description | Type | Default |
---|---|---|---|
actions |
|
|
|
compact |
When set, the code block displays with compact spacing |
|
|
resizable |
When set, the code block is resizable |
|
|
fullHeight |
When set, the code block occupies it's full height, without scrolling |
|
|
wrap |
When set, lines in the code snippet wrap |
|
|
None
None
None
None
None
Other libraries
To learn more about our other libraries, visit this page.
Feedback
To give feedback about anything on this page, contact us.