Menu

Code block

Added in 1.8.0

A code block shows a snippet on a tinted surface, with an optional header and a copy button.

Default markup

A .code-block wraps a pre. The copy button uses the Clipboard plugin: data-bs-target points to the element with the code. Without a header, the button sits in the top right corner.

npm install @tabler/core
<div class="code-block">
  <button type="button" class="code-block-copy" data-bs-toggle="clipboard" data-bs-target="#code-block-basic" aria-label="Copy code">
    <svg xmlns="http://www.w3.org/2000/svg" class="icon clipboard-label" width="24" height="24" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round">
      <path stroke="none" d="M0 0h24v24H0z" fill="none" />
      <path d="M7 7m0 2.667a2.667 2.667 0 0 1 2.667 -2.667h8.666a2.667 2.667 0 0 1 2.667 2.667v8.666a2.667 2.667 0 0 1 -2.667 2.667h-8.666a2.667 2.667 0 0 1 -2.667 -2.667z" />
      <path d="M4.012 16.737a2.005 2.005 0 0 1 -1.012 -1.737v-10c0 -1.1 .9 -2 2 -2h10c.75 0 1.158 .385 1.5 1" />
    </svg>
    <svg xmlns="http://www.w3.org/2000/svg" class="icon clipboard-feedback" width="24" height="24" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round">
      <path stroke="none" d="M0 0h24v24H0z" fill="none" />
      <path d="M5 12l5 5l10 -10" />
    </svg>
  </button>
  <pre id="code-block-basic"><code>npm install @tabler/core</code></pre>
</div>

Add a .code-block-header with a .code-block-title and a .code-block-lang. The copy button moves into the header.

tabler.config.jsJavaScript
export default {
theme: 'dark',
radius: 'lg',
}
<div class="code-block">
  <div class="code-block-header">
    <span class="code-block-title">tabler.config.js</span>
    <span class="code-block-lang">JavaScript</span>
    <button type="button" class="code-block-copy" data-bs-toggle="clipboard" data-bs-target="#code-block-header" aria-label="Copy code">
      <svg xmlns="http://www.w3.org/2000/svg" class="icon clipboard-label" width="24" height="24" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round">
        <path stroke="none" d="M0 0h24v24H0z" fill="none" />
        <path d="M7 7m0 2.667a2.667 2.667 0 0 1 2.667 -2.667h8.666a2.667 2.667 0 0 1 2.667 2.667v8.666a2.667 2.667 0 0 1 -2.667 2.667h-8.666a2.667 2.667 0 0 1 -2.667 -2.667z" />
        <path d="M4.012 16.737a2.005 2.005 0 0 1 -1.012 -1.737v-10c0 -1.1 .9 -2 2 -2h10c.75 0 1.158 .385 1.5 1" />
      </svg>
      <svg xmlns="http://www.w3.org/2000/svg" class="icon clipboard-feedback" width="24" height="24" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round">
        <path stroke="none" d="M0 0h24v24H0z" fill="none" />
        <path d="M5 12l5 5l10 -10" />
      </svg>
    </button>
  </div>
  <pre id="code-block-header"><code>export default {
theme: 'dark',
radius: 'lg',
}</code></pre>
</div>

Line numbers and highlighted lines

Add .code-block-numbered and put every line in a .code-line. Add .code-line-highlight to the lines you want to point out. Write every .code-line on its own line, with nothing but the line break between the tags.

share.js
const button = document.querySelector('#share')

Clipboard.getOrCreateInstance(button, {
  text: window.location.href,
}).copy()
<div class="code-block code-block-numbered">
  <div class="code-block-header">
    <span class="code-block-title">share.js</span>
    <button type="button" class="code-block-copy" data-bs-toggle="clipboard" data-bs-target="#code-block-numbered" aria-label="Copy code">
      <svg xmlns="http://www.w3.org/2000/svg" class="icon clipboard-label" width="24" height="24" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round">
        <path stroke="none" d="M0 0h24v24H0z" fill="none" />
        <path d="M7 7m0 2.667a2.667 2.667 0 0 1 2.667 -2.667h8.666a2.667 2.667 0 0 1 2.667 2.667v8.666a2.667 2.667 0 0 1 -2.667 2.667h-8.666a2.667 2.667 0 0 1 -2.667 -2.667z" />
        <path d="M4.012 16.737a2.005 2.005 0 0 1 -1.012 -1.737v-10c0 -1.1 .9 -2 2 -2h10c.75 0 1.158 .385 1.5 1" />
      </svg>
      <svg xmlns="http://www.w3.org/2000/svg" class="icon clipboard-feedback" width="24" height="24" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round">
        <path stroke="none" d="M0 0h24v24H0z" fill="none" />
        <path d="M5 12l5 5l10 -10" />
      </svg>
    </button>
  </div>
  <pre id="code-block-numbered"><code><span class="code-line">const button = document.querySelector('#share')</span>
<span class="code-line"></span>
<span class="code-line code-line-highlight">Clipboard.getOrCreateInstance(button, {</span>
<span class="code-line code-line-highlight">  text: window.location.href,</span>
<span class="code-line code-line-highlight">}).copy()</span></code></pre>
</div>

Max height

Add .code-block-scroll to scroll long code vertically. Change the height with --tblr-code-block-max-height. Long lines always scroll horizontally.

import { Clipboard } from '@tabler/core'

const button = document.querySelector('#share')
const clipboard = Clipboard.getOrCreateInstance(button, {
  text: window.location.href,
})

clipboard.copy()
<div class="code-block code-block-scroll code-block-numbered" style="--tblr-code-block-max-height: 7rem">
  <button type="button" class="code-block-copy" data-bs-toggle="clipboard" data-bs-target="#code-block-scroll" aria-label="Copy code">
    <svg xmlns="http://www.w3.org/2000/svg" class="icon clipboard-label" width="24" height="24" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round">
      <path stroke="none" d="M0 0h24v24H0z" fill="none" />
      <path d="M7 7m0 2.667a2.667 2.667 0 0 1 2.667 -2.667h8.666a2.667 2.667 0 0 1 2.667 2.667v8.666a2.667 2.667 0 0 1 -2.667 2.667h-8.666a2.667 2.667 0 0 1 -2.667 -2.667z" />
      <path d="M4.012 16.737a2.005 2.005 0 0 1 -1.012 -1.737v-10c0 -1.1 .9 -2 2 -2h10c.75 0 1.158 .385 1.5 1" />
    </svg>
    <svg xmlns="http://www.w3.org/2000/svg" class="icon clipboard-feedback" width="24" height="24" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round">
      <path stroke="none" d="M0 0h24v24H0z" fill="none" />
      <path d="M5 12l5 5l10 -10" />
    </svg>
  </button>
  <pre id="code-block-scroll"><code><span class="code-line">import { Clipboard } from '@tabler/core'</span>
<span class="code-line"></span>
<span class="code-line">const button = document.querySelector('#share')</span>
<span class="code-line">const clipboard = Clipboard.getOrCreateInstance(button, {</span>
<span class="code-line">  text: window.location.href,</span>
<span class="code-line">})</span>
<span class="code-line"></span>
<span class="code-line">clipboard.copy()</span></code></pre>
</div>

Without copy button

Leave out the .code-block-copy button for a plain block.

npm install @tabler/core
<div class="code-block">
  <pre><code>npm install @tabler/core</code></pre>
</div>

Syntax highlighting

The code block does not highlight syntax. Put the output of a highlighter, such as Prism or Shiki, inside the code element. The copy button reads the text, so the markup of the highlighter does not end up in the clipboard.

Component

In Astro, use the CodeBlock component. It takes the code as a prop and renders the lines, the header and the copy button.

<CodeBlock code={code} title="share.js" language="JavaScript" numbered highlight={[3, 4]} />

Class reference

Every class this component ships, grouped by what it changes. A name in braces stands for a family — {color} is any base color, {breakpoint} any responsive step.

code-block componentThe wrapper with the tinted surface and rounded corners
code-block-header partThe bar above the code with the title, the language and the copy button
code-block-title partThe title or file name in the header
code-block-lang partThe language label in the header
code-block-copy partThe copy button. Without a header it floats in the top right corner
code-line partOne line of code, used for line numbers and highlighting
code-line-highlight partHighlights a `.code-line`
code-block-numbered modifierShows a line number before every `.code-line`
code-block-scroll modifierLimits the height of the code and scrolls it vertically

CSS

Variables

The component uses local CSS variables for real-time customization. Values for the CSS variables are set via Sass, so Sass customization is still supported, too.

On .code-block. Source: core/scss/ui/_code-block.scss.

--code-block-bg: #{$code-block-bg};
--code-block-color: #{$code-block-color};
--code-block-border-color: #{$code-block-border-color};
--code-block-border-radius: #{$code-block-border-radius};
--code-block-header-bg: #{$code-block-header-bg};
--code-block-header-color: #{$code-block-header-color};
--code-block-header-border-color: #{$code-block-header-border-color};
--code-block-header-padding-y: #{$code-block-header-padding-y};
--code-block-header-padding-x: #{$code-block-header-padding-x};
--code-block-title-color: #{$code-block-title-color};
--code-block-title-font-weight: #{$code-block-title-font-weight};
--code-block-lang-font-size: #{$code-block-lang-font-size};
--code-block-copy-size: #{$code-block-copy-size};
--code-block-copy-offset: #{$code-block-copy-offset};
--code-block-copy-hover-bg: #{$code-block-copy-hover-bg};
--code-block-line-number-color: #{$code-block-line-number-color};
--code-block-line-number-width: #{$code-block-line-number-width};
--code-block-line-number-gap: #{$code-block-line-number-gap};
--code-block-highlight-bg: #{$code-block-highlight-bg};
--code-block-highlight-border-color: #{$code-block-highlight-border-color};
--code-block-max-height: #{$code-block-max-height};

Sass variables

Override these variables before importing Tabler to change the defaults.

Source: core/scss/_variables.scss.

$code-block-bg: $pre-bg;
$code-block-color: $pre-color;
$code-block-border-color: var(--border-color);
$code-block-border-radius: var(--border-radius);
$code-block-header-bg: transparent;
$code-block-header-color: var(--secondary);
$code-block-header-border-color: $code-block-border-color;
$code-block-header-padding-y: 0.375rem;
$code-block-header-padding-x: 1rem;
$code-block-title-color: $code-block-color;
$code-block-title-font-weight: $font-weight-medium;
$code-block-lang-font-size: $h6-font-size;
$code-block-copy-size: 1.75rem;
$code-block-copy-offset: 0.5rem;
$code-block-copy-hover-bg: color-transparent(var(--code-block-color), 0.1);
$code-block-line-number-color: color-transparent(var(--code-block-color), 0.35);
$code-block-line-number-width: 2ch;
$code-block-line-number-gap: 1rem;
$code-block-highlight-bg: color-transparent(var(--code-block-color), 0.08);
$code-block-highlight-border-color: var(--primary);
$code-block-max-height: 20rem;