Skip to content
npm ↗ GitHub ↗

Documentation

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

ClassDescription
.cb-codeBlock container.
.cb-code__headHeader with file name and button.
.cb-code__copyCopy button. .is-copied turns it green. --floating places it over the code.
.cb-code__preHorizontally scrollable area.
.cb-code--inlineInline code.
.cb-tok-tag|attr|str|com|keySyntax colors based on tokens.

data-cb-* attributes

AttributeDescription
data-cb-copyWith 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-labelElement whose text changes to "Copied".
data-cb-copy-doneAlternative text for the copied state.
data-cb-copy-errorText shown if copying failed. Default: "Copy failed".

JavaScript

EventWhen it fires
cb:copyOn 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.