Code block
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
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.