Code

A command or a piece of code, shown as written, with a button to copy it.

When to use it

Commands, file contents and code that must be copied exactly as written, mainly in guides and documentation.

When not to

A single word in a sentence: mark it in the text instead. It is not for long logs; link to a file to download.

Accessibility

The code keeps its lines and spaces and scrolls sideways on its own instead of the page. The block can be reached with the keyboard to scroll it. The copy button appears only where the browser can copy, and says when it did.

See it live

The component library shows this component in the default theme, in every variant: open it in the component library

The code example as the component library shows it in the default theme

Fields

What the component’s dialog asks for. In a Doc, keyed rows start with the key; rows in order follow the order below. Each row is a key and its value, in any order.

Field

Key

Kind

Required

Code

code

Text kept exactly as written

Yes

Language

language

Plain text, one line

No

Variants

This component has no variants of its own; a theme can add some.

In Google Docs

A component is a table. Its first cell names it; the rows below hold its fields. This is the example table:

code

code

npm install

npm start

language

Bash

In a page file

In a site kept as a folder or in Git, the same component is a block of the page’s JSON file:

JSON
{
  "name": "code",
  "rows": [
    [
      "code",
      "npm install\nnpm start"
    ],
    [
      "language",
      "Bash"
    ]
  ]
}

Markup and classes

What the component writes takes these classes, for a theme’s CSS: the component’s own name, name__part for its parts and name--variant for each variant (the variant’s plain name is there too).

Class

What it marks

code

The component

code__comment

A part of it

code__copy

A part of it

code__key

A part of it

code__keyword

A part of it

code__language

A part of it

code__number

A part of it

code__pre

A part of it

code__string

A part of it

code__text

A part of it

The HTML the example above writes, as the component library builds it in the default theme:

html
            <figure class="code" data-code data-copy="Copy" data-copied="Copied">
              <figcaption class="code__language">Bash</figcaption>
              <pre class="code__pre" tabindex="0"><code class="code__text">npm install
npm start</code></pre>
            </figure>

Visitor texts

Texts it shows visitors, in the page’s language. Every one is in the visitor texts reference.

Key

Text

site.code.copy

Copy

site.code.copied

Copied

Extending it

Its files in SeedPage. A theme or a site changes the markup by giving a file of the same name (the Liquid part), and a new component can extend this one.

  • src/components/code.js
  • src/components/code/code.liquid
  • src/components/code.client.js

It has a browser script, the .client.js file above. Without it the component still shows all its content; the script only adds behaviour.