18/Content
Code block
Code with a header, a copy button and syntax colors that follow the theme.
With header
resources/css/app.css
@import "tailwindcss";
@import "cobalto/tailwind";
HTML
<div class="cb-code">
<div class="cb-code__head"><span>resources/css/app.css</span><button class="cb-code__copy" type="button" data-cb-copy><svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" aria-hidden="true"><rect x="9" y="9" width="11" height="11" rx="2"/><path d="M5 15V5a2 2 0 0 1 2-2h10"/></svg><span data-cb-copy-label>Copiar</span></button></div>
<pre class="cb-code__pre"><code><span class="cb-tok-key">@import</span> <span class="cb-tok-str">"tailwindcss"</span>;
<span class="cb-tok-key">@import</span> <span class="cb-tok-str">"cobalto/tailwind"</span>;</code></pre>
</div>
Copy a text
With a value, data-cb-copy copies that text or the element with that id.
npm install cobalto
HTML
<code class="cb-code--inline">npm install cobalto</code>
<button class="cb-btn cb-btn--outline cb-btn--sm" type="button" data-cb-copy="npm install cobalto"><svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" aria-hidden="true"><rect x="9" y="9" width="11" height="11" rx="2"/><path d="M5 15V5a2 2 0 0 1 2-2h10"/></svg><span data-cb-copy-label>Copiar comando</span></button>
Classes and modifiers
| Class | Description |
|---|---|
.cb-code | Block container. |
.cb-code__head | Header with file name and button. |
.cb-code__copy | Copy button. .is-copied turns it green. --floating places it over the code. |
.cb-code__pre | Horizontally scrollable area. |
.cb-code--inline | Inline code. |
.cb-tok-tag|attr|str|com|key | Syntax colors based on tokens. |
data-cb-* attributes
| Attribute | Description |
|---|---|
data-cb-copy | With no value it copies the block's pre; with an id or selector it copies that element; with any other text, that text. |
data-cb-copy-label | Element whose text changes to "Copied". |
data-cb-copy-done | Alternative text for the copied state. |
data-cb-copy-error | Text shown if copying failed. Default: "Copy failed". |
JavaScript
| Event | When it fires |
|---|---|
cb:copy | On the button after copying (detail.text). |
Accessibility
- The button changes its text to "Copied", which screen readers announce while it has focus.
- Without HTTPS it falls back to an alternative copy method.