Skip to content
Español

Navigate

Type to search. Press Escape to close.

    Components · js New in 1.1

    Code block

    A frame for a snippet: a title bar with the file name, the language and a copy button, over a scrolling <pre>. The panel is the deep navy of the brand in both themes, like the terminal of the hero, so a snippet looks the same on a light page and a dark one; iv-code--light follows the theme instead. The copy button comes from Copy, a separate 1.1 module, and stays hidden until that module is running — the page never shows a button that does nothing.

    CSS: css/components/code.css (in ivolt.css). JS: @intervolutions/ivolt/copy or auto.min.js, only for the copy button. New in 1.1.

    Dark and light frames with copyfixtures/code/basic.htmlOpen alone — Dark and light frames with copy
    <div class="iv-stack iv-u-gap-5" data-iv-component="copy">
      <figure class="iv-code">
        <figcaption class="iv-code__bar">
          <span class="iv-code__title">index.html</span>
          <span class="iv-code__lang">HTML</span>
          <button class="iv-code__copy" type="button" data-iv-copy><span class="iv-code__copy-label">Copy</span></button>
        </figcaption>
        <pre class="iv-code__pre" tabindex="0"><code><span class="iv-code__k">&lt;link</span> <span class="iv-code__a">rel</span>=<span class="iv-code__s">"stylesheet"</span> <span class="iv-code__a">href</span>=<span class="iv-code__s">"ivolt.min.css"</span><span class="iv-code__k">&gt;</span>
    <mark class="iv-code__mark"><span class="iv-code__k">&lt;html</span> <span class="iv-code__a">class</span>=<span class="iv-code__s">"iv-root"</span> <span class="iv-code__a">data-iv-theme</span>=<span class="iv-code__s">"system"</span><span class="iv-code__k">&gt;</span></mark>
    <span class="iv-code__c">&lt;!-- one variable repaints the system --&gt;</span>
    <span class="iv-code__k">&lt;style&gt;</span> :root { --iv-color-primary: #E11D48; } <span class="iv-code__k">&lt;/style&gt;</span></code></pre>
      </figure>
    
      <figure class="iv-code iv-code--light">
        <figcaption class="iv-code__bar">
          <span class="iv-code__title">Install</span>
          <button class="iv-code__copy" type="button" data-iv-copy="#code-install"><span class="iv-code__copy-label">Copy</span></button>
        </figcaption>
        <pre class="iv-code__pre" tabindex="0"><code id="code-install">npm install @intervolutions/ivolt</code></pre>
      </figure>
    
      <p class="iv-u-m-0 iv-u-text-sm iv-u-text-muted">The copy buttons appear once <code>Copy</code> (from <code>@intervolutions/ivolt/copy</code> or <code>auto.min.js</code>) has started on the block; without it they stay hidden. The colours of the snippet are hand-written classes, not a highlighter.</p>
    </div>
    .iv-root {
      --iv-color-primary: #E11D48;
    }
    // data-iv-component="copy"
    {
      copy: true,
    }

    When to use it

    Use it for any code a reader is expected to copy: an install command, a configuration file, an HTML snippet in a tutorial. Inline code in a sentence stays a plain <code>. Syntax highlighting is not part of the package — the colours in the example are four hand-written classes (iv-code__k, __s, __a, __c for keywords, strings, attributes and comments) — and the classes of any highlighter you already use work inside code.

    Anatomy

    <div data-iv-component="copy">
      <figure class="iv-code">
        <figcaption class="iv-code__bar">
          <span class="iv-code__title">index.html</span>
          <span class="iv-code__lang">HTML</span>
          <button class="iv-code__copy" type="button" data-iv-copy>
            <span class="iv-code__copy-label">Copy</span>
          </button>
        </figcaption>
        <pre class="iv-code__pre" tabindex="0"><code>…</code></pre>
      </figure>
    </div>

    Wrap a line in <mark class="iv-code__mark"> to highlight it with a brand bar at the start. The <pre> scrolls in both directions and stops growing at --iv-code-max (32rem), and it carries tabindex="0" so a keyboard can scroll it.

    The copy button

    import { Copy } from "@intervolutions/ivolt/copy";
    Copy.initAll(document);
    
    // or, from a CDN, one file that starts everything plus Copy:
    // <script type="module" src="…/dist/js/auto.min.js"></script>

    A click on [data-iv-copy] copies the text of the element its value names (data-iv-copy="#code-install") or, when empty, the code of the nearest .iv-code. While it works, the button carries data-iv-state="copied" or "failed" for timeout milliseconds and its label changes; if the clipboard refuses, the text is selected so the reader can press Ctrl+C. Options (defaults < data-iv-* < JS): copiedText (“Copied”), failedText (“Press Ctrl+C to copy”) and timeout (2000). Events: iv:copy, cancelable, before copying, and iv:copied with ok after. Copy is not in index.js or the IIFE, so the default bundle did not grow for it.

    Accessibility

    • The <figcaption> names the figure, so the file name is read with the code.
    • The result of a copy is announced in a polite role="status" region that Copy creates, not only by the changed label.
    • The scrolling <pre> takes focus (WCAG 2.1.1), and its focus ring is drawn just inside the frame so the rounded corners do not clip it.
    • The copy button and the scrolling block draw a cyan focus ring that reads on the navy panel; in forced colours the frame and the marked line keep their edges.

    Customising

    .tutorial .iv-code {
      --iv-code-bg: #0b1020;
      --iv-code-max: 24rem;
    }

    Locals: --iv-code-bg, --iv-code-fg, --iv-code-muted (the bar and the comments) and --iv-code-max. The light variant sets the first three to the theme's surface, text and muted text.

    Class index

    Every class of the family. The full list per stylesheet lives in the reference.

    ClassWhat it does
    iv-codeThe frame, on a <figure>.
    iv-code__barTitle bar, on the <figcaption>.
    iv-code__title · iv-code__langFile name and language.
    iv-code__copy · iv-code__copy-labelCopy button and its text; hidden until Copy runs.
    iv-code__preThe scrolling block.
    iv-code__markA highlighted line.
    iv-code__k · __s · __a · __cHand-written token colours.
    iv-code--lightFrame in the theme's surface instead of navy.